Drawing Bresenhams Ellipse Midpoint Algorithm in c/c++ program with output

Without using ellipse function in graphics draw the ellipse. By using Bresenhams algorithm .The main object in the algorithm is to perform analyse and manipulate linear equation so integer arithmetic is used in the calculations.Integer Arithmetic has the advantages of speed and precision; working with floating point values requires more time and it is difficult one and memory and such values would need to be rounded to integers anyway. In this article we assign the more difficult problem of approximating the plot of an ellipse on a grid of discrete pixels, using only
integer arithmetic. | CS1355-Graphics & Multimedia Lab
Ellipse Drawing Algorithm in c program  computer graphics lab

Source code programming

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"");
scanf("%d%d",&xc,&yc);
scanf("%d%d",&a,&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc+x,yc-y,10);
}
//# Author: J.Ajai
//#Mail-id- ajay.compiler@gmail.com
//# PH:+91-9790402155

OUTPUT
Ellipse Drawing Algorithm
Enter the co-ordinates
Xc = 200
Yc = 200
A = 100
B = 70

 

Ellipse Drawing Algorithm in c program  computer graphics lab 

Post a Comment

0 Comments