-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
442 additions
and
22 deletions.
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// From Grumdig's Javascript answer to this question: | ||
// https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment | ||
|
||
function sqr(x) { return x * x } | ||
function dist2(v, w) { return sqr(v.x - w.x) + sqr(v.y - w.y) } | ||
function distToSegmentSquared(p, v, w) { | ||
var l2 = dist2(v, w); | ||
if (l2 == 0) return dist2(p, v); | ||
var t = ((p.x - v.x) * (w.x - v.x) + (p.y - v.y) * (w.y - v.y)) / l2; | ||
t = Math.max(0, Math.min(1, t)); | ||
return dist2(p, { x: v.x + t * (w.x - v.x), | ||
y: v.y + t * (w.y - v.y) }); | ||
} | ||
function distToSegment(p, v, w) { return Math.sqrt(distToSegmentSquared(p, v, w)); } |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.