我编了一个用bresenham算法画椭圆的小程序,结果发现不太好使 那位高手指点一下 谢谢!
程序如下:
#include "stdio.h"
#include <graphics.h>
#define closegr closegraph
#include <conio.h>
void circlepoint(int j,int k,int pointX,int pointY)
{
putpixel(pointX,pointY,WHITE);
putpixel(j+pointX,k+pointY,RED);
putpixel(-j+pointX,k+pointY,YELLOW);
putpixel(-j+pointX,-k+pointY,GREEN);
putpixel(j+pointX,-k+pointY,BLUE);
}
void Midbresenhamellipse(int a,int b,int aR,int bR )
{
if(aR<bR)
{
int x,y;
float d1,d2;
x=0;
y=bR;
d1=bR*bR+aR*aR*(-bR+0.25);
circlepoint(x,y,a,b);
while(bR*bR*(x+1)<aR*aR*(y-0.5))
{
if(d1<=0)
{
d1+=bR*bR*(2*x+3);
x++;
}
else
{
d1+=bR*bR*(2*x+3)+aR*aR*(-2*y+2);
x++;
y--;
}
circlepoint(x,y,a,b);
}
d2=bR*bR*(x+0.5)*(x+0.5)+aR*aR*(y-1)*(y-1)-aR*aR*bR*bR;
while(y>0)
{
if(d2<=0)
{
d2+=bR*bR*(2*x+2)+aR*aR*(-2*y+3);
x++;
y--;
}
else
{
d2+=aR*aR*(-2*y+3);
y--;
}
circlepoint(x,y,a,b);
}
}
else /*横向放置圆*/
{
int X,Y;
float d3,d4;
X=0;
Y=bR;
d3=0.25*bR*bR+aR*aR*(-2*bR+1);
circlepoint(X,Y,a,b);
while(bR*bR*(X+0.5)>aR*aR*(Y-1))
{
if(d3>=0)
{
d3+=aR*aR*(-2*Y+3);
Y--;
}
else {
d3+=aR*aR*(-2*Y+3)+bR*bR*(2*X+2);
X++;
Y--;
}
circlepoint(X,Y,a,b);
}
d4=bR*bR*(X+1)*(X+1)+aR*aR*(Y-0.5)*(Y-0.5)-aR*aR*bR*bR;
while(Y>=0)
{
if(d4<0)
{
d4+=bR*bR*(2*X+3);
X++;
}
else
{
d4+=bR*bR(2*X+3)+aR*aR(2-2*Y);
X++;
Y--;
}
circlepoint(X,Y,a,b);
}
}
}
int main(void)
{ int j,k,longR,shortR;
int gdriver,gmode;
detectgraph(&gdriver,&gmode);
initgraph(&gdriver,&gmode,"");
printf("\nEnter the center of circle :");
scanf("%d,%d",&j,&k);
printf("Enter long and short radius:");
scanf("%d,%d",&longR,&shortR);
Midbresenhamellipse(j,k,longR,shortR);
getch();
closegr();
return 0;
}
求助! bresenham画椭圆