c# 绘图库计算绘制和圆的范围和中点
c# drawing library calculate ranges and midpoints to draw and circle
基本上我有一个菜单,用户可以选择要绘制的形状,然后用户单击两点,在这两个点之间绘制所选形状。
我做了这样计算的平方
// calculate ranges and mid points
xDiff = oppPt.X - keyPt.X;
yDiff = oppPt.Y - keyPt.Y;
xMid = (oppPt.X + keyPt.X) / 2;
yMid = (oppPt.Y + keyPt.Y) / 2;
// draw square
g.DrawLine(blackPen, (int)keyPt.X, (int)keyPt.Y,
(int)(xMid + yDiff / 2), (int)(yMid - xDiff / 2));
g.DrawLine(blackPen, (int)(xMid + yDiff / 2), (int)(yMid - xDiff / 2),
(int)oppPt.X, (int)oppPt.Y);
g.DrawLine(blackPen, (int)oppPt.X, (int)oppPt.Y,
(int)(xMid - yDiff / 2), (int)(yMid + xDiff / 2));
g.DrawLine(blackPen, (int)(xMid - yDiff / 2),
(int)(yMid + xDiff / 2), (int)keyPt.X, (int)keyPt.Y);
但是我不知道怎么用同样的方法画圆和三角形
请指教,谢谢
要画一个圆,使用g.DrawEllipse
,这里有一个很好的扩展方法:
public static void DrawCircle(Graphics g, Pen pen, float centerX, float centerY, float radius)
{
g.DrawEllipse(pen, centerX - radius, centerY - radius, radius + radius, radius + radius);
}
要绘制填充,只需将 DrawEllipse
更改为 FillElipse
并将 Pen
更改为 Brush
:
public static void DrawCircleFilled(Graphics g, Brush brush, float centerX, float centerY, float radius)
{
g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius);
}
如果您坚持只用线条绘制形状,请不要害怕。一个三角形就是简单的用三条线画出来,这很容易所以我就不写了。至于圆:
圆是由若干段组成的,段越多,圆的质量越好。您可以从一个简单的三角形开始,然后再添加一条线,得到一个正方形,然后是五边形、六边形等等。每条线的形状在视觉上更像一个圆。
如您所见,每条线覆盖总角度的 (360 / N)。所以你可以使用一个for循环来绘制所有线段(线)。
同路。
int left = 20, top = 20
int right = 100, bot = 100;
// triangle
g.DrawLine(Pens.Red, left, bot, right, bot);
g.DrawLine(Pens.Red, right, bot, left + (right-left) / 2 /* was miss calc */, top);
g.DrawLine(Pens.Red, left + (right - left) / 2, top, left, bot); // better looks
//g.DrawLine(Pens.Red, left, bot, left + (right-left) / 2 /* was miss calc */, top);
// circle
int len = (right - left) / 2;
int centerX = left + (right - left) / 2, centerY = top + (bot - top) / 2;
for (int i = 0; i <= 360; i++)
{
int degrees = i;
double radians = degrees * (Math.PI / 180);
int x = centerX + (int)(len * Math.Cos(radians));
int y = centerY + (int)(len * Math.Sin(radians));
e.Graphics.FillRectangle(Brushes.Red, x, y, 1, 1); // single pixel drawing
}
但是椭圆比较难
http://www.petercollingridge.co.uk/tutorials/computational-geometry/finding-angle-around-ellipse/
上部 link 对椭圆有帮助
如果用两点((x1,y1),(x2,y2))指定圆的直径,
然后使用 DrawEllipse
和 x,y,w,h
x = cx-r
y = cy-r
d = w = h = sqrt((x2-x1)^2+(y2-y1)^2)
哪里
cx = |x2-x1|/2
cy = |y2-y1|/2
r = d/2
对于三角形,你确实需要三个点。您可以将其限制为两个点,就像指定直角三角形的斜边一样。这仍然不是足够的信息,因为有两个直角三角形可能有斜边,上边和下边。你想要什么样的三角形?右、等、锐、等边、钝?
基本上我有一个菜单,用户可以选择要绘制的形状,然后用户单击两点,在这两个点之间绘制所选形状。
我做了这样计算的平方
// calculate ranges and mid points
xDiff = oppPt.X - keyPt.X;
yDiff = oppPt.Y - keyPt.Y;
xMid = (oppPt.X + keyPt.X) / 2;
yMid = (oppPt.Y + keyPt.Y) / 2;
// draw square
g.DrawLine(blackPen, (int)keyPt.X, (int)keyPt.Y,
(int)(xMid + yDiff / 2), (int)(yMid - xDiff / 2));
g.DrawLine(blackPen, (int)(xMid + yDiff / 2), (int)(yMid - xDiff / 2),
(int)oppPt.X, (int)oppPt.Y);
g.DrawLine(blackPen, (int)oppPt.X, (int)oppPt.Y,
(int)(xMid - yDiff / 2), (int)(yMid + xDiff / 2));
g.DrawLine(blackPen, (int)(xMid - yDiff / 2),
(int)(yMid + xDiff / 2), (int)keyPt.X, (int)keyPt.Y);
但是我不知道怎么用同样的方法画圆和三角形
请指教,谢谢
要画一个圆,使用g.DrawEllipse
,这里有一个很好的扩展方法:
public static void DrawCircle(Graphics g, Pen pen, float centerX, float centerY, float radius)
{
g.DrawEllipse(pen, centerX - radius, centerY - radius, radius + radius, radius + radius);
}
要绘制填充,只需将 DrawEllipse
更改为 FillElipse
并将 Pen
更改为 Brush
:
public static void DrawCircleFilled(Graphics g, Brush brush, float centerX, float centerY, float radius)
{
g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius);
}
如果您坚持只用线条绘制形状,请不要害怕。一个三角形就是简单的用三条线画出来,这很容易所以我就不写了。至于圆:
圆是由若干段组成的,段越多,圆的质量越好。您可以从一个简单的三角形开始,然后再添加一条线,得到一个正方形,然后是五边形、六边形等等。每条线的形状在视觉上更像一个圆。
如您所见,每条线覆盖总角度的 (360 / N)。所以你可以使用一个for循环来绘制所有线段(线)。
同路。
int left = 20, top = 20
int right = 100, bot = 100;
// triangle
g.DrawLine(Pens.Red, left, bot, right, bot);
g.DrawLine(Pens.Red, right, bot, left + (right-left) / 2 /* was miss calc */, top);
g.DrawLine(Pens.Red, left + (right - left) / 2, top, left, bot); // better looks
//g.DrawLine(Pens.Red, left, bot, left + (right-left) / 2 /* was miss calc */, top);
// circle
int len = (right - left) / 2;
int centerX = left + (right - left) / 2, centerY = top + (bot - top) / 2;
for (int i = 0; i <= 360; i++)
{
int degrees = i;
double radians = degrees * (Math.PI / 180);
int x = centerX + (int)(len * Math.Cos(radians));
int y = centerY + (int)(len * Math.Sin(radians));
e.Graphics.FillRectangle(Brushes.Red, x, y, 1, 1); // single pixel drawing
}
但是椭圆比较难
http://www.petercollingridge.co.uk/tutorials/computational-geometry/finding-angle-around-ellipse/
上部 link 对椭圆有帮助
如果用两点((x1,y1),(x2,y2))指定圆的直径,
然后使用 DrawEllipse
和 x,y,w,h
x = cx-r
y = cy-r
d = w = h = sqrt((x2-x1)^2+(y2-y1)^2)
哪里
cx = |x2-x1|/2
cy = |y2-y1|/2
r = d/2
对于三角形,你确实需要三个点。您可以将其限制为两个点,就像指定直角三角形的斜边一样。这仍然不是足够的信息,因为有两个直角三角形可能有斜边,上边和下边。你想要什么样的三角形?右、等、锐、等边、钝?