|
@@ -144,5 +144,36 @@ export class ViewTool {
|
|
|
}
|
|
|
return info;
|
|
|
}
|
|
|
+
|
|
|
+ static distance(point: Point, line: Array<Point>): number {
|
|
|
+ var i = ViewTool.getIntersection(point, line);
|
|
|
+ if(i.x >= Math.min(line[0].x, line[1].x) && i.x <= Math.max(line[0].x, line[1].x)
|
|
|
+ && i.y >= Math.min(line[0].y, line[1].y) && i.y <= Math.max(line[0].y, line[1].y)){
|
|
|
+ return Math.sqrt(Math.pow(point.x - i.x, 2) + Math.pow(point.y - i.y, 2));
|
|
|
+ }
|
|
|
+
|
|
|
+ var d1 = Math.sqrt(Math.pow(point.x - line[0].x, 2) + Math.pow(point.y - line[0].y, 2));
|
|
|
+ var d2 = Math.sqrt(Math.pow(point.x - line[1].x, 2) + Math.pow(point.y - line[1].y, 2));
|
|
|
+ return Math.min(d1, d2);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static getIntersection(point: Point, line: Array<Point>): Point {
|
|
|
+ let p1 = line[0];
|
|
|
+ let p2 = line[1];
|
|
|
+ var rtn = new Point(0, 0);
|
|
|
+
|
|
|
+ if(p1.x == p2.x) {
|
|
|
+ rtn.x = p1.x;
|
|
|
+ rtn.y = point.y;
|
|
|
+ } else {
|
|
|
+ var A = (p1.y - p2.y) / (p1.x - p2.x);
|
|
|
+ var B = p1.y - A * p1.x;
|
|
|
+ var m = point.x + A * point.y;
|
|
|
+ rtn.x = (m - A * B) / (A * A + 1);
|
|
|
+ rtn.y = A * point.x + B;
|
|
|
+ }
|
|
|
+ return rtn;
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
}
|