MCPcopy Create free account
hub / github.com/NikLever/ThreeJS-PathEditor / calcNearestPointOnLine

Method calcNearestPointOnLine

geometry.js:3–12  ·  view source on GitHub ↗
(line1, line2, pnt)

Source from the content-addressed store, hash-verified

1export class Geometry{
2 //Returns {.x, .y}, a projected point perpendicular on the (infinite) line.
3 static calcNearestPointOnLine(line1, line2, pnt) {
4 const L2 = ( ((line2.x - line1.x) * (line2.x - line1.x)) + ((line2.y - line1.y) * (line2.y - line1.y)) );
5 if(L2 == 0) return false;
6 const r = ( ((pnt.x - line1.x) * (line2.x - line1.x)) + ((pnt.y - line1.y) * (line2.y - line1.y)) ) / L2;
7
8 return {
9 x: line1.x + (r * (line2.x - line1.x)),
10 y: line1.y + (r * (line2.y - line1.y))
11 };
12 }
13
14 //Returns float, the shortest distance to the (infinite) line.
15 static calcDistancePointToLine(line1, line2, pnt) {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected