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 |  384 +++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 375 insertions(+), 9 deletions(-)

diff --git a/src/lujing/YixinglujingNoObstacle.java b/src/lujing/YixinglujingNoObstacle.java
index bdda587..9cee24a 100644
--- a/src/lujing/YixinglujingNoObstacle.java
+++ b/src/lujing/YixinglujingNoObstacle.java
@@ -7,6 +7,9 @@
  * 淇锛氳В鍐冲嚬澶氳竟褰㈡壂鎻忕嚎璺ㄨ秺杈圭晫鐨勯棶棰橈紝浼樺寲璺緞瀵归綈
  */
 public class YixinglujingNoObstacle {
+    // 寮�鍏筹細鏄惁寮哄埗鎵�鏈夐潪浣滀笟杩炴帴娌垮畨鍏ㄨ竟鐣岃璧帮紙閬垮厤浠讳綍鍐呭尯鐩寸嚎璺ㄨ秺锛�
+    // 鏀逛负鍙姩鎬佽缃紝鑷姩渚濇嵁鍦板潡褰㈢姸鍚敤
+    private static boolean FORCE_BOUNDARY_TRAVEL = true;
     // 鐢ㄦ硶璇存槑锛堟棤闅滅鐗╄矾寰勮鍒掞級锛�
     // - 鏂规硶鐢ㄩ�旓細鏍规嵁鍦板潡杈圭晫銆佸壊鑽夊搴︿笌瀹夊叏杈硅窛锛岀敓鎴愯鐩栧叏鍖哄煙鐨勫壊鑽夎矾寰勩��
     // - 鍙傛暟锛�
@@ -37,6 +40,9 @@
 
         // 3. 纭畾鏈�浼樹綔涓氳搴�
         double bestAngle = findOptimalAngle(boundary);
+
+        // 3.1 鑷姩鍒ゆ柇鏄惁闇�瑕佸己鍒舵部杈圭晫鏃呰锛堟娴嬪嚬閮�/澶氭鎵弿琛岋級
+        FORCE_BOUNDARY_TRAVEL = shouldForceBoundaryTravel(boundary, mowWidth, bestAngle);
         
         // 4. 鑾峰彇棣栦釜浣滀笟鐐癸紝鐢ㄤ簬瀵归綈鍥磋竟璧风偣
         Point firstScanStart = getFirstScanPoint(boundary, mowWidth, bestAngle);
@@ -59,7 +65,10 @@
         
         finalPath.addAll(scanPath);
 
-        // 8. 鏍煎紡鍖栧潗鏍囷細淇濈暀涓や綅灏忔暟
+        // 8. 鏈�缁堝畨鍏ㄥ噣鍖栵細纭繚鎵�鏈夋鍦ㄥ唴缂╄竟鐣屼笂鎴栧唴閮紙鑷姩璐磋竟闃堝�硷級
+        sanitizePath(finalPath, boundary, mowWidth, safeMargin);
+
+        // 9. 鏍煎紡鍖栧潗鏍囷細淇濈暀涓や綅灏忔暟
         for (PathSegment segment : finalPath) {
             segment.start.x = Math.round(segment.start.x * 100.0) / 100.0;
             segment.start.y = Math.round(segment.start.y * 100.0) / 100.0;
@@ -70,6 +79,112 @@
         return finalPath;
     }
 
+    // 瀵规墍鏈夎矾寰勬杩涜瀹夊叏鍑�鍖栵細
+    // - 闈炰綔涓氭锛氱粺涓�娌胯竟鐣岃矾寰勬浛鎹�
+    // - 浣滀笟娈碉細鑻ョ鐐瑰湪澶栨垨娈典笌杈圭晫鐩镐氦锛屽惛闄勭鐐瑰埌杈圭晫骞跺悜鍐呬晶鍋忕Щ epsilon
+    private static void sanitizePath(List<PathSegment> segments, List<Point> polygon, double width, double margin) {
+        double epsilon = computeAutoInnerOffset(polygon, width, margin);
+        List<PathSegment> sanitized = new ArrayList<>();
+        for (PathSegment s : segments) {
+            boolean startInside = isPointInPolygon(s.start, polygon);
+            boolean endInside = isPointInPolygon(s.end, polygon);
+            boolean intersects = segmentIntersectsBoundary(s.start, s.end, polygon);
+            if (!s.isMowing) {
+                // 闈炰綔涓氭锛氳嫢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) {
+                    SnapResult s1 = snapToBoundary(s.start, polygon);
+                    SnapResult s2 = snapToBoundary(s.end, polygon);
+                    Point p1 = pushInsideOnEdge(s1, polygon, epsilon);
+                    Point p2 = pushInsideOnEdge(s2, polygon, epsilon);
+                    sanitized.add(new PathSegment(p1, p2, true));
+                } else {
+                    sanitized.add(s);
+                }
+            }
+        }
+        segments.clear();
+        segments.addAll(sanitized);
+    }
+
+    private static boolean segmentIntersectsBoundary(Point a, Point b, List<Point> polygon) {
+        for (int i = 0; i < polygon.size(); i++) {
+            Point c = polygon.get(i);
+            Point d = polygon.get((i + 1) % polygon.size());
+            // 蹇界暐鍏变韩绔偣鐨勭浉浜�
+            if (isSamePoint(a, c) || isSamePoint(a, d) || isSamePoint(b, c) || isSamePoint(b, d)) continue;
+            if (segmentsIntersect(a, b, c, d)) return true;
+        }
+        return false;
+    }
+
+    // 灏嗚竟鐣屼笂鐨勬姇褰辩偣鍚戝唴渚у亸绉� epsilon
+    private static Point pushInsideOnEdge(SnapResult sr, List<Point> poly, double epsilon) {
+        int i = sr.edgeIndex;
+        Point s = poly.get(i);
+        Point e = poly.get((i + 1) % poly.size());
+        double dx = e.x - s.x, dy = e.y - s.y;
+        double len = Math.hypot(dx, dy);
+        if (len < 1e-6) return sr.onEdge;
+        // 瀵逛簬閫嗘椂閽堬紙CCW锛夊杈瑰舰锛屽乏杞硶鍚戦噺 (-dy, dx) 鎸囧悜鍐呬晶
+        double nx = -dy / len, ny = dx / len;
+        return new Point(sr.onEdge.x + nx * epsilon, sr.onEdge.y + ny * epsilon);
+    }
+
+    // 鑷姩璁$畻璐磋竟鍐呭亸绉婚槇鍊� epsilon锛氭牴鎹湴鍧楀昂搴︺�佹渶鐭竟銆佸壊鑽夊搴︿笌瀹夊叏杈硅窛缁煎悎浼扮畻
+    private static double computeAutoInnerOffset(List<Point> polygon, double width, double margin) {
+        double minEdge = Double.MAX_VALUE;
+        double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
+        double maxX = -Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
+        for (int i = 0; i < polygon.size(); i++) {
+            Point a = polygon.get(i);
+            Point b = polygon.get((i + 1) % polygon.size());
+            minEdge = Math.min(minEdge, Math.hypot(a.x - b.x, a.y - b.y));
+            minX = Math.min(minX, a.x); minY = Math.min(minY, a.y);
+            maxX = Math.max(maxX, a.x); maxY = Math.max(maxY, a.y);
+        }
+        double diag = Math.hypot(maxX - minX, maxY - minY);
+        // 鍩虹閲忥細鏁板�肩ǔ瀹氶渶瑕佺殑鏈�灏忓唴鍋忕Щ锛堝彇鍓插箙鐨�1%涓庡瑙掔嚎鐨�0.2%涔嬮棿鐨勮緝澶у�硷級
+        double base = Math.max(width * 0.01, diag * 0.002);
+        // 涓婇檺锛氫笉瓒呰繃瀹夊叏杈硅窛鐨�20%涓庡壊骞呯殑10%
+        double upper = Math.min(margin * 0.2, width * 0.1);
+        // 鍙楄竟闀跨害鏉燂細涓嶈秴杩囨渶鐭竟鐨�2%
+        double edgeLimit = minEdge * 0.02;
+        double eps = Math.min(upper, Math.max(base, edgeLimit * 0.5));
+        // 涓嬮檺/涓婇檺鏈�缁堥挸浣嶏細3mm 鍒� 5cm
+        eps = Math.max(0.003, Math.min(eps, 0.05));
+        return eps;
+    }
+
+    // 鏍规嵁鎵弿琛岀殑浜ょ偣鏁伴噺鏉ュ垽鏂槸鍚﹀瓨鍦ㄢ�滃娈佃鈥濓紙>=2娈碉級锛屾湁鍑归儴鎴栫獎閫氶亾鏃跺惎鐢ㄥ己鍒舵部杈圭晫鏃呰
+    private static boolean shouldForceBoundaryTravel(List<Point> polygon, double width, double angle) {
+        List<Point> rotatedPoly = new ArrayList<>();
+        for (Point p : polygon) rotatedPoly.add(rotatePoint(p, -angle));
+        double minY = Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
+        for (Point p : rotatedPoly) { minY = Math.min(minY, p.y); maxY = Math.max(maxY, p.y); }
+
+        int multiSegmentRows = 0;
+        int totalRows = 0;
+        for (double y = minY + width/2; y <= maxY - width/2; y += width) {
+            List<Double> xIntersections = getXIntersections(rotatedPoly, y);
+            if (xIntersections.size() < 2) continue;
+            totalRows++;
+            if (xIntersections.size() >= 4) multiSegmentRows++; // 鍚屼竴琛屽嚭鐜颁袱涓強浠ヤ笂浣滀笟娈�
+        }
+        // 鍙鍑虹幇杩団�滃娈佃鈥濓紝灏卞己鍒舵部杈圭晫鏃呰锛涗篃鍙寜姣斾緥闃堝�艰Е鍙戯紙渚嬪 >=10%锛�
+        if (multiSegmentRows > 0) return true;
+        double ratio = totalRows == 0 ? 0.0 : (double) multiSegmentRows / (double) totalRows;
+        return ratio >= 0.1; // 鍏滃簳闃堝��
+    }
+
     private static List<PathSegment> generateGlobalScanPath(List<Point> polygon, double width, double angle, Point currentPos) {
         // 鍏堝皾璇曞皢鍑归櫡澶勮涓轰袱涓嫭绔嬪尯鍩燂紝鍒嗕袱娆℃壂鎻忥紝閬垮厤璺ㄥ尯鐩寸嚎杩炴帴
         List<PathSegment> all = new ArrayList<>();
@@ -129,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);
@@ -258,24 +373,211 @@
     }
 
     private static void addSafeConnection(List<PathSegment> segments, Point start, Point end, List<Point> polygon) {
-        if (isSegmentSafe(start, end, polygon)) {
-            segments.add(new PathSegment(start, end, false));
-        } else {
-            List<Point> path = getBoundaryPath(start, end, polygon);
+        // 浣跨敤鏂扮殑鏅鸿兘鎹㈣璺緞绠楁硶
+        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;
+        }
+        
+        // 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++) {
-                segments.add(new PathSegment(path.get(i), path.get(i+1), false));
+                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;
     }
 
     // 寮哄埗娌胯竟鐣岀粫琛岀殑杩炴帴锛堜笉鍋氱洿绾垮畨鍏ㄥ垽鏂級锛岀敤鏉ュ湪鍚屼竴鎵弿琛岀殑澶氫釜浣滀笟娈典箣闂磋烦杞�
     private static void addBoundaryConnection(List<PathSegment> segments, Point start, Point end, List<Point> polygon) {
-        List<Point> path = getBoundaryPath(start, end, polygon);
+        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));
         }
     }
 
