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)
|
// 边界点直径 = 边界线宽度的2倍
|
double boundaryLineWidth = 3.0 / scaleFactor; // 边界线宽度
|
double markerDiameter = boundaryLineWidth * 2.0; // 描点直径(边界线宽度的2倍)
|
// 应用直径缩放因子
|
if (diameterScale > 0.0 && Double.isFinite(diameterScale)) {
|
markerDiameter *= diameterScale;
|
}
|
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; // 距离判断
|
}
|
}
|