| 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 = {}; |