| | |
| | | return; |
| | | } |
| | | |
| | | // 计算标签位置(中心点) |
| | | double centerX = 0; |
| | | double centerY = 0; |
| | | |
| | | for (Obstacledge.XYCoordinate coord : xyCoords) { |
| | | centerX += coord.getX(); |
| | | centerY += coord.getY(); |
| | | double centerX; |
| | | double centerY; |
| | | |
| | | Obstacledge.ObstacleShape shape = obstacle.getShape(); |
| | | if (shape == Obstacledge.ObstacleShape.CIRCLE) { |
| | | Obstacledge.XYCoordinate centerCoord = xyCoords.get(0); |
| | | centerX = centerCoord.getX(); |
| | | centerY = centerCoord.getY(); |
| | | } else { |
| | | Point2D.Double centroid = computePolygonCentroid(xyCoords); |
| | | centerX = centroid.x; |
| | | centerY = centroid.y; |
| | | } |
| | | |
| | | centerX /= xyCoords.size(); |
| | | centerY /= xyCoords.size(); |
| | | |
| | | // 获取障碍物名称和形状 |
| | | // 获取障碍物名称 |
| | | String obstacleName = obstacle.getObstacleName(); |
| | | String shapeDesc = obstacle.getShape().getDescription(); |
| | | if (obstacleName == null || obstacleName.trim().isEmpty()) { |
| | | obstacleName = "障碍物"; |
| | | } else { |
| | | obstacleName = obstacleName.trim(); |
| | | } |
| | | |
| | | // 设置字体和颜色 |
| | | g2d.setColor(OBSTACLE_LABEL_COLOR); |
| | |
| | | g2d.setFont(new Font("微软雅黑", Font.PLAIN, fontSize)); |
| | | |
| | | // 绘制标签 |
| | | String label = String.format("%s(%s)", obstacleName, shapeDesc); |
| | | String label = obstacleName; |
| | | FontMetrics metrics = g2d.getFontMetrics(); |
| | | int textWidth = metrics.stringWidth(label); |
| | | int textHeight = metrics.getHeight(); |
| | |
| | | |
| | | g2d.drawString(label, textX, textY); |
| | | } |
| | | |
| | | private static Point2D.Double computePolygonCentroid(List<Obstacledge.XYCoordinate> xyCoords) { |
| | | double area = 0.0; |
| | | double cx = 0.0; |
| | | double cy = 0.0; |
| | | int n = xyCoords.size(); |
| | | for (int i = 0; i < n; i++) { |
| | | Obstacledge.XYCoordinate current = xyCoords.get(i); |
| | | Obstacledge.XYCoordinate next = xyCoords.get((i + 1) % n); |
| | | double x0 = current.getX(); |
| | | double y0 = current.getY(); |
| | | double x1 = next.getX(); |
| | | double y1 = next.getY(); |
| | | double cross = x0 * y1 - x1 * y0; |
| | | area += cross; |
| | | cx += (x0 + x1) * cross; |
| | | cy += (y0 + y1) * cross; |
| | | } |
| | | |
| | | double areaFactor = area * 0.5; |
| | | if (Math.abs(areaFactor) < 1e-9) { |
| | | double avgX = 0.0; |
| | | double avgY = 0.0; |
| | | for (Obstacledge.XYCoordinate coord : xyCoords) { |
| | | avgX += coord.getX(); |
| | | avgY += coord.getY(); |
| | | } |
| | | int size = Math.max(1, xyCoords.size()); |
| | | return new Point2D.Double(avgX / size, avgY / size); |
| | | } |
| | | |
| | | double centroidX = cx / (6.0 * areaFactor); |
| | | double centroidY = cy / (6.0 * areaFactor); |
| | | return new Point2D.Double(centroidX, centroidY); |
| | | } |
| | | |
| | | /** |
| | | * 检查点是否在障碍物内 |