15

中点圆算法可用于光栅化圆的边界。但是,我希望填充圆圈,而不是多次绘制像素(这非常重要)。

这个答案提供了产生一个实心圆的算法的修改,但是一些像素被访问了几次: 绘制实心圆的快速算法?

问:如何在不多次绘制像素的情况下栅格化一个圆?请注意,RAM 非常有限!

更新:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CircleTest
{
    class Program
    {
        static void Main(string[] args)
        {
            byte[,] buffer = new byte[50, 50];
            circle(buffer, 25, 25, 20);

            for (int y = 0; y < 50; ++y)
            {
                for (int x = 0; x < 50; ++x)
                    Console.Write(buffer[y, x].ToString());

                Console.WriteLine();
            }
        }

        // 'cx' and 'cy' denote the offset of the circle center from the origin.
        static void circle(byte[,] buffer, int cx, int cy, int radius)
        {
            int error = -radius;
            int x = radius;
            int y = 0;

            // The following while loop may altered to 'while (x > y)' for a
            // performance benefit, as long as a call to 'plot4points' follows
            // the body of the loop. This allows for the elimination of the
            // '(x != y)' test in 'plot8points', providing a further benefit.
            //
            // For the sake of clarity, this is not shown here.
            while (x >= y)
            {
                plot8points(buffer, cx, cy, x, y);

                error += y;
                ++y;
                error += y;

                // The following test may be implemented in assembly language in
                // most machines by testing the carry flag after adding 'y' to
                // the value of 'error' in the previous step, since 'error'
                // nominally has a negative value.
                if (error >= 0)
                {
                    error -= x;
                    --x;
                    error -= x;
                }
            }
        }

        static void plot8points(byte[,] buffer, int cx, int cy, int x, int y)
        {
            plot4points(buffer, cx, cy, x, y);
            if (x != y) plot4points(buffer, cx, cy, y, x);
        }

        // The '(x != 0 && y != 0)' test in the last line of this function
        // may be omitted for a performance benefit if the radius of the
        // circle is known to be non-zero.
        static void plot4points(byte[,] buffer, int cx, int cy, int x, int y)
        {
#if false // Outlined circle are indeed plotted correctly!
            setPixel(buffer, cx + x, cy + y);
            if (x != 0) setPixel(buffer, cx - x, cy + y);
            if (y != 0) setPixel(buffer, cx + x, cy - y);
            if (x != 0 && y != 0) setPixel(buffer, cx - x, cy - y);
#else // But the filled version plots some pixels multiple times...
            horizontalLine(buffer, cx - x, cy + y, cx + x);
            //if (x != 0) setPixel(buffer, cx - x, cy + y);
            //if (y != 0) setPixel(buffer, cx + x, cy - y);
            //if (x != 0 && y != 0) setPixel(buffer, cx - x, cy - y);
#endif
        }

        static void setPixel(byte[,] buffer, int x, int y)
        {
            buffer[y, x]++;
        }

        static void horizontalLine(byte[,] buffer, int x0, int y0, int x1)
        {
            for (int x = x0; x <= x1; ++x)
                setPixel(buffer, x, y0);
        }
    }
}

以下是相关结果:

00000111111111111111111111111111111111111111110000
00000111111111111111111111111111111111111111110000
00000111111111111111111111111111111111111111110000
00000111111111111111111111111111111111111111110000
00000111111111111111111111111111111111111111110000
00000011111111111111111111111111111111111111100000
00000011111111111111111111111111111111111111100000
00000011111111111111111111111111111111111111100000
00000001111111111111111111111111111111111111000000
00000001111111111111111111111111111111111111000000
00000000111111111111111111111111111111111110000000
00000000111111111111111111111111111111111110000000
00000000011111111111111111111111111111111100000000
00000000001111111111111111111111111111111000000000
00000000000111111111111111111111111111110000000000
00000000000011111111111111111111111111100000000000
00000000000001111111111111111111111111000000000000
00000000000000122222222222222222222210000000000000
00000000000000001222222222222222221000000000000000
00000000000000000012333333333332100000000000000000
00000000000000000000012345432100000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000

