Sem 4‎ > ‎CG LAB‎ > ‎

P7: WAP to draw an elipse using MidPoint Algo

posted Jan 25, 2012, 2:48 AM by Neil Mathew

SOURCE CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
 
void DrawPoints(int x, int y, int xc, int yc)
{
putpixel(xc+x,yc+y, CYAN);
putpixel(xc-x,yc+y, CYAN);
putpixel(xc+x,yc-y, CYAN);
putpixel(xc-x,yc-y, CYAN);
}
 
 
void Eclipse(int xc, int yc, float a, float b)
{
int x=0;
int y=b;
DrawPoints(x,y,xc,yc);
 
float P= b*b - a*a*b + 0.25*a*a;
 
while( 2*b*b*x <= 2*a*a*y )
{
       if(P < 0)
       {
       DrawPoints(++x, y, xc, yc);
       P=P+ 2*b*b*x + b*b;
       }
       else
       {
       DrawPoints(++x,--y, xc, yc);
       P=P+ 2*b*b*x - 2*a*a*y + b*b;
       }
 
}
 
 
float P2= b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
 
while( y >= 0 ) //x!=a || y!=0 );
{
       if(P2 < 0)
       {
       DrawPoints(++x,--y, xc, yc);
       P2=P2+ 2*b*b*x - 2*a*a*y + a*a;
       }
       else
       {
       DrawPoints(x,--y, xc, yc);
       P2=P2 - 2*a*a*y + a*a;
       }
 
}
}
.
void main()
{
 
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
 
 
float a,b;
int xc=getmaxx()/2;
int yc=getmaxy()/2;
 
cout<<"\n Enter Major axis (a) and Minor axis (b) : ";
cin>>a>>b;
 
Eclipse(xc,yc,a,b);
 
getch();
closegraph();
 
 
}



OUTPUT:





Comments