Josephus87
Josephus87

Reputation: 1178

JavaFX draw partial line

I would like to draw a line between two moveable nodes. The line start and end coordinates are bound to the center points. But the nodes are transparent in the middle, so you see the line. The small dots inside the hat.

Dot within the hat is the line

The line should start and end not at the centers but say 25px (the radius) after the startNode center and 25px before the endNode center… how can this be done?

Upvotes: 0

Views: 68

Answers (1)

AlmasB
AlmasB

Reputation: 3407

Given start and end points, do the following:

This gives you the vector from start to end.

Point2D vector = new Point2D(end.x, end.y).substract(start.x, start.y);
vector = vector.normalize().multiply(25); // 25 is the radius here

This gives the point where you need to start your line

Point2D newStart = new Point2D(start.x, start.y).add(vector);

This gives the point where you need to end your line

Point2D newEnd = new Point2D(end.x, end.y).subtract(vector);

Upvotes: 4

Related Questions