From 69b40096cb0ae965f2a3e92672b880edfe7d04d2 Mon Sep 17 00:00:00 2001
From: 826220679@qq.com <826220679@qq.com>
Date: 星期六, 27 十二月 2025 21:14:09 +0800
Subject: [PATCH] 优化了登录页面
---
src/lujing/YixinglujingNoObstacle.java | 141 +++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 136 insertions(+), 5 deletions(-)
diff --git a/src/lujing/YixinglujingNoObstacle.java b/src/lujing/YixinglujingNoObstacle.java
index c38d7c1..fd879ad 100644
--- a/src/lujing/YixinglujingNoObstacle.java
+++ b/src/lujing/YixinglujingNoObstacle.java
@@ -1,6 +1,8 @@
package lujing;
import java.util.*;
+import java.util.Set;
+import java.util.HashSet;
/**
* 寮傚舰鑽夊湴璺緞瑙勫垝 - 鍑瑰杈瑰舰鍏煎浼樺寲鐗� V5.0
@@ -59,6 +61,14 @@
finalPath.addAll(scanPath);
+ // 8. 鏍煎紡鍖栧潗鏍囷細淇濈暀涓や綅灏忔暟
+ 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;
+ segment.end.x = Math.round(segment.end.x * 100.0) / 100.0;
+ segment.end.y = Math.round(segment.end.y * 100.0) / 100.0;
+ }
+
return finalPath;
}
@@ -100,7 +110,7 @@
for (PathSegment s : lineSegmentsInRow) {
if (Math.hypot(currentPos.x - s.start.x, currentPos.y - s.start.y) > 0.01) {
// 濡傛灉闂磋窛澶т簬1cm锛屾坊鍔犵┖璧拌矾寰�
- segments.add(new PathSegment(currentPos, s.start, false));
+ addSafeConnection(segments, currentPos, s.start, polygon);
}
segments.add(s);
currentPos = s.end;
@@ -139,12 +149,31 @@
private static List<Double> getXIntersections(List<Point> rotatedPoly, double y) {
List<Double> xIntersections = new ArrayList<>();
+ double tolerance = 1e-6;
+
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)) {
+
+ // 璺宠繃姘村钩杈癸紙閬垮厤涓庢壂鎻忕嚎閲嶅悎鏃剁殑鐗规畩鎯呭喌锛�
+ if (Math.abs(p1.y - p2.y) < tolerance) {
+ continue;
+ }
+
+ // 妫�鏌ユ槸鍚︾浉浜わ紙浣跨敤涓ユ牸涓嶇瓑寮忛伩鍏嶉《鐐归噸澶嶏級
+ 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);
+ // 绠�鍗曞幓閲嶏細妫�鏌ユ槸鍚﹀凡瀛樺湪鐩歌繎鐨勭偣
+ boolean isDuplicate = false;
+ for (double existingX : xIntersections) {
+ if (Math.abs(x - existingX) < tolerance) {
+ isDuplicate = true;
+ break;
+ }
+ }
+ if (!isDuplicate) {
+ xIntersections.add(x);
+ }
}
}
return xIntersections;
@@ -171,7 +200,7 @@
return maxY - minY;
}
- private static List<Point> getInsetPolygon(List<Point> points, double margin) {
+ public 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++) {
@@ -207,12 +236,114 @@
return result;
}
+ 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);
+ 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;
+
+ for (int i = 0; i < polygon.size(); i++) {
+ Point a = polygon.get(i);
+ Point b = polygon.get((i + 1) % polygon.size());
+ if (isSamePoint(p1, a) || isSamePoint(p1, b) || isSamePoint(p2, a) || isSamePoint(p2, b)) continue;
+ if (segmentsIntersect(p1, p2, a, b)) return false;
+ }
+ return true;
+ }
+
+ private static boolean isSamePoint(Point a, Point b) {
+ return Math.abs(a.x - b.x) < 1e-4 && Math.abs(a.y - b.y) < 1e-4;
+ }
+
+ private static boolean segmentsIntersect(Point a, Point b, Point c, Point d) {
+ return ccw(a, c, d) != ccw(b, c, d) && ccw(a, b, c) != ccw(a, b, d);
+ }
+
+ 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);
+ }
+
+ private static boolean isPointInPolygon(Point p, List<Point> polygon) {
+ boolean result = false;
+ for (int i = 0, j = polygon.size() - 1; i < polygon.size(); j = i++) {
+ if ((polygon.get(i).y > p.y) != (polygon.get(j).y > p.y) &&
+ (p.x < (polygon.get(j).x - polygon.get(i).x) * (p.y - polygon.get(i).y) / (polygon.get(j).y - polygon.get(i).y) + polygon.get(i).x)) {
+ result = !result;
+ }
+ }
+ return result;
+ }
+
+ private static List<Point> getBoundaryPath(Point start, Point end, List<Point> polygon) {
+ int idx1 = getEdgeIndex(start, polygon);
+ int idx2 = getEdgeIndex(end, polygon);
+
+ if (idx1 == -1 || idx2 == -1 || idx1 == idx2) {
+ return Arrays.asList(start, end);
+ }
+
+ List<Point> path1 = new ArrayList<>();
+ path1.add(start);
+ int curr = idx1;
+ while (curr != idx2) {
+ path1.add(polygon.get((curr + 1) % polygon.size()));
+ curr = (curr + 1) % polygon.size();
+ }
+ path1.add(end);
+
+ List<Point> pathRev = new ArrayList<>();
+ pathRev.add(start);
+ curr = idx1;
+ while (curr != idx2) {
+ pathRev.add(polygon.get(curr));
+ curr = (curr - 1 + polygon.size()) % polygon.size();
+ }
+ pathRev.add(polygon.get((idx2 + 1) % polygon.size()));
+ pathRev.add(end);
+
+ return getPathLength(path1) < getPathLength(pathRev) ? path1 : pathRev;
+ }
+
+ private static double getPathLength(List<Point> path) {
+ double len = 0;
+ for (int i = 0; i < path.size() - 1; i++) {
+ len += Math.hypot(path.get(i).x - path.get(i+1).x, path.get(i).y - path.get(i+1).y);
+ }
+ return len;
+ }
+
+ private static int getEdgeIndex(Point p, List<Point> poly) {
+ for (int i = 0; i < poly.size(); i++) {
+ Point p1 = poly.get(i);
+ Point p2 = poly.get((i + 1) % poly.size());
+ if (distToSegment(p, p1, p2) < 1e-3) return i;
+ }
+ return -1;
+ }
+
+ private static double distToSegment(Point p, Point s, Point e) {
+ double l2 = (s.x - e.x)*(s.x - e.x) + (s.y - e.y)*(s.y - e.y);
+ if (l2 == 0) return Math.hypot(p.x - s.x, p.y - s.y);
+ 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));
+ return Math.hypot(p.x - (s.x + t * (e.x - s.x)), p.y - (s.y + t * (e.y - s.y)));
+ }
+
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) {
+ public static void ensureCounterClockwise(List<Point> points) {
double sum = 0;
for (int i = 0; i < points.size(); i++) {
Point p1 = points.get(i), p2 = points.get((i + 1) % points.size());
--
Gitblit v1.10.0