This program is for implementing Mid Point Ellipse Drawing Algorithm.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void plot(int xc,int yc,int x,int y)
{
putpixel(x+xc,y+yc,1);
putpixel(x+xc,-y+yc,1);
putpixel(-x+xc,y+yc,1);
putpixel(-x+xc,-y+yc,1);
}
void myellipse(int xc,int yc,int rx,int ry)
{
long rx2,ry2,px,py,tworx2,twory2,p;
float x,y;
x=0;
y=ry;
rx2=rx*rx;
ry2=ry*ry;
tworx2=2*rx2;
twory2=2*ry2;
px=0;
py=tworx2*y;
//Printing the initial point
plot(xc,yc,x,y);
p=(int)(ry2-(rx2*ry)+(0.25*rx2));
while(px<py)
{
x++;
px+=twory2;
if(p<0)
p+=px+ry2;
else
{
y=y-1;
py-=tworx2;
p+=ry2+px-py;
}
plot(xc,yc,x,y);
}
p=(int)((ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2));
while(y>0)
{
y--;
py-=tworx2;
if(p>0)
p+=rx2-py;
else
{
x++;
px+=twory2;
p+=rx2-py+px;
}
plot(xc,yc,x,y);
}
}
void main()
{
int gd,gm;
int xc,yc,rx,ry;
clrscr();
gd=DETECT;
detectgraph(&gd,&gm);
printf("Enter values of centre of ellipse\n");
scanf("%d%d",&xc,&yc);
printf("Enter the x radius and y radius\n");
scanf("%d%d",&rx,&ry);
initgraph(&gd,&gm,"c:\\tc\\bgi");
myellipse(xc,yc,rx,ry);
getch();
}
\
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void plot(int xc,int yc,int x,int y)
{
putpixel(x+xc,y+yc,1);
putpixel(x+xc,-y+yc,1);
putpixel(-x+xc,y+yc,1);
putpixel(-x+xc,-y+yc,1);
}
void myellipse(int xc,int yc,int rx,int ry)
{
long rx2,ry2,px,py,tworx2,twory2,p;
float x,y;
x=0;
y=ry;
rx2=rx*rx;
ry2=ry*ry;
tworx2=2*rx2;
twory2=2*ry2;
px=0;
py=tworx2*y;
//Printing the initial point
plot(xc,yc,x,y);
p=(int)(ry2-(rx2*ry)+(0.25*rx2));
while(px<py)
{
x++;
px+=twory2;
if(p<0)
p+=px+ry2;
else
{
y=y-1;
py-=tworx2;
p+=ry2+px-py;
}
plot(xc,yc,x,y);
}
p=(int)((ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2));
while(y>0)
{
y--;
py-=tworx2;
if(p>0)
p+=rx2-py;
else
{
x++;
px+=twory2;
p+=rx2-py+px;
}
plot(xc,yc,x,y);
}
}
void main()
{
int gd,gm;
int xc,yc,rx,ry;
clrscr();
gd=DETECT;
detectgraph(&gd,&gm);
printf("Enter values of centre of ellipse\n");
scanf("%d%d",&xc,&yc);
printf("Enter the x radius and y radius\n");
scanf("%d%d",&rx,&ry);
initgraph(&gd,&gm,"c:\\tc\\bgi");
myellipse(xc,yc,rx,ry);
getch();
}
\