我制作的代码很难编码,我希望它将其转换为圆圈,我可以添加的任何片段
代码在c尖锐,输出就像矩形,我必须将其转换为圆
private void pictureBox1_Click(object sender, EventArgs e)
{
int length = 100;
int flag = 0;
int flag2 = 0;
int flag3 = 0;
Pen p = new Pen(Color.Red, 4);
Graphics g = pictureBox1.CreateGraphics();
Brush redBrush = new SolidBrush(Color.Red);
for (int i = 0; i < 20; i++)
{
if(i==0 || i<10)
{
g.DrawLine(p, 622 - 10 * i, 229+10*i, 623 - 10 * i, 229+10*i);
}
if(i==10)
{
flag = 1;
}
if(flag==1)
{
g.DrawLine(p, 622 - 10 * i, 419 - 10 * i, 623 - 10 * i, 419-10*i);
flag2 = 1;
}
if(flag2 == 1)
{
g.DrawLine(p, 622 - 10 * i, 29+10*i, 623 - 10 * i, 29+10*i);
flag3 = 1;
}
if (flag3 == 1)
{
g.DrawLine(p, 432 + 10 * i, 29+10*i, 433 + 10 * i, 29 + 10 *i);
}
}
有一个内置功能。使用G.Drawellipse()而不是。
您可以做这个
void DrawCircle(Graphics g, Pen p, Point centre, double radius=20, int sides = 360)
{
var angle = 2 * Math.PI / sides;
for (int i = 0; i < sides; i++)
{
Point from = new Point((int)(radius * Math.Sin(i * angle) + centre.X), (int)(radius * Math.Cos(i * angle) + centre.Y));
Point to = new Point((int)(radius * Math.Sin((i+1) * angle) + centre.X), (int)(radius * Math.Cos((i+1) * angle) + centre.Y));
g.DrawLine(p, from, to);
}
}
并使用
DrawCircle(g, p, new Point(100, 100), 50, 8); // 8 sides, an octagon
增加边的数量以使其更准确。
或者,
g.DrawEllipse(p, (float)(centre.X-radius), (float)(centre.Y-radius), (float)radius*2, (float)radius*2);