How to visually connect 2 circles?

后端 未结 3 858
长发绾君心
长发绾君心 2020-12-04 03:47

We know 2 circle\'s x and y center position, and the radius is the same. I want to visually connect the circles without looping the draw ellipse for each point on the line w

3条回答
  •  隐瞒了意图╮
    2020-12-04 04:23

    As the other answers so far slightly miss the correct solution, here is one that connects two circles of equal size:

    using (Pen pen = new Pen(Color.Blue, radius)
     { EndCap = LineCap.Round, StartCap = LineCap.Round }  )
         g.DrawLine(pen, x1, y1, x2, y2);
    

    Notes:

    • Usually is is good idea to set the smoothing mode of the graphics object to anti-alias..

    • To connect two circles of different sizes will take some math to calculate the four outer tangent points. From these one can get a polygon to fill or, if necessary one could create a GraphicsPath to fill, in case the color has an alpha < 1.

    • Jimi's comments point to a different solution that make use of GDI+ transformation capabilities.

    • Some of the answers or comments refer to the desired shape as an oval. While this ok in common speech, here, especially when geometry books are mentioned, this is wrong, as an oval will not have any straight lines.

    • As Jimi noted, what you call radius is really the diameter of the circles. I left the wrong term in the code but you should not!

提交回复
热议问题