+    // 灏嗕换鎰忎袱鐐归�氳繃鈥滃惛闄勫埌杈圭晫鈥濆悗娌胯竟鐣屾渶鐭矾寰勮繛鎺�
+    private static List<Point> getBoundaryPathWithSnap(Point start, Point end, List<Point> polygon) {
+        SnapResult s1 = snapToBoundary(start, polygon);
+        SnapResult s2 = snapToBoundary(end, polygon);
+        int n = polygon.size();
+
+        // 鍓嶅悜璺緞锛堥『杈癸級
+        List<Point> pathFwd = new ArrayList<>();
+        pathFwd.add(start);
+        pathFwd.add(s1.onEdge);
+        int curr = s1.edgeIndex;
+        while (curr != s2.edgeIndex) {
+            pathFwd.add(polygon.get((curr + 1) % n));
+            curr = (curr + 1) % n;
+        }
+        pathFwd.add(s2.onEdge);
+        pathFwd.add(end);
+
+        // 鍙嶅悜璺緞锛堥�嗚竟锛�
+        List<Point> pathRev = new ArrayList<>();
+        pathRev.add(start);
+        pathRev.add(s1.onEdge);
+        curr = s1.edgeIndex;
+        while (curr != s2.edgeIndex) {
+            pathRev.add(polygon.get(curr));
+            curr = (curr - 1 + n) % n;
+        }
+        pathRev.add(s2.onEdge);
+        pathRev.add(end);
+
+        return getPathLength(pathFwd) < getPathLength(pathRev) ? pathFwd : pathRev;
+    }
+
+    private static class SnapResult {
+        Point onEdge;
+        int edgeIndex;
+        SnapResult(Point p, int idx) { this.onEdge = p; this.edgeIndex = idx; }
+    }
+
+    // 璁$畻鐐瑰埌杈圭晫鏈�杩戠殑鎶曞奖鐐逛互鍙婃墍鍦ㄨ竟绱㈠紩
+    private static SnapResult snapToBoundary(Point p, List<Point> poly) {
+        double minD = Double.MAX_VALUE;
+        Point bestProj = p;
+        int bestIdx = -1;
+        for (int i = 0; i < poly.size(); i++) {
+            Point s = poly.get(i);
+            Point e = poly.get((i + 1) % poly.size());
+            double l2 = (s.x - e.x)*(s.x - e.x) + (s.y - e.y)*(s.y - e.y);
+            if (l2 == 0) {
+                double d = Math.hypot(p.x - s.x, p.y - s.y);
+                if (d < minD) { minD = d; bestProj = s; bestIdx = i; }
+                continue;
+            }
+            double t = ((p.x - s.x) * (e.x - s.x) + (p.y - s.y) * (e.y - s.y)) / l2;
+            t = Math.max(0, Math.min(1, t));
+            Point proj = new Point(s.x + t * (e.x - s.x), s.y + t * (e.y - s.y));
+            double d = Math.hypot(p.x - proj.x, p.y - proj.y);
+            if (d < minD) { minD = d; bestProj = proj; bestIdx = i; }
+        }
+        return new SnapResult(bestProj, bestIdx == -1 ? 0 : bestIdx);
+    }
+
     private static boolean isSegmentSafe(Point p1, Point p2, List<Point> polygon) {
         Point mid = new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
         if (!isPointInPolygon(mid, polygon)) return false;
@@ -297,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