我有多个顶点作为圆环绘制在一个窗格上。我想画一个图,所以我需要画圆之间的边。我需要能够绘制多个边之间的顶点,同时保持它的清晰。直到现在,我使用的是连,但如果我画2条线之间的相同的顶点,我们将不会再看到他们。我还需要能够在顶点和它本身之间画一条边。我能用点什么让事情变得更简单吗?我考虑在每个顶点之间使用一条路径,当添加一个新的顶点时,每次使用一些不同的东西,比如ArcTo或CurveTo等等。但这似乎不是一个好主意,我希望在顶点(一条线或稍弯曲的线)之间始终保持相同的联系。
发布于 2018-04-22 04:15:38
我不明白为什么使用曲线是个问题。改变参数会产生不同的曲线。对于两个不同的顶点二次曲线应该足够了,对于连接顶点到它自己,你可以使用三次曲线。
// create connections between independent circles
private static Pane createPaths(double x1, double y1, double x2, double y2, int lineCount) {
Circle circle1 = new Circle(x1, y1, 5, Color.DODGERBLUE);
Circle circle2 = new Circle(x2, y2, 5, Color.DODGERBLUE);
double mx = (x1 + x2) / 2;
double my = (y1 + y2) / 2;
double dnX = y2 - y1;
double dnY = x1 - x2;
// normalize ortogonal
double length = Math.hypot(dnX, dnY);
dnX /= length;
dnY /= length;
Pane pane = new Pane();
for (int i = 0; i < lineCount; i++) {
double factor = calculateControlPointDistance(i, lineCount, length);
Path path = new Path(new MoveTo(x1, y1),
new QuadCurveTo(mx + factor * dnX, my + factor * dnY, x2, y2));
pane.getChildren().add(path);
}
pane.getChildren().addAll(circle1, circle2);
return pane;
}
private static final double DISTANCE = 100;
// connect circle to itself
private Pane createCycle(double x, double y, int count) {
Pane pane = new Pane();
final double angleStep = (count == 1 ? Math.PI / 2 : Math.PI / count);
for (int i = 0; i < count; i++) {
double angle = 2 * i * angleStep;
Path path = new Path(new MoveTo(x, y),
new CubicCurveTo(
x + Math.sin(angle) * DISTANCE,
y + Math.cos(angle) * DISTANCE,
x + Math.sin(angle + angleStep) * DISTANCE,
y + Math.cos(angle + angleStep) * DISTANCE,
x,
y));
pane.getChildren().add(path);
}
pane.getChildren().add(new Circle(x, y, 5, Color.DODGERBLUE));
return pane;
}
您可能需要修改曲线的起始/结束坐标,使其位于圆的边缘,以实现正交切线。
https://stackoverflow.com/questions/49961457
复制