如何直观地连接2个圈子? [英] How to visually connect 2 circles?

查看:129
本文介绍了如何直观地连接2个圈子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道2个圆的x和y中心位置,并且半径相同。我想以视觉方式连接圆,而不会在直线上的每个点处使绘制椭圆环成为2个圆的中心。

We know 2 circle' 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 what connect's the 2 circle's center.

从这里开始:

代码:

int radius = 75;

int x1 = 100;
int y1 = 200;

int x2 = 300;
int y2 = 100;

g.FillEllipse(Brushes.Blue, new Rectangle(x1 - radius / 2, y1 - radius / 2, radius, radius));
g.FillEllipse(Brushes.Blue, new Rectangle(x2 - radius / 2, y2 - radius / 2, radius, radius));


推荐答案

到目前为止,其他答案略微错过了正确的解决方案,这是一种将两个相同大小的圆连接起来的方法:

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);

注意:


  • 通常最好将图形对象的平滑模式设置为反别名。

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

要连接两个不同大小的圆,将需要一些数学运算才能计算出四个外部切线点。从中可以得到一个多边形来填充,或者,如果颜色的alpha值小于< ;,则可以创建一个 GraphicsPath 来填充。 1.

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的评论指向使用GDI +转换功能的另一种解决方案。

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.

就像Jimi注意,您所说的 radius 实际上是圆的直径。我在代码中输入了错误的术语,但您不应该

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!

这篇关于如何直观地连接2个圈子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