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

Intersect line method does not always return existing intersections #205

Open
Andrey-Gutsy opened this issue Aug 7, 2023 · 4 comments
Open

Comments

@Andrey-Gutsy
Copy link

There seems to be a problem when calculating intersections between a bezier curve and a line, at very specific cases wIth the common thread being that the intersecting line is parallel to the Y axis.

Wrong result:
In the problem cases below, the code returns an empty array (no intersections) even though the curve does intersect with the line.

Side effect:
Another oddity I've noticed -
for the cases when getting incorrect results for .intersects (these that yield incorrect empty result), the .intersects call seems to mutate the curve object by adding another property "t" to the curve's first point.

Example case:

  const bzCurve = new Bezier(
    {
      x: 623.0342104670767,
      y: 427.30684253642517
    },
    {
      x: 359.8595533490565,
      y: 519.6488274901164
    },
    {
      x: 96.68489623103626,
      y: 427.3068425364252
    }
  );

  const p1 = { x: 608.0342104670767, y: 443.30684253642517 };
  const p2 = { x: 608.0342104670767, y: 422.30684253642517 };
  const ep = 0.00001;

  console.log(bzCurve.points[0]); // { x: 623.0342104670767, y: 427.30684253642517 }

  // original line - problem with .intersects
  console.log('result', bzCurve.intersects({ p1, p2 })); // []
 
  // moving one point slightly so the line is not parallel to Y axis, correct results
  const p1_1 = { ...p1, x: p1.x + ep };
  console.log('result_1', bzCurve.intersects({ p1: p1_1, p2 })); // [0.02849817524894296]
  const p1_2 = { ...p1, x: p1.x - ep };
  console.log('result_2', bzCurve.intersects({ p1: p1_2, p2 })); // [0.028498193956362425]

  // shifting both points, thereby shifting the line slightly - on the X axis. line is again parallel to the Y-axis.
  // getting incorrect results again
  const p2_2 = { ...p2, x: p2.x - ep };
  console.log('result_3', bzCurve.intersects({ p1: p1_2, p2: p2_2 })); // []

  // observe - the key "t" was added to the first point on the curve
  // this property is added only when calling .intersects that returns no intersections.
  console.log(bzCurve.points[0]); // { x: 623.0342104670767, y: 427.30684253642517, t: 0 }
);
@Andrey-Gutsy Andrey-Gutsy changed the title Intersect line method is not always finding intersections Intersect line method does not always return existing intersections Aug 7, 2023
@ShaMan123
Copy link

duplicate #179 ?

@Pomax
Copy link
Owner

Pomax commented Nov 23, 2023

might be a different case for the same underlying symptom, so best case one solution fixes both. I finally have a few spares days so I'll be rewriting bezier.js to modern JS instead of something that had to stay IE compatible and hopefully catch this problem at the same time.

@ShaMan123
Copy link

I am really happy I came across this repo.
Solved something that was deemed too hard to solve or absolutely not performant with what I managed without it.
Thanks!

@Pomax
Copy link
Owner

Pomax commented Nov 23, 2023

cheers

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

No branches or pull requests

3 participants