浏览代码

优化判断点在线上的计算

zhaoyk 2 年之前
父节点
当前提交
952ab0760f
共有 3 个文件被更改,包括 51 次插入42 次删除
  1. 3 17
      adm_comp/src/lib/DiagramEditor.ts
  2. 17 25
      adm_comp/src/lib/TemplateEditor.ts
  3. 31 0
      adm_comp/src/lib/UIUtils.ts

+ 3 - 17
adm_comp/src/lib/DiagramEditor.ts

@@ -482,27 +482,13 @@ class EditUtil {
 	}
 	
 	private onPath(x:number, y:number, path: Array<Point>): boolean {
+		var p = new Point(x, y);
 		var pre: Point = null;
 		for(var point of path) {
 			point = this.toCanvas(point);
 			if(pre != null) {
-				var x1:number;
-				var y1:number;
-				var x2:number;
-				var y2:number;
-				const scope = 3;
-				if(pre.y == point.y) { //水平
-					y1 = pre.y - scope;
-					y2 = pre.y + scope;
-					x1 = Math.min(pre.x, point.x);
-					x2 = Math.max(pre.x, point.x);
-				} else { //垂直
-					x1 = pre.x - scope;
-					x2 = pre.x + scope;
-					y1 = Math.min(pre.y, point.y);
-					y2 = Math.max(pre.y, point.y);
-				}
-				if(x >= x1 && y >= y1 && x <= x2 && y <= y2)
+				var d = ViewTool.distance(p, [pre, point]);
+				if(d < 3)
 					return true;
 			}
 			pre = point;	

+ 17 - 25
adm_comp/src/lib/TemplateEditor.ts

@@ -488,35 +488,27 @@ class EditUtil {
 		if(this.template.mainPipes) {
 			for(const mp of this.template.mainPipes){
 				for(const line of mp.locationPath) {
-					var pre: Point = null;
-					for(var point of line) {
-						point = this.toCanvas(point);
-						if(pre != null) {
-							var x1:number;
-							var y1:number;
-							var x2:number;
-							var y2:number;
-							const scope = 3;
-							if(pre.y == point.y) { //水平
-								y1 = pre.y - scope;
-								y2 = pre.y + scope;
-								x1 = Math.min(pre.x, point.x);
-								x2 = Math.max(pre.x, point.x);
-							} else { //垂直
-								x1 = pre.x - scope;
-								x2 = pre.x + scope;
-								y1 = Math.min(pre.y, point.y);
-								y2 = Math.max(pre.y, point.y);
-							}
-							if(x >= x1 && y >= y1 && x <= x2 && y <= y2)
-								return mp;
-						}
-						pre = point;	
-					}	
+					if(this.onPath(x, y, line))
+						return mp;
 				}
 			}
 		}
 		return null;
 	}
 		
+	private onPath(x:number, y:number, path: Array<Point>): boolean {
+		var p = new Point(x, y);
+		var pre: Point = null;
+		for(var point of path) {
+			point = this.toCanvas(point);
+			if(pre != null) {
+				var d = ViewTool.distance(p, [pre, point]);
+				if(d < 3)
+					return true;
+			}
+			pre = point;	
+		}
+		return false;
+	}
+		
 }

+ 31 - 0
adm_comp/src/lib/UIUtils.ts

@@ -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;
+	}
+	
 	
 }