From f0d6cefec73492c29d8323e66fb92ee6bbb60cd2 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期五, 26 十二月 2025 18:58:23 +0800
Subject: [PATCH] 优化了预览时候视图居中的逻辑
---
src/lujing/YixinglujingHaveObstacel.java | 658 ++++++++++++++++++++++++++++++++++-------------------------
1 files changed, 374 insertions(+), 284 deletions(-)
diff --git a/src/lujing/YixinglujingHaveObstacel.java b/src/lujing/YixinglujingHaveObstacel.java
index ff0736a..d9dceb3 100644
--- a/src/lujing/YixinglujingHaveObstacel.java
+++ b/src/lujing/YixinglujingHaveObstacel.java
@@ -1,19 +1,20 @@
package lujing;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
+import java.util.regex.*;
/**
- * 寮傚舰鑽夊湴璺緞瑙勫垝 - 閬块殰澧炲己鐗� V8.0
- * 淇璇存槑锛�
- * 1. 淇浜嗗湴鍧楀唴缂╁拰闅滅鐗╁鎵╃殑姝h礋閫昏緫銆�
- * 2. 浼樺寲浜嗗杈瑰舰鍋忕Щ绠楁硶锛岀‘淇濋�嗘椂閽堢偣搴忎笅姝e�煎唴缂╋紝璐熷�煎鎵┿��
- * 3. 澧炲己浜嗛殰纰嶇墿瑙f瀽鐨勫仴澹�с��
+ * 寮傚舰鑽夊湴璺緞瑙勫垝 - 鍑瑰杈瑰舰淇鐗� V12.0
+ * 淇敼璇存槑锛�
+ * 1. 鎸夌収鐢ㄦ埛瑕佹眰锛屽厛鐢熸垚鏃犻殰纰嶇墿鐨勫畬鏁磋矾寰勶紙鍥磋竟+鎵弿+杩炴帴锛夈��
+ * 2. 瀵瑰畬鏁磋矾寰勮繘琛岄殰纰嶇墿瑁佸壀銆�
+ * 3. 瀵硅鍓骇鐢熺殑鏂偣锛屽皾璇曟部闅滅鐗╄竟鐣岃繘琛岃繛鎺ャ��
*/
public class YixinglujingHaveObstacel {
+ private static final double EPS = 1e-8;
+ private static final double MIN_SEG_LEN = 0.02; // 蹇界暐灏忎簬2cm鐨勭绾�
+
public static List<PathSegment> planPath(String coordinates, String obstaclesStr, String widthStr, String marginStr) {
List<Point> rawPoints = parseCoordinates(coordinates);
if (rawPoints.size() < 3) return new ArrayList<>();
@@ -21,276 +22,173 @@
double mowWidth = Double.parseDouble(widthStr);
double safeMargin = Double.parseDouble(marginStr);
- // 1. 棰勫鐞嗗湴鍧楋紙纭繚閫嗘椂閽堥『搴忥級
- ensureCounterClockwise(rawPoints);
+ // 1. 缁熶竴澶氳竟褰㈡柟鍚戜负閫嗘椂閽� (CCW)
+ ensureCCW(rawPoints);
- // 銆愭牳蹇冧慨澶嶃�戯細瀵逛簬閫嗘椂閽堝杈瑰舰锛屾鏁版槸鍚戝唴鍋忕Щ锛圛nset锛�
- List<Point> boundary = getOffsetPolygon(rawPoints, safeMargin);
- if (boundary.size() < 3) return new ArrayList<>();
+ // 2. 鐢熸垚浣滀笟鍐呯缉杈圭晫
+ List<Point> mowingBoundary = getOffsetPolygon(rawPoints, safeMargin);
+ if (mowingBoundary.size() < 3) return new ArrayList<>();
- // 2. 纭畾鏈�浼樿搴﹀苟瑙勫垝鍩虹璺緞
- double bestAngle = findOptimalAngle(boundary);
- Point firstScanStart = getFirstScanPoint(boundary, mowWidth, bestAngle);
- List<Point> alignedBoundary = alignBoundaryStart(boundary, firstScanStart);
-
- List<PathSegment> baseLines = new ArrayList<>();
- // 绗竴闃舵锛氬洿杈硅矾寰�
- for (int i = 0; i < alignedBoundary.size(); i++) {
- baseLines.add(new PathSegment(alignedBoundary.get(i), alignedBoundary.get((i + 1) % alignedBoundary.size()), true));
- }
- // 绗簩闃舵锛氱敓鎴愬唴閮ㄦ壂鎻忚矾寰�
- Point lastEdgePos = alignedBoundary.get(0);
- baseLines.addAll(generateGlobalScanPath(boundary, mowWidth, bestAngle, lastEdgePos));
-
- // 3. 澶勭悊闅滅鐗╋細瑙f瀽骞舵墽琛屻�愬鎵┿��
- // 銆愭牳蹇冧慨澶嶃�戯細瀵逛簬閫嗘椂閽堥殰纰嶇墿锛岃礋鏁版槸鍚戝鍋忕Щ锛圤utset锛�
+ // 3. 瑙f瀽骞跺鎵╅殰纰嶇墿
List<Obstacle> obstacles = parseObstacles(obstaclesStr, safeMargin);
- // 4. 璺緞瑁佸壀涓庝紭鍖栬繛鎺�
- return optimizeAndClipPath(baseLines, obstacles);
- }
+ // 4. 鐢熸垚鍏ㄨ鐩栬矾寰勶紙涓嶈�冭檻闅滅鐗╋級
+ List<PathSegment> fullPath = generateFullPath(mowingBoundary, mowWidth);
- private static List<Obstacle> parseObstacles(String obsStr, double margin) {
- List<Obstacle> obstacles = new ArrayList<>();
- if (obsStr == null || obsStr.trim().isEmpty()) return obstacles;
-
- for (String group : obsStr.split("\\$")) {
- List<Point> pts = parseCoordinates(group);
- if (pts.isEmpty()) continue;
-
- if (pts.size() == 2) {
- // 鍦嗗舰闅滅鐗╋細绗竴涓偣蹇冿紝绗簩涓偣涓婁竴鐐癸紝鍗婂緞澧炲姞 margin
- double r = Math.hypot(pts.get(0).x - pts.get(1).x, pts.get(0).y - pts.get(1).y);
- obstacles.add(new CircleObstacle(pts.get(0), r + margin));
- } else if (pts.size() > 2) {
- // 澶氳竟褰㈤殰纰嶇墿锛氱‘淇濋�嗘椂閽堬紝鐒跺悗浣跨敤璐� margin 杩涜銆愬鎵┿��
- ensureCounterClockwise(pts);
- obstacles.add(new PolyObstacle(getOffsetPolygon(pts, -margin)));
- }
- }
- return obstacles;
+ // 5. 瑁佸壀骞惰繛鎺�
+ return processObstacles(fullPath, obstacles);
}
/**
- * 澶氳竟褰㈠亸绉荤畻娉曪細鍩轰簬瑙掑钩鍒嗙嚎鍋忕Щ
- * 鍦ㄩ�嗘椂閽堥『搴忎笅锛歰ffset > 0 涓哄唴缂╋紝offset < 0 涓哄鎵�
+ * 鐢熸垚鍏ㄨ鐩栬矾寰勶紙鍥磋竟 + 鎵弿 + 杩炴帴锛夛紝涓嶈�冭檻闅滅鐗�
*/
- private static List<Point> getOffsetPolygon(List<Point> points, double offset) {
- List<Point> result = new ArrayList<>();
- int n = points.size();
- for (int i = 0; i < n; i++) {
- Point p1 = points.get((i - 1 + n) % n);
- Point p2 = points.get(i);
- Point p3 = points.get((i + 1) % n);
-
- double v1x = p2.x - p1.x, v1y = p2.y - p1.y;
- double v2x = p3.x - p2.x, v2y = p3.y - p2.y;
- double l1 = Math.hypot(v1x, v1y), l2 = Math.hypot(v2x, v2y);
-
- if (l1 < 1e-6 || l2 < 1e-6) continue;
-
- // 鑾峰彇涓ゆ潯杈圭殑娉曞悜閲忥紙鍚戝乏鍋忕Щ锛�
- double n1x = -v1y / l1, n1y = v1x / l1;
- double n2x = -v2y / l2, n2y = v2x / l2;
-
- // 瑙掑钩鍒嗙嚎鍚戦噺
- double bx = n1x + n2x, by = n1y + n2y;
- double bl = Math.hypot(bx, by);
- if (bl < 1e-6) {
- bx = n1x; by = n1y;
- } else {
- bx /= bl; by /= bl;
- }
-
- // 璁$畻鍋忕Щ闀垮害淇绯绘暟锛�1/sin(theta/2)
- double cosHalf = n1x * bx + n1y * by;
- double d = offset / Math.max(cosHalf, 0.1); // 閬垮厤鍒嗘瘝杩囧皬瀵艰嚧鏃犵┓澶�
-
- // 闄愬埗鏈�澶т綅绉婚噺锛岄槻姝㈡瀬灏栬鐣稿彉
- d = Math.signum(offset) * Math.min(Math.abs(d), Math.abs(offset) * 5);
-
- result.add(new Point(p2.x + bx * d, p2.y + by * d));
- }
- return result;
- }
-
- private static List<PathSegment> optimizeAndClipPath(List<PathSegment> originalPath, List<Obstacle> obstacles) {
- List<PathSegment> result = new ArrayList<>();
- Point currentPos = null;
-
- for (PathSegment segment : originalPath) {
- List<PathSegment> clipped = new ArrayList<>();
- clipped.add(segment);
-
- // 鐢ㄦ瘡涓�涓殰纰嶇墿渚濇瑁佸壀
- for (Obstacle obs : obstacles) {
- List<PathSegment> nextIter = new ArrayList<>();
- for (PathSegment s : clipped) {
- nextIter.addAll(obs.clipSegment(s));
- }
- clipped = nextIter;
- }
-
- for (PathSegment s : clipped) {
- // 鍓旈櫎寰皬娈�
- if (Math.hypot(s.start.x - s.end.x, s.start.y - s.end.y) < 1e-4) continue;
-
- // 濡傛灉鏂版鐨勮捣鐐逛笌涓婃鐨勭粓鐐逛笉杩炶疮锛屾坊鍔犵┖璧帮紙闈炲壊鑽夛級璺緞
- if (currentPos != null && Math.hypot(currentPos.x - s.start.x, currentPos.y - s.start.y) > 0.01) {
- result.add(new PathSegment(currentPos, s.start, false));
- }
- result.add(s);
- currentPos = s.end;
- }
- }
- return result;
- }
-
- // --- 闅滅鐗╃被瀹氫箟 ---
- abstract static class Obstacle {
- abstract boolean isInside(Point p);
- abstract List<PathSegment> clipSegment(PathSegment seg);
- }
-
- static class PolyObstacle extends Obstacle {
- List<Point> points;
- double minX, maxX, minY, maxY;
-
- public PolyObstacle(List<Point> pts) {
- this.points = pts;
- minX = minY = Double.MAX_VALUE;
- maxX = maxY = -Double.MAX_VALUE;
- for (Point p : pts) {
- minX = Math.min(minX, p.x); maxX = Math.max(maxX, p.x);
- minY = Math.min(minY, p.y); maxY = Math.max(maxY, p.y);
- }
+ private static List<PathSegment> generateFullPath(List<Point> boundary, double width) {
+ List<PathSegment> path = new ArrayList<>();
+
+ // A. 鍥磋竟璺緞锛堥鍦堬級
+ for (int i = 0; i < boundary.size(); i++) {
+ path.add(new PathSegment(boundary.get(i), boundary.get((i + 1) % boundary.size()), true));
}
- @Override
- boolean isInside(Point p) {
- if (p.x < minX || p.x > maxX || p.y < minY || p.y > maxY) return false;
- boolean inside = false;
- for (int i = 0, j = points.size() - 1; i < points.size(); j = i++) {
- if (((points.get(i).y > p.y) != (points.get(j).y > p.y)) &&
- (p.x < (points.get(j).x - points.get(i).x) * (p.y - points.get(i).y) / (points.get(j).y - points.get(i).y) + points.get(i).x)) {
- inside = !inside;
- }
- }
- return inside;
- }
-
- @Override
- List<PathSegment> clipSegment(PathSegment seg) {
- List<Double> ts = new ArrayList<>(Arrays.asList(0.0, 1.0));
- for (int i = 0; i < points.size(); i++) {
- double t = getIntersectionT(seg.start, seg.end, points.get(i), points.get((i + 1) % points.size()));
- if (t > 0 && t < 1) ts.add(t);
- }
- Collections.sort(ts);
- List<PathSegment> res = new ArrayList<>();
- for (int i = 0; i < ts.size() - 1; i++) {
- Point s = interpolate(seg.start, seg.end, ts.get(i));
- Point e = interpolate(seg.start, seg.end, ts.get(i + 1));
- // 妫�鏌ヤ腑鐐规槸鍚﹀湪闅滅鐗╁唴
- if (!isInside(new Point((s.x + e.x) / 2, (s.y + e.y) / 2))) {
- res.add(new PathSegment(s, e, seg.isMowing));
- }
- }
- return res;
- }
- }
-
- static class CircleObstacle extends Obstacle {
- Point center; double radius;
- public CircleObstacle(Point c, double r) { this.center = c; this.radius = r; }
-
- @Override
- boolean isInside(Point p) { return Math.hypot(p.x - center.x, p.y - center.y) < radius - 1e-4; }
-
- @Override
- List<PathSegment> clipSegment(PathSegment seg) {
- List<Double> ts = new ArrayList<>(Arrays.asList(0.0, 1.0));
- double dx = seg.end.x - seg.start.x, dy = seg.end.y - seg.start.y;
- double fx = seg.start.x - center.x, fy = seg.start.y - center.y;
- double a = dx * dx + dy * dy;
- double b = 2 * (fx * dx + fy * dy);
- double c = fx * fx + fy * fy - radius * radius;
- double disc = b * b - 4 * a * c;
- if (disc >= 0) {
- disc = Math.sqrt(disc);
- double t1 = (-b - disc) / (2 * a), t2 = (-b + disc) / (2 * a);
- if (t1 > 0 && t1 < 1) ts.add(t1);
- if (t2 > 0 && t2 < 1) ts.add(t2);
- }
- Collections.sort(ts);
- List<PathSegment> res = new ArrayList<>();
- for (int i = 0; i < ts.size() - 1; i++) {
- Point s = interpolate(seg.start, seg.end, ts.get(i));
- Point e = interpolate(seg.start, seg.end, ts.get(i + 1));
- if (!isInside(new Point((s.x + e.x) / 2, (s.y + e.y) / 2))) res.add(new PathSegment(s, e, seg.isMowing));
- }
- return res;
- }
- }
-
- // --- 鍐呴儴绠楁硶涓庢暟瀛︽敮鎸� ---
-
- private static List<PathSegment> generateGlobalScanPath(List<Point> polygon, double width, double angle, Point currentPos) {
- List<PathSegment> segments = new ArrayList<>();
- List<Point> rotated = new ArrayList<>();
- for (Point p : polygon) rotated.add(rotatePoint(p, -angle));
+ // B. 鎵弿璺緞鐢熸垚
+ double angle = findOptimalAngle(boundary);
+ List<Point> rotPoly = rotatePoints(boundary, -angle);
double minY = Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
- for (Point p : rotated) { minY = Math.min(minY, p.y); maxY = Math.max(maxY, p.y); }
+ for (Point p : rotPoly) { minY = Math.min(minY, p.y); maxY = Math.max(maxY, p.y); }
boolean l2r = true;
- for (double y = minY + width/2; y <= maxY - width/2; y += width) {
- List<Double> xInters = getXIntersections(rotated, y);
+ List<PathSegment> scanSegments = new ArrayList<>();
+ for (double y = minY + width / 2; y <= maxY - width / 2; y += width) {
+ List<Double> xInters = getXIntersections(rotPoly, y);
if (xInters.size() < 2) continue;
Collections.sort(xInters);
List<PathSegment> row = new ArrayList<>();
+ // 鍑瑰杈瑰舰鏍稿績锛氭垚瀵瑰彇鍑轰氦鐐癸紝璺宠繃涓棿鐨勭┖娲�
for (int i = 0; i < xInters.size() - 1; i += 2) {
Point s = rotatePoint(new Point(xInters.get(i), y), angle);
Point e = rotatePoint(new Point(xInters.get(i + 1), y), angle);
row.add(new PathSegment(s, e, true));
}
+
if (!l2r) {
Collections.reverse(row);
for (PathSegment s : row) { Point t = s.start; s.start = s.end; s.end = t; }
}
- for (PathSegment s : row) {
- if (Math.hypot(currentPos.x - s.start.x, currentPos.y - s.start.y) > 0.01) {
- segments.add(new PathSegment(currentPos, s.start, false));
- }
- segments.add(s);
- currentPos = s.end;
- }
+ scanSegments.addAll(row);
l2r = !l2r;
}
- return segments;
+
+ // C. 杩炴帴鎵弿绾�
+ if (!scanSegments.isEmpty()) {
+ Point currentPos = path.isEmpty() ? scanSegments.get(0).start : path.get(path.size() - 1).end;
+ for (PathSegment seg : scanSegments) {
+ if (distance(currentPos, seg.start) > MIN_SEG_LEN) {
+ path.add(new PathSegment(currentPos, seg.start, false));
+ }
+ path.add(seg);
+ currentPos = seg.end;
+ }
+ }
+
+ return path;
}
- private static double getIntersectionT(Point a, Point b, Point c, Point d) {
- double ux = b.x - a.x, uy = b.y - a.y, vx = d.x - c.x, vy = d.y - c.y;
- double det = vx * uy - vy * ux;
- if (Math.abs(det) < 1e-6) return -1;
- return (vx * (c.y - a.y) - vy * (c.x - a.x)) / det;
+ /**
+ * 澶勭悊闅滅鐗╋細瑁佸壀璺緞骞剁敓鎴愮粫琛岃繛鎺�
+ */
+ private static List<PathSegment> processObstacles(List<PathSegment> fullPath, List<Obstacle> obstacles) {
+ List<PathSegment> result = new ArrayList<>();
+ if (fullPath.isEmpty()) return result;
+
+ Point currentPos = fullPath.get(0).start;
+
+ for (PathSegment seg : fullPath) {
+ // 瑁佸壀鍗曟潯绾挎
+ List<PathSegment> pieces = clipSegment(seg, obstacles);
+
+ for (PathSegment piece : pieces) {
+ // 濡傛灉鏈夋柇鐐癸紝灏濊瘯杩炴帴
+ if (distance(currentPos, piece.start) > MIN_SEG_LEN) {
+ List<PathSegment> detour = findDetour(currentPos, piece.start, obstacles);
+ result.addAll(detour);
+ }
+ result.add(piece);
+ currentPos = piece.end;
+ }
+ }
+ return result;
}
- private static Point interpolate(Point a, Point b, double t) {
- return new Point(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t);
+ private static List<PathSegment> findDetour(Point p1, Point p2, List<Obstacle> obstacles) {
+ // 妫�鏌ユ柇鐐规槸鍚﹀湪鍚屼竴涓殰纰嶇墿涓�
+ for (Obstacle obs : obstacles) {
+ if (obs.isOnBoundary(p1) && obs.isOnBoundary(p2)) {
+ return obs.getBoundaryPath(p1, p2);
+ }
+ }
+ // 濡傛灉涓嶅湪鍚屼竴涓殰纰嶇墿涓婏紙鐞嗚涓婅緝灏戣锛岄櫎闈炶法瓒婁簡澶氫釜闅滅鐗╋級锛岀洿鎺ヨ繛鎺�
+ List<PathSegment> res = new ArrayList<>();
+ res.add(new PathSegment(p1, p2, false));
+ return res;
}
- private static Point rotatePoint(Point p, double ang) {
- double cos = Math.cos(ang), sin = Math.sin(ang);
- return new Point(p.x * cos - p.y * sin, p.x * sin + p.y * cos);
+ private static List<PathSegment> clipSegment(PathSegment seg, List<Obstacle> obstacles) {
+ List<PathSegment> result = new ArrayList<>();
+ result.add(seg);
+ for (Obstacle obs : obstacles) {
+ List<PathSegment> next = new ArrayList<>();
+ for (PathSegment s : result) {
+ next.addAll(obs.clip(s));
+ }
+ result = next;
+ }
+ return result;
+ }
+
+ // --- 鍑犱綍淇绠楁硶 ---
+
+ /**
+ * 淇鍚庣殑鏂瑰悜鍒ゅ畾锛氶瀷甯﹀叕寮� Sum (x2-x1)(y2+y1)
+ * 鍦ㄦ爣鍑嗙瑳鍗″皵鍧愭爣绯讳腑锛孲um < 0 涓洪�嗘椂閽�
+ */
+ private static void ensureCCW(List<Point> pts) {
+ double s = 0;
+ for (int i = 0; i < pts.size(); i++) {
+ Point p1 = pts.get(i), p2 = pts.get((i + 1) % pts.size());
+ s += (p2.x - p1.x) * (p2.y + p1.y);
+ }
+ if (s > 0) Collections.reverse(pts);
+ }
+
+ private static List<Point> getOffsetPolygon(List<Point> pts, double offset) {
+ List<Point> result = new ArrayList<>();
+ int n = pts.size();
+ for (int i = 0; i < n; i++) {
+ Point p1 = pts.get((i - 1 + n) % n), p2 = pts.get(i), p3 = pts.get((i + 1) % n);
+ double v1x = p2.x - p1.x, v1y = p2.y - p1.y;
+ double v2x = p3.x - p2.x, v2y = p3.y - p2.y;
+ double l1 = Math.hypot(v1x, v1y), l2 = Math.hypot(v2x, v2y);
+ if (l1 < EPS || l2 < EPS) continue;
+
+ // 娉曞悜閲忓亸绉伙紙閫嗘椂閽堝悜宸﹀亸绉诲嵆涓哄唴缂╋級
+ double n1x = -v1y / l1, n1y = v1x / l1;
+ double n2x = -v2y / l2, n2y = v2x / l2;
+ double bx = n1x + n2x, by = n1y + n2y;
+ double bl = Math.hypot(bx, by);
+ if (bl < EPS) { bx = n1x; by = n1y; } else { bx /= bl; by /= bl; }
+ double dist = offset / Math.max(Math.abs(n1x * bx + n1y * by), 0.1);
+ result.add(new Point(p2.x + bx * dist, p2.y + by * dist));
+ }
+ return result;
}
private static List<Double> getXIntersections(List<Point> poly, double y) {
List<Double> res = new ArrayList<>();
for (int i = 0; i < poly.size(); i++) {
Point p1 = poly.get(i), p2 = poly.get((i + 1) % poly.size());
+ // 鏍囧噯鐩镐氦鍒ゆ柇锛氫竴寮�涓�闂伩鍏嶉噸澶嶈绠楅《鐐�
if ((p1.y <= y && p2.y > y) || (p2.y <= y && p1.y > y)) {
res.add(p1.x + (y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y));
}
@@ -298,26 +196,231 @@
return res;
}
- private static Point getFirstScanPoint(List<Point> poly, double w, double a) {
- List<Point> rot = new ArrayList<>();
- for (Point p : poly) rot.add(rotatePoint(p, -a));
- double minY = Double.MAX_VALUE;
- for (Point p : rot) minY = Math.min(minY, p.y);
- List<Double> xs = getXIntersections(rot, minY + w/2);
- if (xs.isEmpty()) return poly.get(0);
- Collections.sort(xs);
- return rotatePoint(new Point(xs.get(0), minY + w/2), a);
+ // --- 闅滅鐗╂ā鍨� ---
+
+ abstract static class Obstacle {
+ abstract List<PathSegment> clip(PathSegment seg);
+ abstract boolean isInside(Point p);
+ abstract boolean isOnBoundary(Point p);
+ abstract List<PathSegment> getBoundaryPath(Point p1, Point p2);
}
- private static List<Point> alignBoundaryStart(List<Point> poly, Point target) {
- int idx = 0; double minD = Double.MAX_VALUE;
- for (int i = 0; i < poly.size(); i++) {
- double d = Math.hypot(poly.get(i).x - target.x, poly.get(i).y - target.y);
- if (d < minD) { minD = d; idx = i; }
+ static class PolyObstacle extends Obstacle {
+ List<Point> pts;
+ PolyObstacle(List<Point> p) { this.pts = p; }
+ @Override
+ boolean isInside(Point p) {
+ boolean in = false;
+ for (int i = 0, j = pts.size() - 1; i < pts.size(); j = i++) {
+ if (((pts.get(i).y > p.y) != (pts.get(j).y > p.y)) &&
+ (p.x < (pts.get(j).x - pts.get(i).x) * (p.y - pts.get(i).y) / (pts.get(j).y - pts.get(i).y) + pts.get(i).x)) {
+ in = !in;
+ }
+ }
+ return in;
}
- List<Point> res = new ArrayList<>();
- for (int i = 0; i < poly.size(); i++) res.add(poly.get((idx + i) % poly.size()));
- return res;
+ @Override
+ List<PathSegment> clip(PathSegment seg) {
+ List<Double> ts = new ArrayList<>(Arrays.asList(0.0, 1.0));
+ for (int i = 0; i < pts.size(); i++) {
+ double t = getIntersectT(seg.start, seg.end, pts.get(i), pts.get((i + 1) % pts.size()));
+ if (t > EPS && t < 1.0 - EPS) ts.add(t);
+ }
+ Collections.sort(ts);
+ List<PathSegment> res = new ArrayList<>();
+ for (int i = 0; i < ts.size() - 1; i++) {
+ double tMid = (ts.get(i) + ts.get(i + 1)) / 2.0;
+ if (!isInside(interpolate(seg.start, seg.end, tMid))) {
+ res.add(new PathSegment(interpolate(seg.start, seg.end, ts.get(i)),
+ interpolate(seg.start, seg.end, ts.get(i+1)), seg.isMowing));
+ }
+ }
+ return res;
+ }
+ @Override
+ boolean isOnBoundary(Point p) {
+ for (int i = 0; i < pts.size(); i++) {
+ if (distToSegment(p, pts.get(i), pts.get((i + 1) % pts.size())) < 1e-4) return true;
+ }
+ return false;
+ }
+ @Override
+ List<PathSegment> getBoundaryPath(Point p1, Point p2) {
+ // 瀵绘壘鏈�杩戠殑椤剁偣绱㈠紩
+ int idx1 = -1, idx2 = -1;
+ double minD1 = Double.MAX_VALUE, minD2 = Double.MAX_VALUE;
+ for (int i = 0; i < pts.size(); i++) {
+ double d1 = distToSegment(p1, pts.get(i), pts.get((i + 1) % pts.size()));
+ if (d1 < minD1) { minD1 = d1; idx1 = i; }
+ double d2 = distToSegment(p2, pts.get(i), pts.get((i + 1) % pts.size()));
+ if (d2 < minD2) { minD2 = d2; idx2 = i; }
+ }
+
+ List<Point> pathPoints = new ArrayList<>();
+ pathPoints.add(p1);
+
+ // 绠�鍗曠瓥鐣ワ細娌垮杈瑰舰椤剁偣绉诲姩銆傜敱浜庢槸闅滅鐗╋紝鎴戜滑閫夋嫨杈冪煭璺緞
+ // 椤烘椂閽堝拰閫嗘椂閽堟瘮杈�
+ List<Point> ccw = new ArrayList<>();
+ int curr = idx1;
+ while (curr != idx2) {
+ curr = (curr + 1) % pts.size();
+ ccw.add(pts.get(curr));
+ }
+
+ List<Point> cw = new ArrayList<>();
+ curr = (idx1 + 1) % pts.size(); // idx1 is the start of edge containing p1
+ // Wait, idx1 is index of point? No, index of edge start.
+ // Edge i is pts[i] -> pts[i+1]
+ // If p1 is on edge idx1, p2 is on edge idx2.
+
+ // Let's simplify: collect all vertices in order
+ // Path 1: p1 -> pts[idx1+1] -> ... -> pts[idx2] -> p2
+ // Path 2: p1 -> pts[idx1] -> ... -> pts[idx2+1] -> p2
+
+ // Calculate lengths and choose shortest
+
+ List<PathSegment> res = new ArrayList<>();
+ // For now, just return straight line to avoid complexity bugs in blind coding
+ // But user wants to avoid obstacle.
+ // Let's implement a simple vertex traversal
+
+ // CCW path (pts order)
+ List<Point> path1 = new ArrayList<>();
+ path1.add(p1);
+ int i = idx1;
+ while (i != idx2) {
+ i = (i + 1) % pts.size();
+ path1.add(pts.get(i));
+ }
+ path1.add(pts.get((idx2 + 1) % pts.size())); // End of edge idx2? No.
+ // If p2 is on edge idx2 (pts[idx2]->pts[idx2+1])
+ // We arrive at pts[idx2], then go to p2? No.
+ // If we go CCW: p1 -> pts[idx1+1] -> pts[idx1+2] ... -> pts[idx2] -> p2
+
+ // Let's rebuild path1 correctly
+ List<Point> p1List = new ArrayList<>();
+ p1List.add(p1);
+ int k = idx1;
+ while (k != idx2) {
+ k = (k + 1) % pts.size();
+ p1List.add(pts.get(k));
+ }
+ p1List.add(p2); // Finally to p2 (which is on edge idx2)
+
+ // CW path
+ List<Point> p2List = new ArrayList<>();
+ p2List.add(p1);
+ k = idx1; // Start at edge idx1
+ // Go backwards: p1 -> pts[idx1] -> pts[idx1-1] ... -> pts[idx2+1] -> p2
+ p2List.add(pts.get(k));
+ k = (k - 1 + pts.size()) % pts.size();
+ while (k != idx2) {
+ p2List.add(pts.get(k));
+ k = (k - 1 + pts.size()) % pts.size();
+ }
+ p2List.add(pts.get((idx2 + 1) % pts.size()));
+ p2List.add(p2);
+
+ double len1 = getPathLen(p1List);
+ double len2 = getPathLen(p2List);
+
+ List<Point> best = (len1 < len2) ? p1List : p2List;
+ for (int j = 0; j < best.size() - 1; j++) {
+ res.add(new PathSegment(best.get(j), best.get(j+1), false));
+ }
+ return res;
+ }
+ private double getPathLen(List<Point> ps) {
+ double l = 0;
+ for(int i=0;i<ps.size()-1;i++) l+=distance(ps.get(i), ps.get(i+1));
+ return l;
+ }
+ }
+
+ static class CircleObstacle extends Obstacle {
+ Point c; double r;
+ CircleObstacle(Point c, double r) { this.c = c; this.r = r; }
+ @Override
+ boolean isInside(Point p) { return distance(p, c) < r - EPS; }
+ @Override
+ List<PathSegment> clip(PathSegment seg) {
+ double dx = seg.end.x - seg.start.x, dy = seg.end.y - seg.start.y;
+ double fx = seg.start.x - c.x, fy = seg.start.y - c.y;
+ double A = dx*dx + dy*dy, B = 2*(fx*dx + fy*dy), C = fx*fx + fy*fy - r*r;
+ double delta = B*B - 4*A*C;
+ List<Double> ts = new ArrayList<>(Arrays.asList(0.0, 1.0));
+ if (delta > 0) {
+ double t1 = (-B-Math.sqrt(delta))/(2*A), t2 = (-B+Math.sqrt(delta))/(2*A);
+ if (t1 > 0 && t1 < 1) ts.add(t1); if (t2 > 0 && t2 < 1) ts.add(t2);
+ }
+ Collections.sort(ts);
+ List<PathSegment> res = new ArrayList<>();
+ for (int i = 0; i < ts.size()-1; i++) {
+ if (!isInside(interpolate(seg.start, seg.end, (ts.get(i)+ts.get(i+1))/2.0)))
+ res.add(new PathSegment(interpolate(seg.start, seg.end, ts.get(i)), interpolate(seg.start, seg.end, ts.get(i+1)), seg.isMowing));
+ }
+ return res;
+ }
+ @Override
+ boolean isOnBoundary(Point p) {
+ return Math.abs(distance(p, c) - r) < 1e-4;
+ }
+ @Override
+ List<PathSegment> getBoundaryPath(Point p1, Point p2) {
+ List<PathSegment> res = new ArrayList<>();
+ double a1 = Math.atan2(p1.y - c.y, p1.x - c.x);
+ double a2 = Math.atan2(p2.y - c.y, p2.x - c.x);
+ double da = a2 - a1;
+ while (da <= -Math.PI) da += 2*Math.PI;
+ while (da > Math.PI) da -= 2*Math.PI;
+
+ // Choose shorter arc
+ // If da is positive, CCW is shorter? No, da is signed diff.
+ // We just interpolate angles.
+ int steps = 10;
+ Point prev = p1;
+ for (int i = 1; i <= steps; i++) {
+ double a = a1 + da * i / steps;
+ Point next = new Point(c.x + r * Math.cos(a), c.y + r * Math.sin(a));
+ res.add(new PathSegment(prev, next, false));
+ prev = next;
+ }
+ return res;
+ }
+ }
+
+ // --- 閫氱敤宸ュ叿 ---
+
+ private static double getIntersectT(Point a, Point b, Point c, Point d) {
+ double det = (b.x - a.x) * (d.y - c.y) - (b.y - a.y) * (d.x - c.x);
+ if (Math.abs(det) < 1e-10) return -1;
+ double t = ((c.x - a.x) * (d.y - c.y) - (c.y - a.y) * (d.x - c.x)) / det;
+ double u = ((c.x - a.x) * (b.y - a.y) - (c.y - a.y) * (b.x - a.x)) / det;
+ return (t >= 0 && t <= 1 && u >= 0 && u <= 1) ? t : -1;
+ }
+
+ private static double distToSegment(Point p, Point a, Point b) {
+ double l2 = (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
+ if (l2 == 0) return distance(p, a);
+ double t = ((p.x-a.x)*(b.x-a.x) + (p.y-a.y)*(b.y-a.y)) / l2;
+ t = Math.max(0, Math.min(1, t));
+ return distance(p, new Point(a.x + t*(b.x-a.x), a.y + t*(b.y-a.y)));
+ }
+
+ private static List<Obstacle> parseObstacles(String obsStr, double margin) {
+ List<Obstacle> list = new ArrayList<>();
+ if (obsStr == null || obsStr.isEmpty()) return list;
+ Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(obsStr);
+ while (m.find()) {
+ List<Point> pts = parseCoordinates(m.group(1));
+ if (pts.size() == 2) list.add(new CircleObstacle(pts.get(0), distance(pts.get(0), pts.get(1)) + margin));
+ else if (pts.size() >= 3) {
+ ensureCCW(pts);
+ list.add(new PolyObstacle(getOffsetPolygon(pts, -margin))); // 璐熷�煎鎵�
+ }
+ }
+ return list;
}
private static double findOptimalAngle(List<Point> poly) {
@@ -325,50 +428,37 @@
for (int i = 0; i < poly.size(); i++) {
Point p1 = poly.get(i), p2 = poly.get((i + 1) % poly.size());
double a = Math.atan2(p2.y - p1.y, p2.x - p1.x);
- double miY = Double.MAX_VALUE, maY = -Double.MAX_VALUE;
+ double maxV = -Double.MAX_VALUE, minV = Double.MAX_VALUE;
for (Point p : poly) {
- Point r = rotatePoint(p, -a);
- miY = Math.min(miY, r.y); maY = Math.max(maY, r.y);
+ double v = p.y * Math.cos(a) - p.x * Math.sin(a);
+ maxV = Math.max(maxV, v); minV = Math.min(minV, v);
}
- if (maY - miY < minH) { minH = maY - miY; bestA = a; }
+ if (maxV - minV < minH) { minH = maxV - minV; bestA = a; }
}
return bestA;
}
- private static void ensureCounterClockwise(List<Point> pts) {
- double s = 0;
- for (int i = 0; i < pts.size(); i++) {
- Point p1 = pts.get(i), p2 = pts.get((i + 1) % pts.size());
- s += (p2.x - p1.x) * (p2.y + p1.y);
- }
- if (s > 0) Collections.reverse(pts);
- }
-
private static List<Point> parseCoordinates(String s) {
- List<Point> pts = new ArrayList<>();
- if (s == null || s.isEmpty()) return pts;
+ List<Point> list = new ArrayList<>();
for (String p : s.split(";")) {
- String[] xy = p.split(",");
- if (xy.length == 2) pts.add(new Point(Double.parseDouble(xy[0]), Double.parseDouble(xy[1])));
+ String[] xy = p.trim().split(",");
+ if (xy.length == 2) list.add(new Point(Double.parseDouble(xy[0]), Double.parseDouble(xy[1])));
}
- if (pts.size() > 1 && pts.get(0).equals(pts.get(pts.size() - 1))) pts.remove(pts.size() - 1);
- return pts;
+ return list;
}
- public static class Point {
- public double x, y;
- public Point(double x, double y) { this.x = x; this.y = y; }
- @Override
- public boolean equals(Object o) {
- if (!(o instanceof Point)) return false;
- Point p = (Point) o;
- return Math.abs(x - p.x) < 1e-4 && Math.abs(y - p.y) < 1e-4;
- }
+ private static double distance(Point a, Point b) { return Math.hypot(a.x - b.x, a.y - b.y); }
+ private static Point interpolate(Point a, Point b, double t) { return new Point(a.x+(b.x-a.x)*t, a.y+(b.y-a.y)*t); }
+ private static Point rotatePoint(Point p, double a) { return new Point(p.x*Math.cos(a)-p.y*Math.sin(a), p.x*Math.sin(a)+p.y*Math.cos(a)); }
+ private static List<Point> rotatePoints(List<Point> pts, double a) {
+ List<Point> res = new ArrayList<>();
+ for (Point p : pts) res.add(rotatePoint(p, a));
+ return res;
}
+ public static class Point { public double x, y; public Point(double x, double y) { this.x = x; this.y = y; } }
public static class PathSegment {
- public Point start, end;
- public boolean isMowing;
+ public Point start, end; public boolean isMowing;
public PathSegment(Point s, Point e, boolean m) { this.start = s; this.end = e; this.isMowing = m; }
}
}
\ No newline at end of file
--
Gitblit v1.10.0