简体   繁体   English

意外结果绘制圆

[英]unexpected result drawing circle

I am trying to draw a circle that grows in size by drawing one pixel large radius circle ontop of the old and that way creating a growing circle on the pictureBox. 我试图通过在旧的顶部绘制一个像素大半径的圆来绘制一个尺寸逐渐增大的圆,并在pictureBox上创建一个逐渐增大的圆。

What i am seeing is a drop shaped figure instead of a circle. 我看到的是一个水滴状的图形,而不是一个圆形。 The code i use is: 我使用的代码是:

        for (int x = 0; x < 20; x++)
        {
            System.Drawing.Graphics graphics = box.CreateGraphics();
            System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(xpos-10, ypos-10, x, x);
            graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);

        }

What is it that i am missing? 我想念的是什么?

The rectangle x/y is the upper left corner of the rectangle containing the ellipse. 矩形x / y是包含椭圆的矩形的左上角。 If you draw larger circles, you need to move your bounding rectangle too. 如果绘制较大的圆,则也需要移动边界矩形。

        for (int x = 0; x < 20; x++)
        {
            System.Drawing.Graphics graphics = e.Graphics;
            System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(xpos - 10 - x / 2, ypos - 10 - x / 2, x, x);
            graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);

        }

If you want the circle to be iso-centric, you need to set the X and Y of the rectangle to the left as well. 如果要使圆以等心点为中心,则还需要将矩形的XY设置为左侧。

Thus: 从而:

Rectangle rectangle = new Rectangle(xpos-10-x/2, ypos-10-y/2, x, x);

On the other hand, you won't see the circle grow. 另一方面,您不会看到圆圈在扩大。 Since the PictureBox only swaps buffers after all drawing is done. 由于PictureBox完成所有绘制后才交换缓冲区。 What you need is a refresh event and use the time to determine the size of the next circle. 您需要的是刷新事件,并使用时间来确定下一个圆的大小。

You draw the circle inside the rectangle, when you only increase the hight and width of the rectangle, you also move the center to the right and the bottom. 您在矩形内部绘制圆,当您仅增加矩形的高度和宽度时,也将中心向右和向下移动。 Also, reuse the graphics for better performance 另外,重复使用图形以获得更好的性能

using (var graphics = box.CreateGraphics())
{

    for (int x = 0; x < 20; x++)
    {
        System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(xpos-10-x/2, ypos-10-x/2, x, x);
        graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM