//Mid Point Ellipse
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int ellips(int ,int ,int ,int);
int main(void)
{
int gdriver=DETECT,errorcode,gmode;
int rx,ry,i,x,y;
start:
initgraph(&gdriver, &gmode, "C:\\TC\\BGI");
errorcode=graphresult();
if(errorcode!=grOk)
{
printf("Error");
}
setcolor(3);
rectangle(0,70,getmaxx(),getmaxy());
line(0,getmaxy()/2+35,getmaxx(),getmaxy()/2+35);
line(getmaxx()/2,70,getmaxx()/2,getmaxy());
printf("Enter the values of rx and ry: ");
scanf("%d %d",&rx,&ry);
printf("Enter the coordinates for the center: ");
scanf("%d %d",&x,&y);
ellips(rx,ry,x,y);
printf("Press 1 to continue: ");
scanf("%d",&i);
if(i==1)
goto start;
return;
}
int ellips(int rx,int ry,int x,int y)
{
int p1,p2,x1,y1;
x1=x;
y1=y;
x=0;
y=ry;
p1=(ry*ry)-(rx*rx*ry)+(rx*rx)/4;
//region 1
while((2*ry*ry*x)>=(2*rx*rx*y))
{
if(p1<0)
{
x++;
putpixel(getmaxx()/2+x+x1,getmaxy()/2+y+y1+35,3);
putpixel(getmaxx()/2+x+x1,getmaxy()/2-y+y1+35,3);
putpixel(getmaxx()/2-x+x1,getmaxy()/2-y+y1+35,3);
putpixel(getmaxx()/2-x+x1,getmaxy()/2+y+y1+35,3);
p1=p1+2*(ry*ry*x)+(ry*ry);
}
else if(p1>=0)
{
x++;
y--;
putpixel(getmaxx()/2+x+x1,getmaxy()/2+y+y1+35,3);
putpixel(getmaxx()/2+x+x1,getmaxy()/2-y+y1+35,3);
putpixel(getmaxx()/2-x+x1,getmaxy()/2-y+y1+35,3);
putpixel(getmaxx()/2-x+x1,getmaxy()/2+y+y1+35,3);
p1=p1+(2*ry*ry*x)-(2*rx*rx*y)+(ry*ry);
}
}
//region2
p2=(ry*ry)*(x+(1/2))^2+(rx*ry*(y-1)^2)-(rx*rx*ry*ry);
while(y>0)
{
if(p2>0)
{
y--;
putpixel(getmaxx()/2+x+x1,getmaxy()/2+y+y1+35,3);
putpixel(getmaxx()/2+x+x1,getmaxy()/2-y+y1+35,3);
putpixel(getmaxx()/2-x+x1,getmaxy()/2-y+y1+35,3);
putpixel(getmaxx()/2-x+x1,getmaxy()/2+y+y1+35,3);
p2=p2-2*(ry^2)*y+rx^2;
}
else if(p2<=0)
{
x++;
y--;
putpixel(getmaxx()/2+x+x1,getmaxy()/2+y+y1+35,3);
putpixel(getmaxx()/2+x+x1,getmaxy()/2-y+y1+35,3);
putpixel(getmaxx()/2-x+x1,getmaxy()/2-y+y1+35,3);
putpixel(getmaxx()/2-x+x1,getmaxy()/2+y+y1+35,3);
p2=p2+2*(ry^2)*x-2*(rx^2)*y+rx^2;
}
}
return ;
}
No comments:
Post a Comment