Connect two circles with a line

前端 未结 4 900
名媛妹妹
名媛妹妹 2020-12-21 13:35

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

4条回答
  •  春和景丽
    2020-12-21 14:09

    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)*.
    

提交回复
热议问题