Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line2.closestPoint output is wrong #1225

Closed
achirita opened this issue Apr 8, 2023 · 1 comment · Fixed by #1226
Closed

line2.closestPoint output is wrong #1225

achirita opened this issue Apr 8, 2023 · 1 comment · Fixed by #1226

Comments

@achirita
Copy link
Contributor

achirita commented Apr 8, 2023

Expected Behavior

line2.closestPoint(..) should return a point on the line which is closest to the provided point.

Actual Behavior

In some cases line2.closestPoint(..) returns a point which is not on the line (see screenshots below).
If we consider a line going from [10, 0] to [0, 10] and try to get the point on the line which is closest to [0, 0] we get [-0.5952027266534774, 0.7912811580260264]. The correct result would be [5, 5].

If we switch to the line3.closestPoint(..) function and add 0 as the Z value for all the points we will get the correct result.

Steps to Reproduce the Problem

Get the closest point and draw a circle around it. The result is not correct

const jscad = require('@jscad/modeling')
const { line, circle } = jscad.primitives
const { line2 } = jscad.maths;

const main = () => {
  const start = [10, 0];
  const end = [0, 10];
  const point = [0, 0];
  
  const l = line2.fromPoints(line2.create(), start, end);
  const closest = line2.closestPoint(l, point);
  
  return [line([start, end]), circle({radius: 0.2, center: closest})];
}

module.exports = { main }

wrongPoint

Get the closest point using line3. This one returns the correct result.

const jscad = require('@jscad/modeling')
const { line, circle } = jscad.primitives
const { line3 } = jscad.maths;

const main = () => {
  const start = [10, 0, 0];
  const end = [0, 10, 0];
  const point = [0, 0, 0];
  
  const l = line3.fromPoints(line3.create(), start, end);
  const closest = line3.closestPoint(l, point);
  console.log(closest);
  
  return [line([start, end]), circle({radius: 0.2, center: closest})];
}

module.exports = { main }

correctPoint

Specifications

  • Version: modeling 2.11.0
  • Platform: all
  • Environment: all
@achirita
Copy link
Contributor Author

achirita commented Apr 8, 2023

One potential implementation based on this post https://forum.unity.com/threads/how-do-i-find-the-closest-point-on-a-line.340058/#post-2198950 would be:

const closestPoint = (line, point) => {
  const origin = vec2.scale([], line, line[2]);
  const direction = vec2.normal([], line);

  const v = vec2.subtract([], point, origin);
  const d = vec2.dot(v, direction);

  return vec2.add([], origin, vec2.scale([], direction, d));
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant