可以使用Midpoint circle algorithm栅格化圆的边框。但是,我想要填充圆圈,而不是多次绘制像素(这非常重要)。
这个答案提供了算法的修改,产生了一个实心圆,但有几个像素被访问了几次: fast algorithm for drawing filled circles?
问:如何在不多次绘制像素的情况下栅格化圆圈?请注意,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);
}
答案 0 :(得分:15)
另一个问题的答案非常好。然而,由于它造成了混乱,我将稍微解释一下。
你在维基百科中看到的算法基本上找到1/8圆的x
和y
(角度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)
水平移动。
如果你看一下this wikipedia图片:
您会注意到,在圆圈顶部,某些像素水平对齐。绘制源自这些点的水平线重叠。
如果您不想这样,解决方案非常简单。您必须保留之前绘制的x
(因为顶部和底部是原始(x,y)
的镜像,您应该保留前面的x代表这些行的y)并且只绘制水平线,如果该值更改。如果没有,则意味着您在同一条线上。
鉴于您将首先遇到最里面的点,您应该为前一点绘制线条,只有新点具有不同的x
(当然,最后一行总是被绘制)。或者,您可以从角度PI / 4向下绘制到0而不是0到PI / 4,并且您将首先遇到外部点,因此每次看到新的x
时都会绘制线条。
答案 1 :(得分:3)
我需要这样做,这是我提出的代码。此处的可视图像显示绘制的像素,其中数字是遍历像素的顺序,绿色数字表示使用对称性的列完成反射绘制的像素,如代码所示。
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);
}
}
}
答案 2 :(得分:3)
我想出了一个算法来绘制已填充的圆圈 它会迭代圆圈将被绘制的像素,而不是其他任何东西 从这里开始,关于绘制像素函数的速度。
Here's a *.gif that demonstrates what the algorithm does !
至于算法,这里是代码:
//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);
}
}
希望这会有所帮助...一些新用户...抱歉发帖 〜Shmiggy
答案 3 :(得分:1)
我想评论你的更新#2:这个解决方案有效:(但我想我首先需要更多的声誉......)解决方案中存在一个小错误,巧合的是在绘制小圆圈时。如果将半径设置为1,则得到
00000
00000
01110
00100
00000
要解决这个问题,您需要做的就是从
更改plot4points中的条件检查if (x != 0 && y != 0)
到
if (y != 0)
我已经在小圆圈和大圆圈上对此进行了测试,以确保每个像素仍然只分配一次。似乎工作得很好。我认为不需要x!= 0。节省一点性能。
答案 4 :(得分: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;