c语言画实心坐标点,c-绘制实心圆的快速算法?
這是我的做法:
我正在使用具有兩位精度的定點值(我們必須管理半點和半點的平方值)
如上一個答案所述,我也使用平方值而不是平方根。
首先,我在圓的1/8部分中檢測到圓的邊界限制。 我正在使用這些點的對稱性繪制圓的4個“邊界”。 然后,我在圓圈內(nèi)繪制正方形。
與中點圓算法不同,該算法將在直徑均勻的情況下工作(并且在實數(shù)直徑情況下也有一些變化)。
如果我的解釋不清楚,請原諒我,我是法國人;)
void DrawFilledCircle(int circleDiameter, int circlePosX, int circlePosY)
{
const int FULL = (1 << 2);
const int HALF = (FULL >> 1);
int size = (circleDiameter << 2);// fixed point value for size
int ray = (size >> 1);
int dY2;
int ray2 = ray * ray;
int posmin,posmax;
int Y,X;
int x = ((circleDiameter&1)==1) ? ray : ray - HALF;
int y = HALF;
circlePosX -= (circleDiameter>>1);
circlePosY -= (circleDiameter>>1);
for (;; y+=FULL)
{
dY2 = (ray - y) * (ray - y);
for (;; x-=FULL)
{
if (dY2 + (ray - x) * (ray - x) <= ray2) continue;
if (x < y)
{
Y = (y >> 2);
posmin = Y;
posmax = circleDiameter - Y;
// Draw inside square and leave
while (Y < posmax)
{
for (X = posmin; X < posmax; X++)
setPixel(circlePosX+X, circlePosY+Y);
Y++;
}
// Just for a better understanding, the while loop does the same thing as:
// DrawSquare(circlePosX+Y, circlePosY+Y, circleDiameter - 2*Y);
return;
}
// Draw the 4 borders
X = (x >> 2) + 1;
Y = y >> 2;
posmax = circleDiameter - X;
int mirrorY = circleDiameter - Y - 1;
while (X < posmax)
{
setPixel(circlePosX+X, circlePosY+Y);
setPixel(circlePosX+X, circlePosY+mirrorY);
setPixel(circlePosX+Y, circlePosY+X);
setPixel(circlePosX+mirrorY, circlePosY+X);
X++;
}
// Just for a better understanding, the while loop does the same thing as:
// int lineSize = circleDiameter - X*2;
// Upper border:
// DrawHorizontalLine(circlePosX+X, circlePosY+Y, lineSize);
// Lower border:
// DrawHorizontalLine(circlePosX+X, circlePosY+mirrorY, lineSize);
// Left border:
// DrawVerticalLine(circlePosX+Y, circlePosY+X, lineSize);
// Right border:
// DrawVerticalLine(circlePosX+mirrorY, circlePosY+X, lineSize);
break;
}
}
}
void DrawSquare(int x, int y, int size)
{
for( int i=0 ; i
DrawHorizontalLine(x, y+i, size);
}
void DrawHorizontalLine(int x, int y, int width)
{
for(int i=0 ; i
SetPixel(x+i, y);
}
void DrawVerticalLine(int x, int y, int height)
{
for(int i=0 ; i
SetPixel(x, y+i);
}
要使用非整數(shù)直徑,可以提高定點精度或使用雙精度值。根據(jù)dY2 +(ray-x)*(ray-x)和ray2(dx2+dy2和r2)之間的差異,甚至應該可以進行某種抗鋸齒處理
總結(jié)
以上是生活随笔為你收集整理的c语言画实心坐标点,c-绘制实心圆的快速算法?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 万圣节| Huluween“魔幻洞窟”闯
- 下一篇: 第十三首歌曲《翱翔的骄鹰》