//This is Midpoint ellipse algorithm developed using C language
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void jayuellipse(int x,int y,int rx,int ry);
void print(int,int,int,int);
void main()
{
int xc,yc,p,r,rx,ry,gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "..\\bgi");
printf("enter values of (x,y,xrad,yrad)"),scanf("%d%d%d%d",&xc,&yc,&rx,&ry);
jayuellipse(xc,yc,rx,ry);
getch();
}
void jayuellipse(int xc,int yc,int rx,int ry)
{
long int p,p2,x,y,dx,dy,ry2,rx2,rx2ry,hfrx2,_2ry2x,_2rx2y;
float x5;
//initialization of values
x=0;
y=ry;
ry2=ry*ry;
rx2=rx*rx;
rx2ry=rx*rx*ry;
hfrx2=.25*rx*rx;
p=ry2-rx2ry+hfrx2;
_2ry2x=2*ry*ry*x;
_2rx2y=2*rx*rx*y;
dx=_2ry2x;
dy=_2rx2y;
//calculation for region 1
while(dx<dy)
{
print(x,y,xc,yc);
if(p<0)
{
x++;
dx+=2*ry2;
p+=dx+ry2;
}
else if(p>=0)
{
x++;
y--;
dx+=2*ry2;
dy-=2*rx2;
p+=dx+ry2-dy;
}
}
//end of region 1
x5=x+.5;
p2=(ry2*x5*x5)+(rx2*(y-1)*(y-1))-(rx2*ry2);
//calculation for region 2
while(y>=0)
{
print(x,y,xc,yc);
if(p2>0)
{
y--;
dx+=-(2*rx2);
p2+=rx2-(dx);
}
else
{
x++;y--;
dx+=-(2*rx2);
dy+=2*ry2;
p2+=rx2-(dx)+(dy);
}
}
}
void print(int x,int y,int xc,int yc)
{
putpixel(x+xc,y+yc,7);
putpixel(x+xc,-y+yc,7);
putpixel(-x+xc,y+yc,7);
putpixel(-x+xc,-y+yc,7);
}
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void jayuellipse(int x,int y,int rx,int ry);
void print(int,int,int,int);
void main()
{
int xc,yc,p,r,rx,ry,gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "..\\bgi");
printf("enter values of (x,y,xrad,yrad)"),scanf("%d%d%d%d",&xc,&yc,&rx,&ry);
jayuellipse(xc,yc,rx,ry);
getch();
}
void jayuellipse(int xc,int yc,int rx,int ry)
{
long int p,p2,x,y,dx,dy,ry2,rx2,rx2ry,hfrx2,_2ry2x,_2rx2y;
float x5;
//initialization of values
x=0;
y=ry;
ry2=ry*ry;
rx2=rx*rx;
rx2ry=rx*rx*ry;
hfrx2=.25*rx*rx;
p=ry2-rx2ry+hfrx2;
_2ry2x=2*ry*ry*x;
_2rx2y=2*rx*rx*y;
dx=_2ry2x;
dy=_2rx2y;
//calculation for region 1
while(dx<dy)
{
print(x,y,xc,yc);
if(p<0)
{
x++;
dx+=2*ry2;
p+=dx+ry2;
}
else if(p>=0)
{
x++;
y--;
dx+=2*ry2;
dy-=2*rx2;
p+=dx+ry2-dy;
}
}
//end of region 1
x5=x+.5;
p2=(ry2*x5*x5)+(rx2*(y-1)*(y-1))-(rx2*ry2);
//calculation for region 2
while(y>=0)
{
print(x,y,xc,yc);
if(p2>0)
{
y--;
dx+=-(2*rx2);
p2+=rx2-(dx);
}
else
{
x++;y--;
dx+=-(2*rx2);
dy+=2*ry2;
p2+=rx2-(dx)+(dy);
}
}
}
void print(int x,int y,int xc,int yc)
{
putpixel(x+xc,y+yc,7);
putpixel(x+xc,-y+yc,7);
putpixel(-x+xc,y+yc,7);
putpixel(-x+xc,-y+yc,7);
}
No comments:
Post a Comment