Reputation: 107
I have multiple vertices drawn as Circle on a Pane. I would like to draw a graph, so I need to draw edges between Circles. I need to be able to draw multiple edges between vertices while keeping it clear. Until now, I was using Line, but if i draw 2 Line between the same vertices, we wont see them anymore. I also need to be able to draw an edge between a vertex and itself. Is there something i could use that would make this easier ? I thought about using a Path between each vertex and when adding a new vertex, using something different each time like an ArcTo or CurveTo etc... but it doesn't seem like a good idea, and i would like to have always the same link between vertices ( a line or slightly curved line ).
Upvotes: 0
Views: 304
Reputation: 82461
I don't see why using curves is an issue. Varying the parameters should yield distinct curves. For 2 different vertices quadratic curves should suffice, for connecting the vertex to itself you could use cubic curves.
// 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;
}
You may want of modify the start/end coordinates of the curves to be at the edge of the circle to achieve orthogonal tangents.
Upvotes: 2