826220679@qq.com
6 小时以前 d58d5e1baf9390d4119f48c4af3fbcf308da10a3
src/lujing/YixinglujingNoObstacle.java
@@ -71,6 +71,20 @@
    }
    private static List<PathSegment> generateGlobalScanPath(List<Point> polygon, double width, double angle, Point currentPos) {
        // 先尝试将凹陷处视为两个独立区域,分两次扫描,避免跨区直线连接
        List<PathSegment> all = new ArrayList<>();
        // 第一次扫描:优先处理左侧区域(groupIndex=0)
        List<PathSegment> leftScan = generateScanPathForSide(polygon, width, angle, currentPos, 0);
        all.addAll(leftScan);
        Point posAfterLeft = leftScan.isEmpty() ? currentPos : leftScan.get(leftScan.size() - 1).end;
        // 第二次扫描:处理右侧区域(groupIndex=1),从左侧结束点沿边界到右侧首段
        List<PathSegment> rightScan = generateScanPathForSide(polygon, width, angle, posAfterLeft, 1);
        all.addAll(rightScan);
        return all;
    }
    // 仅扫描指定侧(同一条扫描线的第 groupIndex 段),用于将“耳朵”视为独立区域
    private static List<PathSegment> generateScanPathForSide(List<Point> polygon, double width, double angle, Point currentPos, int sideIndex) {
        List<PathSegment> segments = new ArrayList<>();
        List<Point> rotatedPoly = new ArrayList<>();
        for (Point p : polygon) rotatedPoly.add(rotatePoint(p, -angle));
@@ -82,37 +96,46 @@
        }
        boolean leftToRight = true;
        // 步长 y 从最小到最大扫描
        boolean firstSegmentConnected = false;
        for (double y = minY + width/2; y <= maxY - width/2; y += width) {
            List<Double> xIntersections = getXIntersections(rotatedPoly, y);
            if (xIntersections.size() < 2) continue;
            Collections.sort(xIntersections);
            // 处理凹多边形:每两个点组成一个有效作业段
            // 构建本行的作业段(左到右)和组索引
            List<PathSegment> lineSegmentsInRow = new ArrayList<>();
            for (int i = 0; i < xIntersections.size() - 1; i += 2) {
            List<Integer> groupIndices = new ArrayList<>();
            for (int i = 0, g = 0; i < xIntersections.size() - 1; i += 2, g++) {
                Point pS = rotatePoint(new Point(xIntersections.get(i), y), angle);
                Point pE = rotatePoint(new Point(xIntersections.get(i + 1), y), angle);
                lineSegmentsInRow.add(new PathSegment(pS, pE, true));
                groupIndices.add(g);
            }
            // 根据当前S型方向排序作业段
            if (!leftToRight) {
                Collections.reverse(lineSegmentsInRow);
                Collections.reverse(groupIndices);
                for (PathSegment s : lineSegmentsInRow) {
                    Point temp = s.start; s.start = s.end; s.end = temp;
                }
            }
            // 将作业段连接到总路径
            for (PathSegment s : lineSegmentsInRow) {
                if (Math.hypot(currentPos.x - s.start.x, currentPos.y - s.start.y) > 0.01) {
                    // 如果间距大于1cm,添加空走路径
                    addSafeConnection(segments, currentPos, s.start, polygon);
                }
                segments.add(s);
                currentPos = s.end;
            int idxInRow = groupIndices.indexOf(sideIndex);
            if (idxInRow == -1) {
                // 本行不包含该侧的作业段,跳过
                leftToRight = !leftToRight;
                continue;
            }
            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);
                firstSegmentConnected = true;
            }
            segments.add(s);
            currentPos = s.end;
            leftToRight = !leftToRight;
        }
        return segments;
@@ -245,6 +268,14 @@
        }
    }
    // 强制沿边界绕行的连接(不做直线安全判断),用来在同一扫描行的多个作业段之间跳转
    private static void addBoundaryConnection(List<PathSegment> segments, Point start, Point end, List<Point> polygon) {
        List<Point> path = getBoundaryPath(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 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;