底部像素被绘制了太多次。我在这里想念什么?

更新 #2:此解决方案有效:

static void circle(byte[,] buffer, int cx, int cy, int radius)
{
    int error = -radius;
    int x = radius;
    int y = 0;

    while (x >= y)
    {
        int lastY = y;

        error += y;
        ++y;
        error += y;

        plot4points(buffer, cx, cy, x, lastY);

        if (error >= 0)
        {
            if (x != lastY)
                plot4points(buffer, cx, cy, lastY, x);

            error -= x;
            --x;
            error -= x;
        }
    }
}

static void plot4points(byte[,] buffer, int cx, int cy, int x, int y)
{
    horizontalLine(buffer, cx - x, cy + y, cx + x);
    if (y != 0)
        horizontalLine(buffer, cx - x, cy - y, cx + x);
}    
4

5 回答 5

19

另一个问题的答案非常好。但是,由于它会造成混乱,因此我将对其进行一些解释。

您在 Wikipedia 中看到的算法基本上找到1/8xy圆(角度 0 到pi/4),然后绘制 8 个点作为其镜像。例如:

    (o-y,o+x) x         x (o+y,o+x)

(o-x,o+y) x                  x (o+x,o+y) <-- compute x,y

                   o

(o-x,o-y) x                  x (o+x,o-y)

    (o-y,o-x) x         x (o+y,o-x)

另一个解决方案的建议是,如果您仔细观察这张图片,这非常有意义,它不是绘制 8 个点,而是绘制 4 条水平线:

    (o-y,o+x) x---------x (o+y,o+x)

(o-x,o+y) x-----------------x (o+x,o+y) <-- compute x,y

                   o

(o-x,o-y) x-----------------x (o+x,o-y)

    (o-y,o-x) x---------x (o+y,o-x)

现在,如果您计算(x,y)角度[0, pi/4]并为每个计算点绘制这 4 条线,您将绘制许多水平线填充一个圆圈,而没有任何线与另一条重叠。

更新

你在圆的底部得到重叠线的原因是(x,y)坐标是四舍五入的,所以在这些位置,它们本身水平(x,y) 移动。

如果你看一下这张维基百科图片:

在此处输入图像描述

您会注意到在圆圈的顶部,一些像素水平对齐。绘制源自这些点的水平线重叠。

如果你不想要这个,解决方案很简单。你必须保留x你绘制的前一个(因为顶部和底部是原始的镜像(x,y),你应该保留前一个代表这些线的 y 的 x )并且仅在该值发生变化时才绘制水平线。如果不是,则表示您在同一条线上。

鉴于您将首先遇到最里面的点,只有当新点不同时,您才应该为前一个点画线x(当然,总是画最后一条线)。或者,您可以从角度 PI/4 开始绘制到 0 而不是 0 到 PI/4,并且您将首先遇到外部点,因此每次看到新的x.

于 2012-06-04T08:45:43.020 回答
4

我想出了一个算法来绘制已经填充的圆圈。
它遍历将绘制圆的像素,仅此而已。
从这里开始,关于绘制像素函数的速度。

这是一个 *.gif,它演示了算法的作用!

至于算法这里的代码:

    //The center of the circle and its radius.
    int x = 100;
    int y = 100;
    int r = 50;
    //This here is sin(45) but i just hard-coded it.
    float sinus = 0.70710678118;
    //This is the distance on the axis from sin(90) to sin(45). 
    int range = r/(2*sinus);
    for(int i = r ; i >= range ; --i)
    {
        int j = sqrt(r*r - i*i);
        for(int k = -j ; k <= j ; k++)
        {
            //We draw all the 4 sides at the same time.
            PutPixel(x-k,y+i);
            PutPixel(x-k,y-i);
            PutPixel(x+i,y+k);
            PutPixel(x-i,y-k);
        }
    }
    //To fill the circle we draw the circumscribed square.
    range = r*sinus;
    for(int i = x - range + 1 ; i < x + range ; i++)
    {
        for(int j = y - range + 1 ; j < y + range ; j++)
        {
            PutPixel(i,j);
        }
    }

希望这会有所帮助……一些新用户……抱歉发布死灵。
~什米吉

