张世豪
2025-12-01 d23f280e37080cb9b5934350cc0fafb2c68421d5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package zhuye; // 包声明
 
import java.awt.Color; // 颜色类
import java.awt.Font; // 字体类
import java.awt.FontMetrics; // 字体度量
import java.awt.Graphics2D; // 绘图上下文
import java.awt.geom.Ellipse2D; // 圆形绘制
import java.awt.geom.Point2D; // 二维坐标点
import java.util.List; // 列表接口
 
/**
 * Utility for rendering boundary vertices and numeric labels. // 工具类说明
 */
public final class pointandnumber { // 工具类定义
    private pointandnumber() { // 私有构造防止实例化
    }
 
    /**
     * Draw numbered boundary points with consistent styling.
     */
    public static void drawBoundaryPoints(Graphics2D g2d, // 绘制方法入口
                                          List<Point2D.Double> boundary, // 边界点集合
                                          double scale, // 缩放比例
                                          double mergeThreshold, // 合并阈值
                                          Color pointColor, // 点颜色
                                          Color labelColor) { // 序号颜色
        if (boundary == null || boundary.size() < 2) { // 判断数据是否有效
            return; // 无效直接返回
        }
 
        int totalPoints = boundary.size(); // 总点数量
        boolean closed = totalPoints > 2 && arePointsClose(boundary.get(0), boundary.get(totalPoints - 1), mergeThreshold); // 是否闭合
        int effectiveCount = closed ? totalPoints - 1 : totalPoints; // 有效点数量
        if (effectiveCount <= 0) { // 检查数量
            return; // 无效返回
        }
 
        double scaleFactor = Math.max(0.5, scale); // 防止过小缩放
        double markerDiameter = Math.max(1.0, (10.0 / scaleFactor) * 0.2); // 描点直径
        double markerRadius = markerDiameter / 2.0; // 半径
        int fontSize =2; // 字体大小
 
        Font originalFont = g2d.getFont(); // 保存原字体
        g2d.setFont(new Font("Times New Roman ", Font.CENTER_BASELINE, fontSize)); // 切换字体
        FontMetrics metrics = g2d.getFontMetrics(); // 字体度量
 
        for (int i = 0; i < effectiveCount; i++) { // 遍历有效点
            Point2D.Double point = boundary.get(i); // 当前点
            double x = point.x; // X坐标
            double y = point.y; // Y坐标
 
            g2d.setColor(pointColor); // 设置点颜色
            Ellipse2D marker = new Ellipse2D.Double(x - markerRadius, y - markerRadius, markerDiameter, markerDiameter); // 创建圆
            g2d.fill(marker); // 绘制圆
 
            String label = String.valueOf(i + 1); // 序号文本
            int textWidth = metrics.stringWidth(label); // 文本宽度
            g2d.setColor(labelColor); // 设置文本颜色
            float baselineX = (float) (x - textWidth / 2.0); // 计算X基线
            float baselineY = (float) (y + (metrics.getAscent() - metrics.getDescent()) / 2.0); // 计算Y基线
            g2d.drawString(label, baselineX, baselineY); // 绘制文本
        }
 
        g2d.setFont(originalFont); // 还原字体
    }
 
    private static boolean arePointsClose(Point2D.Double a, Point2D.Double b, double threshold) { // 点距离判断
        if (a == null || b == null) { // 判空
            return false; // 任一为空返回
        }
        double dx = a.x - b.x; // X差值
        double dy = a.y - b.y; // Y差值
        return Math.hypot(dx, dy) <= threshold; // 距离判断
    }
}