From 1cf1ecbc75c6d14b40efb3161e7db0b8b64f7de2 Mon Sep 17 00:00:00 2001
From: 张世豪 <979909237@qq.com>
Date: 星期三, 17 十二月 2025 12:05:27 +0800
Subject: [PATCH] 新增有障碍物的路径规划算法和优化没有障碍物的路径算法
---
src/zhangaiwu/Obstacledraw.java | 63 +++++++++++++++++++++----------
1 files changed, 42 insertions(+), 21 deletions(-)
diff --git a/src/zhangaiwu/Obstacledraw.java b/src/zhangaiwu/Obstacledraw.java
index e5d3223..3125dc7 100644
--- a/src/zhangaiwu/Obstacledraw.java
+++ b/src/zhangaiwu/Obstacledraw.java
@@ -10,21 +10,28 @@
/**
* 闅滅鐗╃粯鍒剁被 - 璐熻矗缁樺埗鍦板潡涓殑闅滅鐗�
+ *
+ * 娉ㄦ剰锛氶殰纰嶇墿鍥惧眰闇�瑕佸浜庡湴鍧楀拰瀵艰埅璺緞涓婃柟銆�
+ * 鍦� MapRenderer.renderMap() 涓紝缁樺埗椤哄簭搴斾负锛�
+ * 1. 鍦板潡杈圭晫锛堝簳灞傦級
+ * 2. 瀵艰埅璺緞锛堜腑灞傦級
+ * 3. 闅滅鐗╋紙椤跺眰锛屾樉绀哄湪鍦板潡鍜屽鑸矾寰勪笂鏂癸級
*/
public class Obstacledraw {
// 棰滆壊瀹氫箟
- private static final Color CIRCLE_FILL_COLOR = new Color(255, 182, 193, 120); // 鍦嗗舰濉厖鑹� - 娴呯矇绾�
+ private static final Color CIRCLE_FILL_COLOR = new Color(128, 128, 128, 128); // 鍦嗗舰濉厖鑹� - 鐏拌壊锛岄�忔槑搴�50%
private static final Color CIRCLE_BORDER_COLOR = new Color(199, 21, 133); // 鍦嗗舰杈规鑹� - 娣辩矇绾�
- private static final Color POLYGON_FILL_COLOR = new Color(173, 216, 230, 120); // 澶氳竟褰㈠~鍏呰壊 - 娴呰摑
+ private static final Color POLYGON_FILL_COLOR = new Color(128, 128, 128, 128); // 澶氳竟褰㈠~鍏呰壊 - 鐏拌壊锛岄�忔槑搴�50%
private static final Color POLYGON_BORDER_COLOR = new Color(25, 25, 112); // 澶氳竟褰㈣竟妗嗚壊 - 娣辫摑
private static final Color OBSTACLE_LABEL_COLOR = Color.BLACK;
private static final Color OBSTACLE_POINT_COLOR = Color.RED;
// 灏哄瀹氫箟
private static final double OBSTACLE_POINT_SIZE = 0.1; // 闅滅鐗╂帶鍒剁偣澶у皬锛堢背锛�
- private static final float DEFAULT_BORDER_WIDTH = 1.0f;
- private static final float SELECTED_BORDER_WIDTH = 2.5f;
+ // 杈圭晫绾垮搴︿笌鍦板潡杈圭晫绾垮搴︿竴鑷达細3 / Math.max(0.5, scale)
+ private static final float BOUNDARY_STROKE_BASE = 3.0f; // 涓庡湴鍧楄竟鐣岀嚎瀹藉害涓�鑷�
+ private static final float SELECTED_WIDTH_MULTIPLIER = 1.5f; // 閫変腑鏃剁殑瀹藉害鍊嶆暟
/**
* 缁樺埗鍦板潡鐨勬墍鏈夐殰纰嶇墿
@@ -109,13 +116,15 @@
g2d.setColor(CIRCLE_FILL_COLOR);
g2d.fill(circle);
- // 璁剧疆杈规棰滆壊鍜屽搴�
+ // 璁剧疆杈规棰滆壊鍜屽搴︼紙涓庡湴鍧楄竟鐣岀嚎瀹藉害涓�鑷达級
+ float strokeWidth = (float)(BOUNDARY_STROKE_BASE / Math.max(0.5, scale));
if (isSelected) {
g2d.setColor(CIRCLE_BORDER_COLOR.darker());
- g2d.setStroke(new BasicStroke(SELECTED_BORDER_WIDTH / (float)scale));
+ // 閫変腑鏃剁◢寰姞绮�
+ g2d.setStroke(new BasicStroke(strokeWidth * SELECTED_WIDTH_MULTIPLIER));
} else {
g2d.setColor(CIRCLE_BORDER_COLOR);
- g2d.setStroke(new BasicStroke(DEFAULT_BORDER_WIDTH / (float)scale));
+ g2d.setStroke(new BasicStroke(strokeWidth));
}
g2d.draw(circle);
@@ -153,13 +162,15 @@
g2d.setColor(POLYGON_FILL_COLOR);
g2d.fill(polygon);
- // 璁剧疆杈规棰滆壊鍜屽搴�
+ // 璁剧疆杈规棰滆壊鍜屽搴︼紙涓庡湴鍧楄竟鐣岀嚎瀹藉害涓�鑷达級
+ float strokeWidth = (float)(BOUNDARY_STROKE_BASE / Math.max(0.5, scale));
if (isSelected) {
g2d.setColor(POLYGON_BORDER_COLOR.darker());
- g2d.setStroke(new BasicStroke(SELECTED_BORDER_WIDTH / (float)scale));
+ // 閫変腑鏃剁◢寰姞绮�
+ g2d.setStroke(new BasicStroke(strokeWidth * SELECTED_WIDTH_MULTIPLIER));
} else {
g2d.setColor(POLYGON_BORDER_COLOR);
- g2d.setStroke(new BasicStroke(DEFAULT_BORDER_WIDTH / (float)scale));
+ g2d.setStroke(new BasicStroke(strokeWidth));
}
g2d.draw(polygon);
@@ -202,6 +213,7 @@
/**
* 缁樺埗闅滅鐗╂爣绛�
+ * 鏂囧瓧澶у皬涓�"缂╂斁"鏂囧瓧涓�鑷达紙11鍙峰瓧浣擄級锛屼笖涓嶉殢鍦板浘缂╂斁鍙樺寲
*/
private static void drawObstacleLabel(Graphics2D g2d, Obstacledge.Obstacle obstacle,
double scale) {
@@ -210,6 +222,9 @@
return;
}
+ // 淇濆瓨褰撳墠鍙樻崲
+ AffineTransform originalTransform = g2d.getTransform();
+
double centerX;
double centerY;
@@ -224,6 +239,14 @@
centerY = centroid.y;
}
+ // 灏嗕笘鐣屽潗鏍囪浆鎹负灞忓箷鍧愭爣
+ Point2D.Double worldPoint = new Point2D.Double(centerX, centerY);
+ Point2D.Double screenPoint = new Point2D.Double();
+ originalTransform.transform(worldPoint, screenPoint);
+
+ // 鎭㈠鍘熷鍙樻崲浠ヤ娇鐢ㄥ睆骞曞潗鏍囩粯鍒�
+ g2d.setTransform(new AffineTransform());
+
// 鑾峰彇闅滅鐗╁悕绉�
String obstacleName = obstacle.getObstacleName();
if (obstacleName == null || obstacleName.trim().isEmpty()) {
@@ -232,26 +255,24 @@
obstacleName = obstacleName.trim();
}
- // 璁剧疆瀛椾綋鍜岄鑹�
+ // 璁剧疆瀛椾綋鍜岄鑹诧紙涓�"缂╂斁"鏂囧瓧涓�鑷达級
g2d.setColor(OBSTACLE_LABEL_COLOR);
-
- // 鏍规嵁缂╂斁姣斾緥璋冩暣瀛椾綋澶у皬
- int fontSize = (int)(10 / scale);
- fontSize = Math.max(8, Math.min(fontSize, 14)); // 闄愬埗瀛椾綋澶у皬鑼冨洿
-
- g2d.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, fontSize));
+ g2d.setFont(new Font("寰蒋闆呴粦", Font.PLAIN, 11)); // 涓�"缂╂斁"鏂囧瓧澶у皬涓�鑷�
// 缁樺埗鏍囩
- String label = obstacleName;
+ String label = obstacleName;
FontMetrics metrics = g2d.getFontMetrics();
int textWidth = metrics.stringWidth(label);
int textHeight = metrics.getHeight();
- // 鍦ㄤ腑蹇冪偣缁樺埗鏍囩
- int textX = (int)(centerX - textWidth / 2.0);
- int textY = (int)(centerY + textHeight / 4.0); // 绋嶅井鍚戜笅鍋忕Щ
+ // 鍦ㄥ睆骞曞潗鏍囦腑蹇冪偣缁樺埗鏍囩
+ int textX = (int)(screenPoint.x - textWidth / 2.0);
+ int textY = (int)(screenPoint.y + textHeight / 4.0); // 绋嶅井鍚戜笅鍋忕Щ
g2d.drawString(label, textX, textY);
+
+ // 鎭㈠鍘熷鍙樻崲
+ g2d.setTransform(originalTransform);
}
private static Point2D.Double computePolygonCentroid(List<Obstacledge.XYCoordinate> xyCoords) {
--
Gitblit v1.10.0