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

Class Geometry

geometry.js:1–84  ·  view source on GitHub ↗

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) {
16 const L2 = ( ((line2.x - line1.x) * (line2.x - line1.x)) + ((line2.y - line1.y) * (line2.y - line1.y)) );
17 if(L2 == 0) return false;
18 const s = (((line1.y - pnt.y) * (line2.x - line1.x)) - ((line1.x - pnt.x) * (line2.y - line1.y))) / L2;
19 return Math.abs(s) * Math.sqrt(L2);
20 }
21
22 //Returns bool, whether the projected point is actually inside the (finite) line segment.
23 static calcIsInsideLineSegment(line1, line2, pnt) {
24 const L2 = ( ((line2.x - line1.x) * (line2.x - line1.x)) + ((line2.y - line1.y) * (line2.y - line1.y)) );
25 if(L2 == 0) return false;
26 const r = ( ((pnt.x - line1.x) * (line2.x - line1.x)) + ((pnt.y - line1.y) * (line2.y - line1.y)) ) / L2;
27
28 return (0 <= r) && (r <= 1);
29 }
30
31 //The most useful function. Returns bool true, if the mouse point is actually inside the (finite) line, given a line thickness from the theoretical line away. It also assumes that the line end points are circular, not square.
32 static calcIsInsideThickLineSegment(line1, line2, pnt, lineThickness) {
33 const L2 = ( ((line2.x - line1.x) * (line2.x - line1.x)) + ((line2.y - line1.y) * (line2.y - line1.y)) );
34 if(L2 == 0) return false;
35 const r = ( ((pnt.x - line1.x) * (line2.x - line1.x)) + ((pnt.y - line1.y) * (line2.y - line1.y)) ) / L2;
36
37 //Assume line thickness is circular
38 if(r < 0) {
39 //Outside line1
40 return (Math.sqrt(( (line1.x - pnt.x) * (line1.x - pnt.x) ) + ( (line1.y - pnt.y) * (line1.y - pnt.y) )) <= lineThickness);
41 } else if((0 <= r) && (r <= 1)) {
42 //On the line segment
43 const s = (((line1.y - pnt.y) * (line2.x - line1.x)) - ((line1.x - pnt.x) * (line2.y - line1.y))) / L2;
44 return (Math.abs(s) * Math.sqrt(L2) <= lineThickness);
45 } else {
46 //Outside line2
47 return (Math.sqrt(( (line2.x - pnt.x) * (line2.x - pnt.x) ) + ( (line2.y - pnt.y) * (line2.y - pnt.y) )) <= lineThickness);
48 }
49 }
50
51 static calcLineMidPoint( a, b ){
52 const pt = {};
53 pt.x = (a.x - b.x)/2 + b.x;
54 pt.y = (a.y - b.y)/2 + b.y;
55
56 return pt;
57 }
58
59 static calcPointAlongLine( a, b, delta ){
60 const pt = {};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected