From 0930bed760105b81e2e5055801bec6d6e8d57358 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期二, 23 十二月 2025 18:40:08 +0800
Subject: [PATCH] 新增了功能
---
src/lujing/YixinglujingNoObstacle.java | 528 +++++++++++++++++-----------------------------------------
1 files changed, 153 insertions(+), 375 deletions(-)
diff --git a/src/lujing/YixinglujingNoObstacle.java b/src/lujing/YixinglujingNoObstacle.java
index ae28e81..67a5bb0 100644
--- a/src/lujing/YixinglujingNoObstacle.java
+++ b/src/lujing/YixinglujingNoObstacle.java
@@ -1,454 +1,232 @@
package lujing;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
+import java.util.*;
/**
- * 寮傚舰锛堟棤闅滅鐗╋級鑽夊湴璺緞瑙勫垝绫� - 浼樺寲鐗� V2.0
- * * 鍔熻兘鐗圭偣锛�
- * 1. 鑷姩澶勭悊鍑瑰杈瑰舰锛堥�氳繃鑰冲垏娉曞垎鍓诧級
- * 2. 澧炲姞鈥滃洿杈光�濊矾寰勶紝淇濊瘉杈圭紭鍓茶崏鏁存磥
- * 3. 鑷姩璁$畻姣忎釜瀛愬尯鍩熺殑鏈�浼樻壂鎻忚搴︼紙鍑忓皯鎺夊ご娆℃暟锛�
- * 4. 鏅鸿兘鍖哄煙杩炴帴锛堟敮鎸佸弻鍚戣矾寰勯�夋嫨锛�
+ * 寮傚舰鑽夊湴璺緞瑙勫垝 - 鍥磋竟+鍏ㄥ眬鎵弿鐗� V4.1
+ * 浼樺寲锛氬洿杈圭粓鐐逛笌寮撳瓧褰㈣捣鐐硅嚜鍔ㄥ榻愶紝瀹炵幇鏃犵紳鍒囨崲锛岀‘淇濊矾寰勪笉瓒婄晫
*/
public class YixinglujingNoObstacle {
- // ==========================================
- // 瀵瑰鎺ュ彛
- // ==========================================
-
- /**
- * 瑙勫垝寮傚舰鑽夊湴鍓茶崏璺緞
- *
- * @param coordinates 鍦板潡杈圭晫鍧愭爣瀛楃涓诧紝鏍煎紡锛�"x1,y1;x2,y2;x3,y3;..."
- * @param widthStr 鍓茶崏瀹藉害锛堢背锛夛紝濡� "0.34"
- * @param marginStr 瀹夊叏杈硅窛锛堢背锛夛紝濡� "0.2"
- * @return 璺緞娈靛垪琛�
- */
public static List<PathSegment> planPath(String coordinates, String widthStr, String marginStr) {
- // 1. 鍙傛暟瑙f瀽涓庨澶勭悊
List<Point> rawPoints = parseCoordinates(coordinates);
- if (rawPoints.size() < 3) {
- throw new IllegalArgumentException("澶氳竟褰㈢偣鏁颁笉瓒筹紝鏃犳硶鏋勬垚鍦板潡");
- }
- // 纭繚閫嗘椂閽堥『搴忥紝鏂逛究鍚庣画鍑犱綍璁$畻
- ensureCounterClockwise(rawPoints);
+ if (rawPoints.size() < 3) return new ArrayList<>();
double mowWidth = Double.parseDouble(widthStr);
double safeMargin = Double.parseDouble(marginStr);
+
+ // 1. 棰勫鐞嗭細閫嗘椂閽堝寲
+ ensureCounterClockwise(rawPoints);
+ // 2. 鐢熸垚鍐呯缉澶氳竟褰�
+ List<Point> boundary = getInsetPolygon(rawPoints, safeMargin);
+ if (boundary.size() < 3) return new ArrayList<>();
+
+ // 3. 纭畾鏈�浼樻壂鎻忚搴﹀苟鎵惧埌寮撳瓧褰㈣矾寰勭殑绗竴涓綔涓氳捣鐐�
+ double bestAngle = findOptimalAngle(boundary);
+ Point firstScanStart = getFirstScanPoint(boundary, mowWidth, bestAngle);
+
+ // 4. 瀵归綈鍥磋竟璧风偣锛氶噸鏂版帓鍒楀洿杈瑰潗鏍囷紝浣挎渶鍚庝竴涓偣闈犺繎(鎴栫瓑浜�)鎵弿璧风偣
+ List<Point> alignedBoundary = alignBoundaryStart(boundary, firstScanStart);
+
List<PathSegment> finalPath = new ArrayList<>();
- // 2. 鐢熸垚鍥磋竟璺緞 (Contour Path)
- // 杩欎竴姝ュ厛瑙勫垝涓�鍦堣疆寤擄紝瑙e喅寮傚舰杈圭紭闅惧鐞嗙殑闂
- List<Point> contourPoly = getInsetPolygon(rawPoints, safeMargin);
-
- // 濡傛灉鍐呯缉鍚庨潰绉お灏忔垨鐐规暟涓嶈冻锛岀洿鎺ヨ繑鍥炵┖
- if (contourPoly.size() < 3) {
- return new ArrayList<>();
+ // 5. 銆愮涓�姝ャ�戠敓鎴愬洿杈硅矾寰�
+ for (int i = 0; i < alignedBoundary.size(); i++) {
+ Point pStart = alignedBoundary.get(i);
+ Point pEnd = alignedBoundary.get((i + 1) % alignedBoundary.size());
+ finalPath.add(new PathSegment(pStart, pEnd, true));
}
- // 灏嗗洿杈硅矾寰勫姞鍏ョ粨鏋�
- for (int i = 0; i < contourPoly.size(); i++) {
- Point p1 = contourPoly.get(i);
- Point p2 = contourPoly.get((i + 1) % contourPoly.size());
- finalPath.add(new PathSegment(p1, p2, true)); // true = 鍓茶崏
- }
+ // 6. 銆愮浜屾銆戜粠瀵归綈鍚庣殑缁堢偣寮�濮嬬敓鎴愬唴閮ㄦ壂鎻忚矾寰�
+ Point lastEdgePos = alignedBoundary.get(0); // 鍥磋竟闂悎鍥炲埌璧风偣
+ List<PathSegment> scanPath = generateGlobalScanPath(boundary, mowWidth, bestAngle, lastEdgePos);
- // 璁板綍鍥磋竟缁撴潫鍚庣殑浣嶇疆锛堥�氬父鍥炲埌鍥磋竟璧风偣锛�
- Point endOfContour = contourPoly.get(0);
-
- // 3. 鍖哄煙鍒嗗壊 (Decomposition)
- // 浣跨敤鑰冲垏娉曞皢鍥磋竟鍚庣殑澶氳竟褰㈠垎鍓蹭负澶氫釜鍑稿杈瑰舰锛堜笁瑙掑舰锛�
- // 杩欐牱鍙互淇濊瘉瑕嗙洊鏃犻仐婕�
- List<List<Point>> triangles = triangulatePolygon(contourPoly);
-
- // 4. 瀵规瘡涓尯鍩熺敓鎴愬唴閮ㄥ~鍏呰矾寰�
- List<List<PathSegment>> allRegionPaths = new ArrayList<>();
-
- for (List<Point> triangle : triangles) {
- // 銆愪紭鍖栥�戝鎵炬渶浼樻壂鎻忚搴︼細
- // 閬嶅巻涓夎褰㈢殑涓夋潯杈癸紝璁$畻浠ュ摢鏉¤竟涓哄熀鍑嗘壂鎻忔椂锛岀敓鎴愮殑琛屾暟鏈�灏戯紙杞集鏈�灏戯級
- List<PathSegment> regionPath = planConvexPathOptimal(triangle, mowWidth);
- if (!regionPath.isEmpty()) {
- allRegionPaths.add(regionPath);
- }
- }
-
- // 5. 杩炴帴鎵�鏈夊唴閮ㄥ尯鍩� (Greedy Connection)
- // 浠庡洿杈圭粨鏉熺偣寮�濮嬶紝瀵绘壘鏈�杩戠殑涓嬩竴涓尯鍩�
- List<PathSegment> internalPaths = connectRegions(allRegionPaths, endOfContour);
- finalPath.addAll(internalPaths);
+ finalPath.addAll(scanPath);
return finalPath;
}
- // ==========================================
- // 鏍稿績瑙勫垝绠楁硶
- // ==========================================
-
/**
- * 瑙勫垝鍑稿杈瑰舰璺緞锛岃嚜鍔ㄩ�夋嫨鏈�浼樿搴�
+ * 璁$畻骞惰幏鍙栨壂鎻忚矾寰勭殑绗竴琛岃捣鐐�
*/
- private static List<PathSegment> planConvexPathOptimal(List<Point> polygon, double width) {
- if (polygon.size() < 3) return new ArrayList<>();
+ private static Point getFirstScanPoint(List<Point> polygon, double width, double angle) {
+ List<Point> rotatedPoly = new ArrayList<>();
+ for (Point p : polygon) rotatedPoly.add(rotatePoint(p, -angle));
- double bestAngle = 0;
- double minLines = Double.MAX_VALUE;
+ double minY = Double.MAX_VALUE;
+ for (Point p : rotatedPoly) minY = Math.min(minY, p.y);
- // 閬嶅巻澶氳竟褰㈢殑姣忎竴鏉¤竟锛屽皾璇曚互璇ヨ竟瑙掑害杩涜鎵弿
- for (int i = 0; i < polygon.size(); i++) {
- Point p1 = polygon.get(i);
- Point p2 = polygon.get((i + 1) % polygon.size());
-
- // 璁$畻杈圭殑瑙掑害
- double angle = Math.atan2(p2.y - p1.y, p2.x - p1.x);
-
- // 璁$畻鍦ㄨ繖涓搴︿笅锛屽杈瑰舰鐨勫瀭鐩存姇褰遍珮搴�
- // 楂樺害瓒婂皬锛屾剰鍛崇潃娌挎鏂瑰悜鎵弿鐨勮鏁拌秺灏戯紝鏁堢巼瓒婇珮
- double height = calculatePolygonHeight(polygon, -angle);
-
- if (height < minLines) {
- minLines = height;
- bestAngle = angle;
- }
- }
-
- // 浣跨敤鏈�浣宠搴︾敓鎴愯矾寰�
- return generatePathWithAngle(polygon, width, bestAngle);
+ double firstY = minY + width;
+ List<Double> xIntersections = getXIntersections(rotatedPoly, firstY);
+
+ if (xIntersections.isEmpty()) return polygon.get(0);
+ return rotatePoint(new Point(Collections.min(xIntersections), firstY), angle);
}
/**
- * 鏍规嵁鎸囧畾瑙掑害鐢熸垚寮撳瓧褰㈣矾寰�
+ * 閲嶆柊鎺掑垪澶氳竟褰㈤《鐐癸紝浣胯捣濮嬬偣涓庢壂鎻忚捣鐐瑰鎺�
*/
- private static List<PathSegment> generatePathWithAngle(List<Point> polygon, double width, double angle) {
- // 1. 灏嗗杈瑰舰鏃嬭浆鍒版按骞充綅缃�
- List<Point> rotatedPoints = new ArrayList<>();
- for (Point p : polygon) {
- rotatedPoints.add(rotatePoint(p, -angle));
+ private static List<Point> alignBoundaryStart(List<Point> boundary, Point targetStart) {
+ int bestIdx = 0;
+ double minDist = Double.MAX_VALUE;
+ for (int i = 0; i < boundary.size(); i++) {
+ double d = Math.hypot(boundary.get(i).x - targetStart.x, boundary.get(i).y - targetStart.y);
+ if (d < minDist) {
+ minDist = d;
+ bestIdx = i;
+ }
}
+ List<Point> aligned = new ArrayList<>();
+ for (int i = 0; i < boundary.size(); i++) {
+ aligned.add(boundary.get((bestIdx + i) % boundary.size()));
+ }
+ return aligned;
+ }
- // 2. 璁$畻Y杞磋寖鍥�
- double minY = Double.MAX_VALUE;
- double maxY = -Double.MAX_VALUE;
- for (Point p : rotatedPoints) {
+ private static List<PathSegment> generateGlobalScanPath(List<Point> polygon, double width, double angle, Point currentPos) {
+ List<PathSegment> segments = new ArrayList<>();
+ 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);
}
- List<PathSegment> segments = new ArrayList<>();
boolean leftToRight = true;
+ // 浠� minY + width 寮�濮嬶紝閬垮紑鍥磋竟宸插壊鍖哄煙
+ for (double y = minY + width; y <= maxY - width/2; y += width) {
+ List<Double> xIntersections = getXIntersections(rotatedPoly, y);
+ if (xIntersections.size() < 2) continue;
+ Collections.sort(xIntersections);
- // 3. 鎵弿绾跨敓鎴� (浠� minY + width/2 寮�濮嬶紝淇濊瘉绗竴鍒�鍒囧湪澶氳竟褰㈠唴)
- for (double y = minY + width / 2; y <= maxY; y += width) {
- List<Double> intersections = new ArrayList<>();
- for (int i = 0; i < rotatedPoints.size(); i++) {
- Point p1 = rotatedPoints.get(i);
- Point p2 = rotatedPoints.get((i + 1) % rotatedPoints.size());
-
- // 鍒ゆ柇鎵弿绾挎槸鍚︾┛杩囪竟
- if ((p1.y <= y && p2.y > y) || (p2.y <= y && p1.y > y)) {
- double x = p1.x + (y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
- intersections.add(x);
+ List<PathSegment> lineRows = new ArrayList<>();
+ for (int i = 0; i < xIntersections.size() - 1; i += 2) {
+ Point pS = rotatePoint(new Point(xIntersections.get(i), y), angle);
+ Point pE = rotatePoint(new Point(xIntersections.get(i + 1), y), angle);
+ lineRows.add(new PathSegment(pS, pE, true));
+ }
+
+ if (!leftToRight) {
+ Collections.reverse(lineRows);
+ for (PathSegment s : lineRows) {
+ Point t = s.start; s.start = s.end; s.end = t;
}
}
- Collections.sort(intersections);
- // 鎴愬鐢熸垚绾挎
- for (int k = 0; k < intersections.size() - 1; k += 2) {
- double x1 = leftToRight ? intersections.get(k) : intersections.get(k + 1);
- double x2 = leftToRight ? intersections.get(k + 1) : intersections.get(k);
-
- Point start = new Point(x1, y);
- Point end = new Point(x2, y);
-
- // 鏃嬭浆鍥炲師濮嬪潗鏍囩郴
- Point originalStart = rotatePoint(start, angle);
- Point originalEnd = rotatePoint(end, angle);
-
- // 杩炴帴閫昏緫锛氬鏋滀笉鏄涓�娈碉紝闇�瑕佷粠涓婁竴娈电粓鐐硅繛杩囨潵
- if (!segments.isEmpty()) {
- PathSegment prev = segments.get(segments.size() - 1);
- // 娣诲姞杩炴帴绾匡紙閫氬父绠椾綔鍓茶崏璺緞鐨勪竴閮ㄥ垎锛屼繚鎸佸紦瀛楀舰杩炵画锛�
- segments.add(new PathSegment(prev.end, originalStart, true));
+ for (PathSegment s : lineRows) {
+ // 濡傛灉闂磋窛鏋佸皬锛岃涓烘棤缂濊鎺�
+ if (Math.hypot(currentPos.x - s.start.x, currentPos.y - s.start.y) > 0.05) {
+ segments.add(new PathSegment(currentPos, s.start, false));
}
-
- segments.add(new PathSegment(originalStart, originalEnd, true));
+ segments.add(s);
+ currentPos = s.end;
}
- leftToRight = !leftToRight; // 鎹㈠悜
+ leftToRight = !leftToRight;
}
-
return segments;
}
- /**
- * 杩炴帴鎵�鏈夊垎鍓插悗鐨勫尯鍩� (璐績绛栫暐 + 鍙屽悜浼樺寲)
- */
- private static List<PathSegment> connectRegions(List<List<PathSegment>> regions, Point startPoint) {
- List<PathSegment> result = new ArrayList<>();
- if (regions.isEmpty()) return result;
-
- List<List<PathSegment>> remaining = new ArrayList<>(regions);
- Point currentPos = startPoint;
-
- while (!remaining.isEmpty()) {
- int bestIndex = -1;
- double minDist = Double.MAX_VALUE;
- boolean needReverse = false;
-
- // 瀵绘壘绂诲綋鍓嶄綅缃渶杩戠殑鍖哄煙璧风偣鎴栫粓鐐�
- for (int i = 0; i < remaining.size(); i++) {
- List<PathSegment> region = remaining.get(i);
- Point pStart = region.get(0).start;
- Point pEnd = region.get(region.size() - 1).end;
-
- double dStart = distance(currentPos, pStart);
- double dEnd = distance(currentPos, pEnd);
-
- // 妫�鏌ユ鍚戣繘鍏�
- if (dStart < minDist) {
- minDist = dStart;
- bestIndex = i;
- needReverse = false;
- }
- // 妫�鏌ュ弽鍚戣繘鍏ワ紙鍊掔潃鍓茶崏濡傛灉鏇磋繎锛�
- if (dEnd < minDist) {
- minDist = dEnd;
- bestIndex = i;
- needReverse = true;
- }
- }
-
- if (bestIndex != -1) {
- List<PathSegment> targetRegion = remaining.remove(bestIndex);
-
- if (needReverse) {
- // 鍙嶈浆璇ュ尯鍩熺殑鎵�鏈夎矾寰�
- List<PathSegment> reversedRegion = new ArrayList<>();
- for (int k = targetRegion.size() - 1; k >= 0; k--) {
- PathSegment seg = targetRegion.get(k);
- // 浜ゆ崲璧风偣缁堢偣
- reversedRegion.add(new PathSegment(seg.end, seg.start, seg.isMowing));
- }
- targetRegion = reversedRegion;
- }
-
- // 娣诲姞杩囨浮璺緞锛堟姮鍒�绉诲姩锛宨sMowing=false锛�
- Point nextStart = targetRegion.get(0).start;
- // 鍙湁璺濈鏄捐憲鎵嶆坊鍔犵Щ鍔ㄦ
- if (distance(currentPos, nextStart) > 0.01) {
- result.add(new PathSegment(currentPos, nextStart, false));
- }
-
- result.addAll(targetRegion);
- currentPos = targetRegion.get(targetRegion.size() - 1).end;
- } else {
- break; // 闃插尽鎬т唬鐮�
+ private static List<Double> getXIntersections(List<Point> rotatedPoly, double y) {
+ List<Double> xIntersections = new ArrayList<>();
+ for (int i = 0; i < rotatedPoly.size(); i++) {
+ Point p1 = rotatedPoly.get(i);
+ Point p2 = rotatedPoly.get((i + 1) % rotatedPoly.size());
+ if ((p1.y <= y && p2.y > y) || (p2.y <= y && p1.y > y)) {
+ double x = p1.x + (y - p1.y) * (p2.x - p1.x) / (p2.y - p1.y);
+ xIntersections.add(x);
}
}
- return result;
+ return xIntersections;
}
- // ==========================================
- // 鍑犱綍杩愮畻杈呭姪鏂规硶
- // ==========================================
-
- /**
- * 鍐呯缉澶氳竟褰� (鍩轰簬瑙掑钩鍒嗙嚎)
- */
- private static List<Point> getInsetPolygon(List<Point> points, double margin) {
- List<Point> result = new ArrayList<>();
- int n = points.size();
-
- for (int i = 0; i < n; i++) {
- Point pPrev = points.get((i - 1 + n) % n);
- Point pCurr = points.get(i);
- Point pNext = points.get((i + 1) % n);
-
- Point v1 = new Point(pCurr.x - pPrev.x, pCurr.y - pPrev.y);
- Point v2 = new Point(pNext.x - pCurr.x, pNext.y - pCurr.y);
-
- double len1 = Math.hypot(v1.x, v1.y);
- double len2 = Math.hypot(v2.x, v2.y);
-
- if (len1 < 1e-6 || len2 < 1e-6) continue;
-
- // 褰掍竴鍖栧悜閲�
- Point n1 = new Point(v1.x / len1, v1.y / len1);
- Point n2 = new Point(v2.x / len2, v2.y / len2);
-
- // 璁$畻骞冲垎绾挎柟鍚�
- // v1鍙嶅悜 + v2
- Point bisector = new Point(-n1.x + n2.x, -n1.y + n2.y);
- double biLen = Math.hypot(bisector.x, bisector.y);
-
- // 璁$畻鍗婅 sin(theta/2)
- double cross = n1.x * n2.y - n1.y * n2.x; // 鍙夌Н鍒ゆ柇杞悜
-
- // 榛樿鍚戝乏渚у唴缂� (CCW澶氳竟褰�)
- if (biLen < 1e-6) {
- // 鍏辩嚎锛屾部娉曠嚎鏂瑰悜
- bisector = new Point(-n1.y, n1.x);
- } else {
- bisector.x /= biLen;
- bisector.y /= biLen;
- }
-
- // 璁$畻鍋忕Щ璺濈
- double dot = n1.x * n2.x + n1.y * n2.y;
- double angle = Math.acos(Math.max(-1, Math.min(1, dot)));
- double dist = margin / Math.sin(angle / 2.0);
-
- // 鏂瑰悜淇锛氱‘淇濆钩鍒嗙嚎鎸囧悜澶氳竟褰㈠唴閮紙閫嗘椂閽堝杈瑰舰鐨勫乏渚э級
- Point leftNormal = new Point(-n1.y, n1.x);
- if (bisector.x * leftNormal.x + bisector.y * leftNormal.y < 0) {
- bisector.x = -bisector.x;
- bisector.y = -bisector.y;
- }
-
- // 濡傛灉鏄嚬瑙掞紙cross < 0锛夛紝骞冲垎绾挎寚鍚戝閮紝璺濈闇�瑕佸弽杞垨鑰呯壒娈婂鐞�
- // 绠�鍗曞鐞嗭細瀵逛簬鍑硅锛屽亸绉荤偣瀹為檯涓婁細杩滅鍘熺偣锛屼笂杩伴�昏緫閫氬父鑳借鐩栵紝
- // 浣嗘瀬绔攼瑙掑彲鑳藉鑷磀ist杩囧ぇ銆傛澶勫仛绠�鍗曟埅鏂繚鎶ゆ槸涓嶅鐨勶紝
- // 浣嗛拡瀵逛竴鑸崏鍦板舰鐘讹紝姝ら�昏緫鍙敤銆�
-
- result.add(new Point(pCurr.x + bisector.x * dist, pCurr.y + bisector.y * dist));
+ private static double findOptimalAngle(List<Point> polygon) {
+ double bestAngle = 0;
+ double minHeight = Double.MAX_VALUE;
+ for (int i = 0; i < polygon.size(); i++) {
+ Point p1 = polygon.get(i), p2 = polygon.get((i + 1) % polygon.size());
+ double angle = Math.atan2(p2.y - p1.y, p2.x - p1.x);
+ double h = calculateHeightAtAngle(polygon, angle);
+ if (h < minHeight) { minHeight = h; bestAngle = angle; }
}
- return result;
+ return bestAngle;
}
- /**
- * 鑰冲垏娉曞垎鍓插杈瑰舰
- */
- private static List<List<Point>> triangulatePolygon(List<Point> poly) {
- List<List<Point>> triangles = new ArrayList<>();
- List<Point> remaining = new ArrayList<>(poly);
-
- int maxIter = remaining.size() * 3;
- int iter = 0;
-
- while (remaining.size() > 3 && iter++ < maxIter) {
- int n = remaining.size();
- boolean earFound = false;
-
- for (int i = 0; i < n; i++) {
- Point prev = remaining.get((i - 1 + n) % n);
- Point curr = remaining.get(i);
- Point next = remaining.get((i + 1) % n);
-
- if (isConvex(prev, curr, next)) {
- boolean hasPoint = false;
- for (int j = 0; j < n; j++) {
- if (j == i || j == (i - 1 + n) % n || j == (i + 1) % n) continue;
- if (isPointInTriangle(remaining.get(j), prev, curr, next)) {
- hasPoint = true;
- break;
- }
- }
-
- if (!hasPoint) {
- List<Point> tri = new ArrayList<>();
- tri.add(prev); tri.add(curr); tri.add(next);
- triangles.add(tri);
- remaining.remove(i);
- earFound = true;
- break;
- }
- }
- }
- if (!earFound) break;
- }
-
- if (remaining.size() == 3) {
- triangles.add(remaining);
- }
- return triangles;
- }
-
- private static double calculatePolygonHeight(List<Point> poly, double angle) {
- double minY = Double.MAX_VALUE;
- double maxY = -Double.MAX_VALUE;
+ private static double calculateHeightAtAngle(List<Point> poly, double angle) {
+ double minY = Double.MAX_VALUE, maxY = -Double.MAX_VALUE;
for (Point p : poly) {
- Point r = rotatePoint(p, angle);
- minY = Math.min(minY, r.y);
- maxY = Math.max(maxY, r.y);
+ Point rp = rotatePoint(p, -angle);
+ minY = Math.min(minY, rp.y); maxY = Math.max(maxY, rp.y);
}
return maxY - minY;
}
- private static Point rotatePoint(Point p, double angle) {
- double cos = Math.cos(angle);
- double sin = Math.sin(angle);
- return new Point(p.x * cos - p.y * sin, p.x * sin + p.y * cos);
- }
-
- private static boolean isConvex(Point a, Point b, Point c) {
- return (b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x) >= 0;
- }
-
- private static boolean isPointInTriangle(Point p, Point a, Point b, Point c) {
- double areaABC = Math.abs((a.x*(b.y-c.y) + b.x*(c.y-a.y) + c.x*(a.y-b.y))/2.0);
- double areaPBC = Math.abs((p.x*(b.y-c.y) + b.x*(c.y-p.y) + c.x*(p.y-b.y))/2.0);
- double areaPAC = Math.abs((a.x*(p.y-c.y) + p.x*(c.y-a.y) + c.x*(a.y-p.y))/2.0);
- double areaPAB = Math.abs((a.x*(b.y-p.y) + b.x*(p.y-a.y) + p.x*(a.y-b.y))/2.0);
- return Math.abs(areaABC - (areaPBC + areaPAC + areaPAB)) < 1e-6;
- }
-
- private static List<Point> parseCoordinates(String coordinates) {
- List<Point> points = new ArrayList<>();
- String cleanStr = coordinates.replaceAll("[()\\[\\]{}]", "").trim();
- String[] pairs = cleanStr.split(";");
- for (String pair : pairs) {
- pair = pair.trim();
- if (pair.isEmpty()) continue;
- String[] xy = pair.split(",");
- if (xy.length == 2) {
- points.add(new Point(Double.parseDouble(xy[0].trim()), Double.parseDouble(xy[1].trim())));
- }
+ private static List<Point> getInsetPolygon(List<Point> points, double margin) {
+ List<Point> result = new ArrayList<>();
+ int n = points.size();
+ for (int i = 0; i < n; i++) {
+ Point pPrev = points.get((i - 1 + n) % n);
+ Point pCurr = points.get(i);
+ Point pNext = points.get((i + 1) % n);
+ double d1x = pCurr.x - pPrev.x, d1y = pCurr.y - pPrev.y;
+ double l1 = Math.hypot(d1x, d1y);
+ double d2x = pNext.x - pCurr.x, d2y = pNext.y - pCurr.y;
+ double l2 = Math.hypot(d2x, d2y);
+ if (l1 < 1e-6 || l2 < 1e-6) continue;
+ double n1x = -d1y / l1, n1y = d1x / l1;
+ double n2x = -d2y / l2, n2y = d2x / l2;
+ double bisectorX = n1x + n2x, bisectorY = n1y + n2y;
+ double bLen = Math.hypot(bisectorX, bisectorY);
+ if (bLen < 1e-6) { bisectorX = n1x; bisectorY = n1y; }
+ else { bisectorX /= bLen; bisectorY /= bLen; }
+ double cosHalfAngle = n1x * bisectorX + n1y * bisectorY;
+ double dist = margin / Math.max(cosHalfAngle, 0.1);
+ result.add(new Point(pCurr.x + bisectorX * dist, pCurr.y + bisectorY * dist));
}
- return points;
+ return result;
+ }
+
+ private static Point rotatePoint(Point p, double angle) {
+ double cos = Math.cos(angle), sin = Math.sin(angle);
+ return new Point(p.x * cos - p.y * sin, p.x * sin + p.y * cos);
}
private static void ensureCounterClockwise(List<Point> points) {
double sum = 0;
for (int i = 0; i < points.size(); i++) {
- Point p1 = points.get(i);
- Point p2 = points.get((i + 1) % points.size());
+ Point p1 = points.get(i), p2 = points.get((i + 1) % points.size());
sum += (p2.x - p1.x) * (p2.y + p1.y);
}
- if (sum > 0) {
- Collections.reverse(points);
- }
- }
-
- private static double distance(Point p1, Point p2) {
- return Math.hypot(p1.x - p2.x, p1.y - p2.y);
+ if (sum > 0) Collections.reverse(points);
}
- // ==========================================
- // 鍐呴儴鏁版嵁缁撴瀯
- // ==========================================
+ private static List<Point> parseCoordinates(String coordinates) {
+ List<Point> points = new ArrayList<>();
+ String[] pairs = coordinates.split(";");
+ for (String pair : pairs) {
+ String[] xy = pair.split(",");
+ if (xy.length == 2) points.add(new Point(Double.parseDouble(xy[0]), Double.parseDouble(xy[1])));
+ }
+ if (points.size() > 1 && points.get(0).equals(points.get(points.size()-1))) points.remove(points.size()-1);
+ return points;
+ }
public static class Point {
public double x, y;
public Point(double x, double y) { this.x = x; this.y = y; }
- @Override public String toString() { return String.format("%.2f,%.2f", x, 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;
+ }
}
public static class PathSegment {
- public Point start;
- public Point end;
+ public Point start, end;
public boolean isMowing;
-
- public PathSegment(Point start, Point end, boolean isMowing) {
- this.start = start;
- this.end = end;
- this.isMowing = isMowing;
- }
-
- @Override
- public String toString() {
- return String.format("[%s -> %s, 鍓茶崏:%b]", start, end, 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