From f94b1436d7a28c8e28d010b2cb657ab7c064e353 Mon Sep 17 00:00:00 2001
From: 826220679@qq.com <826220679@qq.com>
Date: 星期日, 28 十二月 2025 20:36:38 +0800
Subject: [PATCH] 修改了导航预览

---
 src/lujing/YixinglujingNoObstacle.java |  217 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 205 insertions(+), 12 deletions(-)

diff --git a/src/lujing/YixinglujingNoObstacle.java b/src/lujing/YixinglujingNoObstacle.java
index 31e87d5..9cee24a 100644
--- a/src/lujing/YixinglujingNoObstacle.java
+++ b/src/lujing/YixinglujingNoObstacle.java
@@ -90,10 +90,14 @@
             boolean endInside = isPointInPolygon(s.end, polygon);
             boolean intersects = segmentIntersectsBoundary(s.start, s.end, polygon);
             if (!s.isMowing) {
-                // 闈炰綔涓氭缁熶竴鏇挎崲涓烘部杈圭晫璺緞
-                List<Point> path = getBoundaryPathWithSnap(s.start, s.end, polygon);
-                for (int i = 0; i < path.size() - 1; i++) {
-                    sanitized.add(new PathSegment(path.get(i), path.get(i+1), false));
+                // 闈炰綔涓氭锛氳嫢AB鐩寸嚎瀹夊叏锛堜笉璺ㄨ秺杈圭晫涓斾腑鐐瑰湪鍐咃級锛屼繚鐣欑洿绾匡紱鍚﹀垯娌胯竟鐣屾浛鎹�
+                if (isSegmentSafe(s.start, s.end, polygon)) {
+                    sanitized.add(s);
+                } else {
+                    List<Point> path = getBoundaryPathWithSnap(s.start, s.end, polygon);
+                    for (int i = 0; i < path.size() - 1; i++) {
+                        sanitized.add(new PathSegment(path.get(i), path.get(i+1), false));
+                    }
                 }
             } else {
                 if (!startInside || !endInside || intersects) {
@@ -240,9 +244,9 @@
             }
 
             PathSegment s = lineSegmentsInRow.get(idxInRow);
-            // 棣栨杩炴帴鎴栬法鍖鸿繛鎺ュ潎寮哄埗娌胯竟鐣岋紝閬垮厤绌胯秺鍑归櫡鍖�
+            // 棣栨鎴栬法鍖鸿繛鎺ワ細浣跨敤鏅鸿兘鎹㈣璺緞锛堜紭鍏堢洿绾挎渶鐭紝蹇呰鏃舵部杈圭晫锛�
             if (Math.hypot(currentPos.x - s.start.x, currentPos.y - s.start.y) > 0.01) {
-                addBoundaryConnection(segments, currentPos, s.start, polygon);
+                addSafeConnection(segments, currentPos, s.start, polygon);
                 firstSegmentConnected = true;
             }
             segments.add(s);
@@ -369,14 +373,139 @@
     }
 
     private static void addSafeConnection(List<PathSegment> segments, Point start, Point end, List<Point> polygon) {
-        if (!FORCE_BOUNDARY_TRAVEL && isSegmentSafe(start, end, polygon)) {
-            segments.add(new PathSegment(start, end, false));
-            return;
+        // 浣跨敤鏂扮殑鏅鸿兘鎹㈣璺緞绠楁硶
+        List<PathSegment> connectionPath = generateSmartConnectionPath(start, end, polygon);
+        segments.addAll(connectionPath);
+    }
+
+    /**
+     * 鏅鸿兘鎹㈣璺緞鐢熸垚锛氭牴鎹瓵B绾挎涓庡畨鍏ㄨ竟鐣孋鐨勪氦鐐癸紝鐢熸垚娣峰悎璺緞
+     * 閫昏緫锛�
+     * 1. 濡傛灉AB绾挎涓嶇┛瓒婅竟鐣岋紝鐩存帴杩斿洖AB鐩寸嚎
+     * 2. 濡傛灉AB绾挎绌胯秺杈圭晫锛屾壘鍒版墍鏈変氦鐐癸紙濡侱, F, G, H锛夛紝鐢熸垚璺緞锛�
+     *    A -> D -> (娌胯竟鐣孌鍒癋) -> F -> G -> (娌胯竟鐣孏鍒癏) -> H -> B
+     */
+    private static List<PathSegment> generateSmartConnectionPath(Point A, Point B, List<Point> polygon) {
+        List<PathSegment> result = new ArrayList<>();
+        
+        // 1. 妫�鏌B鐩寸嚎鏄惁瀹夊叏锛堜笉绌胯秺杈圭晫涓斾腑鐐瑰湪鍐呴儴锛夛紱涓庢槸鍚﹀己鍒舵部杈圭晫鏃犲叧
+        if (isSegmentSafe(A, B, polygon)) {
+            result.add(new PathSegment(A, B, false));
+            return result;
         }
-        List<Point> path = getBoundaryPathWithSnap(start, end, polygon);
-        for (int i = 0; i < path.size() - 1; i++) {
-            segments.add(new PathSegment(path.get(i), path.get(i+1), false));
+        
+        // 2. 鑾峰彇AB涓庤竟鐣岀殑鎵�鏈変氦鐐�
+        List<IntersectionPoint> intersections = getSegmentBoundaryIntersections(A, B, polygon);
+        
+        // 3. 濡傛灉娌℃湁浜ょ偣浣嗕笉瀹夊叏锛岃鏄庢暣鏉$嚎閮藉湪澶栭儴锛屽己鍒舵部杈圭晫
+        if (intersections.isEmpty()) {
+            List<Point> path = getBoundaryPathWithSnap(A, B, polygon);
+            for (int i = 0; i < path.size() - 1; i++) {
+                result.add(new PathSegment(path.get(i), path.get(i+1), false));
+            }
+            return result;
         }
+        
+        // 4. 鏍规嵁浜ょ偣鎴愬澶勭悊锛岃�冭檻A/B鏄惁鍦ㄥ唴渚�
+        Point currentPos = A;
+        boolean startInside = isPointInPolygon(A, polygon);
+        boolean endInside = isPointInPolygon(B, polygon);
+        
+        for (int i = 0; i < intersections.size(); i += 2) {
+            IntersectionPoint entry = intersections.get(i);
+            
+            // 浠庡綋鍓嶄綅缃埌绗竴涓氦鐐癸細璧风偣鍦ㄥ唴鈫掔洿绾匡紱璧风偣鍦ㄥ鈫掓部杈圭晫鍒颁氦鐐�
+            if (Math.hypot(currentPos.x - entry.point.x, currentPos.y - entry.point.y) > 1e-6) {
+                if (startInside) {
+                    result.add(new PathSegment(currentPos, entry.point, false));
+                } else {
+                    List<Point> pathToEntry = getBoundaryPathWithSnap(currentPos, entry.point, polygon);
+                    for (int j = 0; j < pathToEntry.size() - 1; j++) {
+                        result.add(new PathSegment(pathToEntry.get(j), pathToEntry.get(j+1), false));
+                    }
+                }
+            }
+            
+            // 妫�鏌ユ槸鍚︽湁閰嶅鐨勭寮�鐐�
+            if (i + 1 < intersections.size()) {
+                IntersectionPoint exit = intersections.get(i + 1);
+                
+                // 浠庤繘鍏ョ偣D鍒扮寮�鐐笷锛氭部杈圭晫琛岃蛋
+                List<Point> boundaryPath = getBoundaryPathBetweenPoints(
+                    entry.point, entry.edgeIndex,
+                    exit.point, exit.edgeIndex,
+                    polygon
+                );
+                
+                for (int j = 0; j < boundaryPath.size() - 1; j++) {
+                    result.add(new PathSegment(boundaryPath.get(j), boundaryPath.get(j+1), false));
+                }
+                
+                currentPos = exit.point;
+                // 涔嬪悗鐨勮捣鐐硅涓哄唴渚�
+                startInside = true;
+            } else {
+                // 濡傛灉娌℃湁閰嶅鐨勭寮�鐐癸紝璇存槑缁堢偣鍦ㄥ閮紝娌胯竟鐣屽埌B
+                List<Point> path = getBoundaryPathWithSnap(entry.point, B, polygon);
+                for (int j = 0; j < path.size() - 1; j++) {
+                    result.add(new PathSegment(path.get(j), path.get(j+1), false));
+                }
+                return result;
+            }
+        }
+        
+        // 5. 浠庢渶鍚庝竴涓寮�鐐瑰埌缁堢偣B锛氱洿绾挎锛堝湪鍐呴儴锛�
+        if (Math.hypot(currentPos.x - B.x, currentPos.y - B.y) > 1e-6) {
+            if (endInside) {
+                result.add(new PathSegment(currentPos, B, false));
+            } else {
+                List<Point> pathToB = getBoundaryPathWithSnap(currentPos, B, polygon);
+                for (int j = 0; j < pathToB.size() - 1; j++) {
+                    result.add(new PathSegment(pathToB.get(j), pathToB.get(j+1), false));
+                }
+            }
+        }
+        
+        return result;
+    }
+
+    /**
+     * 鍦ㄥ凡鐭ヨ竟鐣岃竟绱㈠紩鐨勬儏鍐典笅锛屾部杈圭晫鑾峰彇涓ょ偣涔嬮棿鐨勬渶鐭矾寰�
+     */
+    private static List<Point> getBoundaryPathBetweenPoints(
+        Point start, int startEdgeIdx, 
+        Point end, int endEdgeIdx, 
+        List<Point> polygon) {
+        
+        int n = polygon.size();
+        
+        // 濡傛灉鍦ㄥ悓涓�鏉¤竟涓婏紝鐩存帴杩炴帴
+        if (startEdgeIdx == endEdgeIdx) {
+            return Arrays.asList(start, end);
+        }
+        
+        // 姝e悜璺緞锛堥『杈癸級
+        List<Point> pathFwd = new ArrayList<>();
+        pathFwd.add(start);
+        int curr = startEdgeIdx;
+        while (curr != endEdgeIdx) {
+            pathFwd.add(polygon.get((curr + 1) % n));
+            curr = (curr + 1) % n;
+        }
+        pathFwd.add(end);
+
+        // 鍙嶅悜璺緞锛堥�嗚竟锛�
+        List<Point> pathRev = new ArrayList<>();
+        pathRev.add(start);
+        curr = startEdgeIdx;
+        while (curr != endEdgeIdx) {
+            pathRev.add(polygon.get(curr));
+            curr = (curr - 1 + n) % n;
+        }
+        pathRev.add(end);
+
+        // 杩斿洖鍑犱綍闀垮害鏇寸煭鐨勮矾寰�
+        return getPathLength(pathFwd) <= getPathLength(pathRev) ? pathFwd : pathRev;
     }
 
     // 寮哄埗娌胯竟鐣岀粫琛岀殑杩炴帴锛堜笉鍋氱洿绾垮畨鍏ㄥ垽鏂級锛岀敤鏉ュ湪鍚屼竴鎵弿琛岀殑澶氫釜浣滀笟娈典箣闂磋烦杞�
@@ -470,6 +599,70 @@
         return ccw(a, c, d) != ccw(b, c, d) && ccw(a, b, c) != ccw(a, b, d);
     }
 
+    // 鑾峰彇绾挎AB涓庤竟鐣屽杈瑰舰鐨勬墍鏈変氦鐐癸紝鎸夌収璺濈A鐐圭殑椤哄簭鎺掑簭
+    private static class IntersectionPoint {
+        Point point;           // 浜ょ偣鍧愭爣
+        int edgeIndex;        // 浜ょ偣鎵�鍦ㄧ殑杈圭晫杈圭储寮�
+        double distFromStart; // 璺濈璧风偣A鐨勮窛绂�
+        boolean isEntry;      // true琛ㄧず杩涘叆杈圭晫锛宖alse琛ㄧず绂诲紑杈圭晫
+        
+        IntersectionPoint(Point p, int idx, double dist, boolean entry) {
+            this.point = p;
+            this.edgeIndex = idx;
+            this.distFromStart = dist;
+            this.isEntry = entry;
+        }
+    }
+
+    // 璁$畻绾挎AB涓庡杈瑰舰杈圭晫鐨勬墍鏈変氦鐐�
+    private static List<IntersectionPoint> getSegmentBoundaryIntersections(Point A, Point B, List<Point> polygon) {
+        List<IntersectionPoint> intersections = new ArrayList<>();
+        for (int i = 0; i < polygon.size(); i++) {
+            Point C = polygon.get(i);
+            Point D = polygon.get((i + 1) % polygon.size());
+            Point intersection = getLineSegmentIntersection(A, B, C, D);
+            if (intersection != null) {
+                if (isSamePoint(intersection, A) || isSamePoint(intersection, B)) continue;
+                double dist = Math.hypot(intersection.x - A.x, intersection.y - A.y);
+                intersections.add(new IntersectionPoint(intersection, i, dist, false));
+            }
+        }
+        // 璺濈鎺掑簭
+        Collections.sort(intersections, (i1, i2) -> Double.compare(i1.distFromStart, i2.distFromStart));
+        // 鍘婚噸杩戜技鐩稿悓浜ょ偣锛堥《鐐瑰弻浜わ級
+        List<IntersectionPoint> deduped = new ArrayList<>();
+        for (IntersectionPoint ip : intersections) {
+            boolean dup = false;
+            for (IntersectionPoint kept : deduped) {
+                if (Math.abs(ip.point.x - kept.point.x) < 1e-6 && Math.abs(ip.point.y - kept.point.y) < 1e-6) { dup = true; break; }
+            }
+            if (!dup) deduped.add(ip);
+        }
+        return deduped;
+    }
+
+    // 璁$畻涓ゆ潯绾挎鐨勪氦鐐癸紙濡傛灉瀛樺湪锛�
+    private static Point getLineSegmentIntersection(Point p1, Point p2, Point p3, Point p4) {
+        double x1 = p1.x, y1 = p1.y;
+        double x2 = p2.x, y2 = p2.y;
+        double x3 = p3.x, y3 = p3.y;
+        double x4 = p4.x, y4 = p4.y;
+        
+        double denom = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
+        if (Math.abs(denom) < 1e-10) return null; // 骞宠鎴栭噸鍚�
+        
+        double t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / denom;
+        double u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / denom;
+        
+        // 妫�鏌ヤ氦鐐规槸鍚﹀湪涓ゆ潯绾挎涓婏紙浣跨敤鐣ュ井瀹芥澗鐨勮寖鍥翠互澶勭悊娴偣璇樊锛�
+        double epsilon = 1e-6;
+        if (t >= -epsilon && t <= 1 + epsilon && u >= -epsilon && u <= 1 + epsilon) {
+            return new Point(x1 + t * (x2 - x1), y1 + t * (y2 - y1));
+        }
+        
+        return null;
+    }
+
     private static boolean ccw(Point a, Point b, Point c) {
         return (c.y - a.y) * (b.x - a.x) > (b.y - a.y) * (c.x - a.x);
     }

--
Gitblit v1.10.0