于 2014-12-12T21:02:06.937 回答
4

我需要这样做,这是我想出的代码。此处的可视图像显示了绘制的像素,其中数字是遍历像素的顺序,绿色数字表示使用对称性的列的完成反射绘制的像素,如代码中所示。

在此处输入图像描述

void drawFilledMidpointCircleSinglePixelVisit( int centerX, int centerY, int radius )
{
    int x = radius;
    int y = 0;
    int radiusError = 1 - x;

    while (x >= y)  // iterate to the circle diagonal
    {

        // use symmetry to draw the two horizontal lines at this Y with a special case to draw
        // only one line at the centerY where y == 0
        int startX = -x + centerX;
        int endX = x + centerX;
        drawHorizontalLine( startX, endX, y + centerY );
        if (y != 0)
        {
            drawHorizontalLine( startX, endX, -y + centerY );
        }

        // move Y one line
        y++;

        // calculate or maintain new x
        if (radiusError<0)
        {
            radiusError += 2 * y + 1;
        }
        else 
        {
            // we're about to move x over one, this means we completed a column of X values, use
            // symmetry to draw those complete columns as horizontal lines at the top and bottom of the circle
            // beyond the diagonal of the main loop
            if (x >= y)
            {
                startX = -y + 1 + centerX;
                endX = y - 1 + centerX;
                drawHorizontalLine( startX, endX,  x + centerY );
                drawHorizontalLine( startX, endX, -x + centerY );
            }
            x--;
            radiusError += 2 * (y - x + 1);
        }

    }

}
于 2014-07-02T09:47:44.007 回答
2

我想评论您的更新#2:这个解决方案有效:(但我想我首先需要更多的声誉......)解决方案中有一个小错误,巧合的是在绘制小圆圈时。如果你将半径设置为 1,你会得到

00000
00000
01110
00100
00000

要解决这个问题,您需要做的就是将 plot4points 中的条件检查从

if (x != 0 && y != 0)

if (y != 0)

我已经在大大小小的圆圈上进行了测试,以确保每个像素仍然只分配一次。似乎工作得很好。我认为 x != 0 是不需要的。也节省了一点性能。

于 2015-04-26T15:30:17.403 回答
0

更新#2

 if (error >= 0)
 {
    if (x != lastY) 
       plot4points(buffer, cx, cy, lastY, x);

 if (error >= 0)
 {     
    plot4points(buffer, cx, cy, lastY, x);

Circle 和 FillCircle 版本:

Const
  Vypln13:Boolean=False;  // Fill Object


//Draw a circle at (cx,cy)
Procedure Circle(cx: integer; cy: integer; radius: integer );
Var
   error,x,y: integer;
Begin  
   error := -radius;
   x := radius;
   y := 0;

   while (x >= y) do
   Begin

     Draw4Pixel(cx,cy, x, y);
     if ( Not Vypln13 And ( x <> y) ) Then Draw4Pixel(cx,cy, y, x);

     error := error + y;
     y := y + 1;
     error := error + y;

     if (error >= 0) Then
     Begin

       if ( Vypln13) then Draw4Pixel(cx, cy, y - 1, x);

       error := error - x;
       x := x - 1;
       error := error - x;
     End;
   End;
End;


Procedure Draw4Pixel(cx,cy,dx,dy: integer);
Begin
  if ( (dx = 0) And (dy = 0) ) then
  begin
    PutPixel (cx , cy , Color13);
    exit;
  End;

  IF Vypln13 Then
  Begin
    HorizontLine (cx - dx,  cx + dx, cy + dy, Color13);
    if ( dy = 0 ) then exit;
    HorizontLine (cx - dx,  cx + dx, cy - dy, Color13);
    exit;
  end;

  PutPixel (cx + dx, cy + dy, Color13);
  if ( dx <> 0 ) then
  begin
    PutPixel (cx - dx, cy + dy, Color13);
    if ( dy = 0 ) then exit;
    PutPixel (cx + dx, cy - dy, Color13);
  End;
  PutPixel (cx - dx, cy - dy, Color13);

End;
于 2015-05-16T10:42:04.627 回答