#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
void put_pixel(const int h,const int k,const int x,const int y,int color)
{
putpixel((getmaxx()/2)+(h+x),(getmaxy()/2)-(k+y),color);
putpixel((getmaxx()/2)+(h+x),(getmaxy()/2)-(k-y),color);
putpixel((getmaxx()/2)+(h-x),(getmaxy()/2)-(k-y),color);
putpixel((getmaxx()/2)+(h-x),(getmaxy()/2)-(k+y),color);
}
void midpoint_ellipse(const int h,const int k,const int a,const int b,int col)
{
float aa=(a*a);
float bb=(b*b);
float aa2=(aa*2);
float bb2=(bb*2);
float x=0;
float y=b;
float fx=0;
float fy=(aa2*b);
float p=(int)(bb-(aa*b)+(0.25*aa)+0.5);
put_pixel(h,k,x,y,col);
while(fx<fy)
{
x++;
fx+=bb2;
if(p<0)
p+=(fx+bb);
else
{
y--;
fy-=aa2;
p+=(fx+bb-fy);
}
put_pixel(h,k,x,y,col);
}
p=(int)((bb*(x+0.5)*(x+0.5))+(aa*(y-1)*(y-1))-(aa*bb)+0.5);
while(y>0)
{
y--;
fy-=aa2;
if(p>=0)
p+=(aa-fy);
else
{
x++;
fx+=bb2;
p+=(fx+aa-fy);
}
put_pixel(h,k,x,y,col);
}
}
void init_graph()
{
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
}
}
void main()
{
init_graph();
int h =0;
int k =0;
int rx = 100;
int ry = 100;
int rdx = -2;
do
{
rx += rdx;
if(rx == 0 || rx == 100)
rdx *= -1;
midpoint_ellipse(h,k,rx,ry,RED);
delay(10);
cleardevice();
}while(!kbhit());
getch();
}
No comments:
Post a Comment