-
-
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 20 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,25 +1,10 @@ | ||
import { Point } from './Point'; | ||
import { createVector } from './util/misc/vectors'; | ||
|
||
/* Adaptation of work of Kevin Lindsey ([email protected]) */ | ||
|
||
export type IntersectionType = 'Intersection' | 'Coincident' | 'Parallel'; | ||
|
||
/** | ||
* **Assuming `T`, `A`, `B` are points on the same line**, | ||
* 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 | ||
* @returns true if `T` is contained | ||
*/ | ||
const isContainedInInterval = (T: Point, A: Point, B: Point) => { | ||
const TA = new Point(T).subtract(A); | ||
const TB = new Point(T).subtract(B); | ||
return ( | ||
Math.sign(TA.x) !== Math.sign(TB.x) || Math.sign(TA.y) !== Math.sign(TB.y) | ||
); | ||
}; | ||
|
||
export class Intersection { | ||
declare points: Point[]; | ||
|
||
|
@@ -54,8 +39,39 @@ export class Intersection { | |
return this; | ||
} | ||
|
||
/** | ||
* check if `T ∈ [A, B]` | ||
* | ||
* Solving the linear equation `A + s * AB = T` will give us `s`\ | ||
* If `s` has a solution and `s ∈ [0, 1]` then `T ∈ [A, B]` | ||
* If `AB` has a zero component we revert to a simple coordinate check | ||
* | ||
* @param T | ||
* @param A | ||
* @param B | ||
* @returns true if `T` is contained | ||
*/ | ||
static isContainedInInterval(T: Point, A: Point, B: Point) { | ||
if (A.x === B.x) { | ||
return ( | ||
T.x === A.x && T.y >= Math.min(A.y, B.y) && T.y <= Math.max(A.y, B.y) | ||
); | ||
} else if (A.y === B.y) { | ||
return ( | ||
T.y === A.y && T.x >= Math.min(A.x, B.x) && T.x <= Math.max(A.x, B.x) | ||
); | ||
} else { | ||
const AB = createVector(A, B); | ||
const AT = createVector(A, T); | ||
const s = AT.divide(AB); | ||
return 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 | ||
|
@@ -73,12 +89,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, | ||
uaT = b2xb1x * a1yb1y - b2yb1y * a1xb1x, | ||
ubT = a2xa1x * a1yb1y - a2ya1y * a1xb1x, | ||
uB = b2yb1y * a2xa1x - b2xb1x * a2ya1y; | ||
|
@@ -100,10 +116,10 @@ export class Intersection { | |
const segmentsCoincide = | ||
aInfinite || | ||
bInfinite || | ||
isContainedInInterval(a1, b1, b2) || | ||
isContainedInInterval(a2, b1, b2) || | ||
isContainedInInterval(b1, a1, a2) || | ||
isContainedInInterval(b2, a1, a2); | ||
Intersection.isContainedInInterval(a1, b1, b2) || | ||
Intersection.isContainedInInterval(a2, b1, b2) || | ||
Intersection.isContainedInInterval(b1, a1, a2) || | ||
Intersection.isContainedInInterval(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
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.
changed order to be less confusing
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.
and found the algebra behind it and documented it