瀏覽代碼

修复bug

xushiting 2 年之前
父節點
當前提交
0bb9195691
共有 3 個文件被更改,包括 80 次插入31 次删除
  1. 2 2
      src/graphic/ListenLayer.js
  2. 11 10
      src/graphic/Service/CurveRoadService.js
  3. 67 19
      src/graphic/Util/MathUtil.js

+ 2 - 2
src/graphic/ListenLayer.js

@@ -806,7 +806,7 @@ export default class ListenLayer {
       }
 
       if (leftJoinInfo.distance < Constant.minAdsorbPix) {
-        const index = mathUtil.getIndexForCurvesPoints(
+        const index = mathUtil.getCurvesIndexForCurvesPoints(
           leftJoinInfo.position,
           curveRoad.points
         );
@@ -819,7 +819,7 @@ export default class ListenLayer {
           y: leftJoinInfo.position.y,
         };
       } else if (rightJoinInfo.distance < Constant.minAdsorbPix) {
-        const index = mathUtil.getIndexForCurvesPoints(
+        const index = mathUtil.getCurvesIndexForCurvesPoints(
           rightJoinInfo.position,
           curveRoad.points
         );

+ 11 - 10
src/graphic/Service/CurveRoadService.js

@@ -8,6 +8,7 @@ import Constant from "../Constant";
 import RoadService from "./RoadService";
 import { lineService } from "./LineService";
 import { uiService } from "./UIService";
+import { coordinate } from "../Coordinate";
 
 const offsetDis = 50;
 
@@ -945,7 +946,7 @@ export default class CurveRoadService extends RoadService {
           let points1 = mathUtil.RectangleVertex(
             curveRoad.points[i],
             curveRoad.points[i + 1],
-            newWidth + midDivideWidth / 2
+            (newWidth + midDivideWidth / 2) * coordinate.ratio
           );
           line1 = mathUtil.createLine1(
             points1.leftEdgeStart,
@@ -958,7 +959,7 @@ export default class CurveRoadService extends RoadService {
           let points1 = mathUtil.RectangleVertex(
             curveRoad.points[i - 1],
             curveRoad.points[i],
-            newWidth + midDivideWidth / 2
+            (newWidth + midDivideWidth / 2) * coordinate.ratio
           );
           line1 = mathUtil.createLine1(
             points1.leftEdgeStart,
@@ -989,7 +990,7 @@ export default class CurveRoadService extends RoadService {
             let points1 = mathUtil.RectangleVertex(
               curveRoad.points[i - 1],
               curveRoad.points[i],
-              newWidth + midDivideWidth / 2
+              (newWidth + midDivideWidth / 2) * coordinate.ratio
             );
             line1 = mathUtil.createLine1(
               points1.leftEdgeStart,
@@ -998,7 +999,7 @@ export default class CurveRoadService extends RoadService {
             let points2 = mathUtil.RectangleVertex(
               curveRoad.points[i],
               curveRoad.points[i + 1],
-              newWidth + midDivideWidth / 2
+              (newWidth + midDivideWidth / 2) * coordinate.ratio
             );
             line2 = mathUtil.createLine1(
               points2.leftEdgeStart,
@@ -1017,7 +1018,7 @@ export default class CurveRoadService extends RoadService {
           let points1 = mathUtil.RectangleVertex(
             curveRoad.points[i],
             curveRoad.points[i + 1],
-            newWidth + midDivideWidth / 2
+            (newWidth + midDivideWidth / 2) * coordinate.ratio
           );
           line1 = mathUtil.createLine1(
             points1.rightEdgeStart,
@@ -1030,11 +1031,11 @@ export default class CurveRoadService extends RoadService {
           let points1 = mathUtil.RectangleVertex(
             curveRoad.points[i - 1],
             curveRoad.points[i],
-            newWidth + midDivideWidth / 2
+            (newWidth + midDivideWidth / 2) * coordinate.ratio
           );
           line1 = mathUtil.createLine1(
-            points1.leftEdgeStart,
-            points1.leftEdgeEnd
+            points1.rightEdgeStart,
+            points1.rightEdgeEnd
           );
           let rightJoin = mathUtil.getJoinLinePoint(
             curveRoad.points[curveRoad.points.length - 1],
@@ -1061,7 +1062,7 @@ export default class CurveRoadService extends RoadService {
             let points1 = mathUtil.RectangleVertex(
               curveRoad.points[i - 1],
               curveRoad.points[i],
-              newWidth + midDivideWidth / 2
+              (newWidth + midDivideWidth / 2) * coordinate.ratio
             );
             line1 = mathUtil.createLine1(
               points1.rightEdgeStart,
@@ -1070,7 +1071,7 @@ export default class CurveRoadService extends RoadService {
             let points2 = mathUtil.RectangleVertex(
               curveRoad.points[i],
               curveRoad.points[i + 1],
-              newWidth + midDivideWidth / 2
+              (newWidth + midDivideWidth / 2) * coordinate.ratio
             );
             line2 = mathUtil.createLine1(
               points2.rightEdgeStart,

+ 67 - 19
src/graphic/Util/MathUtil.js

@@ -1456,21 +1456,32 @@ export default class MathUtil {
     var p1 = curve.controls[0];
     var p2 = curve.controls[1];
     var p3 = curve.end;
-    var target = position
+    var target = position;
 
-// 参数化方式在曲线上取一系列的点
+    // 参数化方式在曲线上取一系列的点
     var pointsOnCurve = [];
     for (var t = 0; t <= 1; t += 0.01) {
-      var x = Math.pow(1 - t, 3) * p0.x + 3 * Math.pow(1 - t, 2) * t * p1.x + 3 * (1 - t) * Math.pow(t, 2) * p2.x + Math.pow(t, 3) * p3.x;
-      var y = Math.pow(1 - t, 3) * p0.y + 3 * Math.pow(1 - t, 2) * t * p1.y + 3 * (1 - t) * Math.pow(t, 2) * p2.y + Math.pow(t, 3) * p3.y;
+      var x =
+        Math.pow(1 - t, 3) * p0.x +
+        3 * Math.pow(1 - t, 2) * t * p1.x +
+        3 * (1 - t) * Math.pow(t, 2) * p2.x +
+        Math.pow(t, 3) * p3.x;
+      var y =
+        Math.pow(1 - t, 3) * p0.y +
+        3 * Math.pow(1 - t, 2) * t * p1.y +
+        3 * (1 - t) * Math.pow(t, 2) * p2.y +
+        Math.pow(t, 3) * p3.y;
       pointsOnCurve.push({ x: x, y: y });
     }
 
-// 计算每个点与目标点的距离
+    // 计算每个点与目标点的距离
     var shortestDistance = Number.MAX_VALUE;
     var closestPoint;
     for (var i = 0; i < pointsOnCurve.length; i++) {
-      var distance = Math.sqrt(Math.pow(pointsOnCurve[i].x - target.x, 2) + Math.pow(pointsOnCurve[i].y - target.y, 2));
+      var distance = Math.sqrt(
+        Math.pow(pointsOnCurve[i].x - target.x, 2) +
+          Math.pow(pointsOnCurve[i].y - target.y, 2)
+      );
       if (distance < shortestDistance) {
         shortestDistance = distance;
         closestPoint = pointsOnCurve[i];
@@ -1479,13 +1490,13 @@ export default class MathUtil {
     return {
       position: closestPoint,
       distance: shortestDistance,
-    }
+    };
 
     console.log("最短距离:", shortestDistance);
     console.log("最近点:", closestPoint);
 
     const { x: offsetX, y: offsetY } = position;
-    let results = []
+    let results = [];
     // 用 x 求出对应的 t,用 t 求相应位置的 y,再比较得出的 y 与 offsetY 之间的差值
     const tsx = this.getThreeBezierT(
       curve.start.x,
@@ -1494,7 +1505,7 @@ export default class MathUtil {
       curve.end.x,
       offsetX
     );
-    console.log(tsx)
+    console.log(tsx);
     for (let x = 0; x < 3; x++) {
       if (tsx[x] <= 1 && tsx[x] >= 0) {
         const point = this.getThreeBezierPoint(
@@ -1505,10 +1516,10 @@ export default class MathUtil {
           curve.end
         );
         // if (Math.abs(point.y - offsetY) < rang) {
-          results.push({
-            position: point,
-            distance: this.getDistance(point, position),
-          });
+        results.push({
+          position: point,
+          distance: this.getDistance(point, position),
+        });
         // }
       }
     }
@@ -1530,16 +1541,16 @@ export default class MathUtil {
           curve.end
         );
         // if (Math.abs(point.x - offsetX) < rang) {
-          results.push({
-            position: point,
-            distance: this.getDistance(point, position),
-          });
+        results.push({
+          position: point,
+          distance: this.getDistance(point, position),
+        });
         // }
       }
     }
 
-    console.log(results)
-    return results.sort((a, b) => a.distance - b.distance)[0]
+    console.log(results);
+    return results.sort((a, b) => a.distance - b.distance)[0];
   }
 
   // 二次曲线
@@ -1637,6 +1648,43 @@ export default class MathUtil {
     }
   }
 
+  getCurvesIndexForCurvesPoints(position, points) {
+    let minDis = null;
+    let minDisToPoint = null;
+    let minPointIndex = -1;
+    let index = -1;
+    for (let i = 0; i < points.length - 1; ++i) {
+      const line = this.createLine1(points[i], points[i + 1]);
+      const join = this.getJoinLinePoint(position, line);
+      const dis = this.getDistance(position, join);
+      if (this.isContainForSegment(join, points[i], points[i + 1])) {
+        if (minDis == null || minDis > dis) {
+          minDis = dis;
+          index = i;
+        }
+      }
+      if (minDisToPoint == null) {
+        minDisToPoint = mathUtil.getDistance(position, points[i]);
+        minPointIndex = i;
+      } else if (minDisToPoint > mathUtil.getDistance(position, points[i])) {
+        minDisToPoint = mathUtil.getDistance(position, points[i]);
+        minPointIndex = i;
+      }
+    }
+    if ((index = -1)) {
+      if (
+        minDisToPoint >
+        mathUtil.getDistance(position, points[points.length - 1])
+      ) {
+        return points.length - 2;
+      } else {
+        return minPointIndex;
+      }
+    } else {
+      return index;
+    }
+  }
+
   // //获取一组点的偏移
   // getOffset(points, leftWidth, rightWidth, dir) {
   //   //斜边长度d已知,角度angle已知