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

feat(): intersectsLine line type #213

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Conversation

ShaMan123
Copy link

Motivation

I have use cases in which I need to specify the line type (line, ray, segment) for different use cases, e.g. ray casting algorithm to determine if point is contained in a closed shape, segment for rect/bezier intersections and line for finding extreme points on the bezier that intersect without needs to get the hull/bbox

Changes

I have changed to lineIntersects method to respect the 3 types of line, segment being the default value as it is currently.

bezier.lineIntersects({ p1, p2, type: 'ray' }) // ray from p1 to p2

The line case test I have added surfaced a bug in roots or at least a precision error that creates quite a substantial difference. I tried debugging it and saw that when I pass a line and a line pointing to the opposite direction the aligned method rotates the curve wrt the line and that is where the precision error originates.
I wonder if using a change basis calculation will fix this (I will try).
If not maybe consider sorting the line points so that the output is the same for both cases.

I admire your work
Thanks for this great repo

Copy link
Author

@ShaMan123 ShaMan123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried out basis change 0f25a14 and indeed it fixed the precision error
Now 2 tests are failing, but since both of them are degenerated beziers maybe this should happen. If I understand correctly the basis change, it means both lines coincide after applying the basis change.

src/utils.js Outdated
return points.map((p) => {
const x = p.x - tx;
const y = p.y - ty;
// Apply a basis change on x,y to align the x axis of the curve with the line
Copy link
Author

@ShaMan123 ShaMan123 Jan 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure the comment is 100% accurate
I understand that the curve moves to p1, p1 becoming its zero point and is rotated to align its x axis with the line.
Is that correct?

@ShaMan123
Copy link
Author

I logged some values and it seems I am right

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 this pull request may close these issues.

1 participant