-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
fix(Intersection): bug causing selection edge case #8735
Merged
Merged
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
6863ed8
fix
ShaMan123 b3184e6
proof
ShaMan123 e039a3a
Update CHANGELOG.md
ShaMan123 3c6cfc0
Update intersection.js
ShaMan123 eef83a6
slope
ShaMan123 efbf857
Revert "slope"
ShaMan123 705ce6a
Update intersection.js
ShaMan123 ab5add3
zero
ShaMan123 a7a02b8
reorder
ShaMan123 122608f
Update intersection.js
ShaMan123 1e6c57c
Update Intersection.ts
ShaMan123 dd644b9
wikis
ShaMan123 275a9cd
jsdocify
ShaMan123 05af7ca
Update intersection.js
ShaMan123 7d72d85
Update intersection.js
ShaMan123 7845627
cleanup
ShaMan123 8282094
pure linear algebra
ShaMan123 adc3b24
Merge branch 'master' into fix-intersection-edge-case
ShaMan123 0df947b
Update Intersection.ts
ShaMan123 f4fb162
Update Intersection.ts
ShaMan123 7214d87
rename + infinite
ShaMan123 9df5095
coverage
ShaMan123 370b175
Merge branch 'master' into fix-intersection-edge-case
asturur 74b3db7
useful comments
ShaMan123 8937960
fix commit
ShaMan123 387279a
Update Intersection.ts
ShaMan123 52d7b43
Update Intersection.ts
ShaMan123 633ec84
Update Intersection.ts
ShaMan123 1c68531
checkout
ShaMan123 acda592
Update Intersection.ts
asturur File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { Point } from './Point'; | ||
import { createVector, slope } from './util/misc/vectors'; | ||
import { createVector } from './util/misc/vectors'; | ||
|
||
/* Adaptation of work of Kevin Lindsey (kevin@kevlindev.com) */ | ||
|
||
|
@@ -40,27 +40,50 @@ export class Intersection { | |
} | ||
|
||
/** | ||
* check if `T` is contained in `[A, B]` by comparing the direction of the vectors from `T` to `A` and `B` | ||
* @param T | ||
* @param A | ||
* @param B | ||
* check if point T is on the segment or line defined between A and B | ||
* | ||
* @param {Point} T the point we are checking for | ||
* @param {Point} A one extremity of the segment | ||
* @param {Point} B the other extremity of the segment | ||
* @param [extendToLine] if true checks if `T` is on the line defined by `A` and `B` | ||
* @returns true if `T` is contained | ||
*/ | ||
static isContainedInInterval(T: Point, A: Point, B: Point) { | ||
if (T.eq(A) || T.eq(B)) { | ||
return true; | ||
} | ||
const AB = createVector(A, B); | ||
if (!AB.eq(new Point())) { | ||
const s = slope(AB); | ||
return s === slope(createVector(T, B)) && s === slope(createVector(T, B)); | ||
static isPointContained(T: Point, A: Point, B: Point, extendToLine = false) { | ||
if (A.eq(B)) { | ||
// Edge case: the segment is a point, we check for coincidence, | ||
// extendToLine has no meaning because there are infinite lines to consider | ||
return T.eq(A); | ||
} else if (A.x === B.x) { | ||
// Edge case: horizontal line. | ||
// we first check if T.x is on the same value, and then if T.y is contained between A.y and B.y | ||
return ( | ||
T.x === A.x && | ||
(extendToLine || (T.y >= Math.min(A.y, B.y) && T.y <= Math.max(A.y, B.y))) | ||
); | ||
} else if (A.y === B.y) { | ||
// Edge case: vertical line. | ||
// we first check if T.y is on the same value, and then if T.x is contained between A.x and B.x | ||
return ( | ||
T.y === A.y && | ||
(extendToLine || (T.x >= Math.min(A.x, B.x) && T.x <= Math.max(A.x, B.x))) | ||
); | ||
} else { | ||
return false; | ||
// generic case: we normalize AT over AB. | ||
// then for the line case we just verify that AB has the same inclination than AT | ||
// for the segment case we need the same direction and the length of AT needs to be less than AB | ||
const AB = createVector(A, B); | ||
const AT = createVector(A, T); | ||
const s = AT.divide(AB); | ||
return infinite | ||
? Math.abs(s.x) === Math.abs(s.y) | ||
: s.x === s.y && s.x >= 0 && s.x <= 1; | ||
} | ||
} | ||
|
||
/** | ||
* Checks if a line intersects another | ||
* @see {@link https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection line intersection} | ||
* @see {@link https://en.wikipedia.org/wiki/Cramer%27s_rule Cramer's rule} | ||
* @static | ||
* @param {Point} a1 | ||
* @param {Point} a2 | ||
|
@@ -78,12 +101,12 @@ export class Intersection { | |
aInfinite = true, | ||
bInfinite = true | ||
): Intersection { | ||
const b2xb1x = b2.x - b1.x, | ||
a1yb1y = a1.y - b1.y, | ||
const a2xa1x = a2.x - a1.x, | ||
a2ya1y = a2.y - a1.y, | ||
b2xb1x = b2.x - b1.x, | ||
b2yb1y = b2.y - b1.y, | ||
a1xb1x = a1.x - b1.x, | ||
a2ya1y = a2.y - a1.y, | ||
a2xa1x = a2.x - a1.x, | ||
a1yb1y = a1.y - b1.y, | ||
Comment on lines
+107
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed order to be less confusing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and found the algebra behind it and documented it |
||
uaT = b2xb1x * a1yb1y - b2yb1y * a1xb1x, | ||
ubT = a2xa1x * a1yb1y - a2ya1y * a1xb1x, | ||
uB = b2yb1y * a2xa1x - b2xb1x * a2ya1y; | ||
|
@@ -105,11 +128,11 @@ export class Intersection { | |
const segmentsCoincide = | ||
aInfinite || | ||
bInfinite || | ||
Intersection.isContainedInInterval(a1, b1, b2) || | ||
Intersection.isContainedInInterval(a2, b1, b2) || | ||
Intersection.isContainedInInterval(b1, a1, a2) || | ||
Intersection.isContainedInInterval(b2, a1, a2); | ||
return new this(segmentsCoincide ? 'Coincident' : undefined); | ||
Intersection.isPointContained(a1, b1, b2) || | ||
Intersection.isPointContained(a2, b1, b2) || | ||
Intersection.isPointContained(b1, a1, a2) || | ||
Intersection.isPointContained(b2, a1, a2); | ||
return new Intersection(segmentsCoincide ? 'Coincident' : undefined); | ||
} else { | ||
return new Intersection('Parallel'); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure about thte name
extendToLine
I chose
infinite
same as the rest of the methods, but maybe that isn't great for non math usersThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
took me a while to understand that infinite wasn't Math.inifinite, and i was telling myself, infinite is always true.
So i tried with a different name.
Was this infinite here before we restructured the class from 5.x?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see infinite i used in all the class and i think we should come up with a better name for it
considerSegmentAsLine, or something that. can be read and let not wonder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i ll put back infinite for consistency, and then we can rename all together
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, I put it I think to centralize logic