I\'m drawing two shapes (circles) in a JPanel and I need to connect them with a line. I was doing this by just getting the middle point of the circle and connecting each oth
My trick:
Let the two centers be C0
and C1
. Using complex numbers, you map these two points to a horizontal segment from the origin by the transformation
P' = (P - C0) (C1 - C0)* / L
where *
denotes conjugation and L = |C1 - C0|
. (If you don't like the complex number notation, you can express this with matrices as well.)
Now the visible part of the segment goes from (R0, 0)
to (L - R1, 0)
. The two other vertices of the arrow are at (L - R1 - H, W)
and (L - R1 - H, -W)
for an arrowhead of height H
and width 2W
.
By applying the inverse transform you get the original coordinates,
P = C0 + L P' / (C1 - C0)*.