可以看到,都是delta=2*(1-radius)这样的,起作用应该是判断要画的点x、y坐标的变化趋势,先把我注释了的代码贴下,加了getch()可以看到画的过程
-----------------------------------------------------------------
#include<graphics.h>
#include<stdio.h>
void BresenhemCircle(int centerx, int centery, int radius, int color, int type)
void main()
{
int drive=DETECT,mode
int i,j
initgraph(&drive,&mode,"")
BresenhemCircle(300,200,100,15,0)
getch()
}
void BresenhemCircle(int centerx, int centery, int radius, int color, int type)
{
int y = radius/*初始纵坐标远离原点*/
int delta = 2*(1-radius)
int direction
while (y >= 0)
{
getch()
if (!type)/*执行*/
{
/*在上半圆画两点*/
putpixel(centerx+x, centery+y, color)
putpixel(centerx-x, centery+y, color)
/*在下半圆画两点*/
putpixel(centerx-x, centery-y, color)
putpixel(centerx+x, centery-y, color)
getch()
}
else/*不执行*/
{
line(centerx+x, centery+y, centerx+x, centery-y)
line(centerx-x, centery+y, centerx-x, centery-y)
getch()
}
/*以下代码设置下次四点的位置,圆是对称的,且此方法相当于同时画四个圆弧
观察右上方圆弧可知,前一半是x增的要快些,后一半是y减的快些*/
if (delta <0)
{
if ((2*(delta+y)-1) <0)
direction = 1/*选择横向加*/
else
direction = 2
}
else if(delta >0)
{
if ((2*(delta-x)-1) >0)
direction = 3/*选择纵向减*/
else
direction = 2
}
else
direction=2
switch(direction)
{
case 1:
x++/*只横坐标远离原点*/
delta += (2*x+1)/*小执行到这,所以加*/
break
case 2:
x++
y--/*横向远离,同时纵向靠近*/
delta += 2*(x-y+1)/*即(2*x+1)+(-2*y+1)*/
break
case 3:
y--/*只纵坐标靠近原点*/
delta += (-2*y+1)/*大执行到这,所以减*/
break
}
}
}
这是我做过的,你试试#include<stdio.h>
#include<graphics.h>
#define PATH "c:\\TC30\\BGI"
int x0,y0
void Bresenham_Circle(int r,int color)
{
int x,y,delta,delta1,delta2,direction
x=0
y=r
delta=2*(1-r)
while(y>=0)
{
putpixel(x+x0,y+y0,color)
putpixel(x0-x,y0-y,color)
putpixel(x0-x,y+y0,color)
putpixel(x+x0,y0-y,color)
if(delta<0)
{
delta1=2*(delta+y)-1
if(delta1<=0)
direction=1
else
direction=2
}
else
{
if(delta>0)
{
delta2=2*(delta-x)-1
if(delta2<=0)
direction=2
else
direction=3
}
else
direction=2
}
switch(direction)
{
case 1:x++
delta+=2*x+1
break
case 2:x++
y--
delta+=2*(x-y+1)
break
case 3:y--
delta+=(-2*y+1)
break
}
}
}
int main()
{
int a,b
int r,color
// scanf("%d%d%d%d%d%d",&x0,&y0,&x1,&y1,&color)
a=VGA
b=VGAHI
initgraph(&a,&b,PATH)
setbkcolor(24)
setcolor(15)
x0=200
y0=200
r=180
color=10
Bresenham_Circle(r,color)
getch()
return 1
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)