张世豪
6 天以前 5d6d890cfd10466d5d14ff5177adcc888baaa0e4
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
package zhuye; // 包声明
 
import java.awt.Color; // 颜色类
import java.awt.Graphics2D; // 绘图上下文
import java.awt.geom.Ellipse2D; // 圆形绘制
import java.awt.geom.Point2D; // 二维坐标点
import java.util.List; // 列表接口
 
/**
 * Utility for rendering boundary vertices. // 工具类说明
 */
public final class pointandnumber { // 工具类定义
    private pointandnumber() { // 私有构造防止实例化
    }
 
    /**
     * Draw boundary points with consistent styling.
     */
    public static void drawBoundaryPoints(Graphics2D g2d, // 绘制方法入口
                                          List<Point2D.Double> boundary, // 边界点集合
                                          double scale, // 缩放比例
                                          double mergeThreshold, // 合并阈值
                                          Color pointColor, // 点颜色
                                          double diameterScale) { // 直径缩放因子
        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); // 防止过小缩放
    // 边界点直径与边界线宽度一致:3 / Math.max(0.5, scale)
    double markerDiameter = 3.0 / scaleFactor; // 描点直径(与边界线宽度一致)
        double markerRadius = markerDiameter / 2.0; // 半径
 
        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); // 绘制圆
        }
    }
 
    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; // 距离判断
    }
}