Sem 4‎ > ‎CG LAB‎ > ‎

P5: WAP to draw a circle using Bresenham's Algo

posted Jan 8, 2012, 7:37 AM by Neil Mathew   [ updated Jan 8, 2012, 7:37 AM ]
Notes:
  • One of my mistakes: (yc+y,xc+x) IS WRONG. (xc,yc) are centre coordinates.
    They cannot be rearranged with the symmetry points x and y.
    It should be like: (xc+y,yc+x)

  • Second Mistake: I initially added getmaxx()/2 directly to x. This is WRONG.
    The Bresenham Algo is made assuming the centre is at the origin. Therefore, the centre coordinates should be added only at the putpixel function. Not into the variables x and y.

    int x=xc+0, y=yc+r;     putpixel(xc+x,yc+y);   

  • Third is more of a caution, a silly mistake I most likely will continue to make:

    d=d+ 4*(x - y) + 10;


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
77
78
79
#include <graphics.h>
#include <math.h>
#include <iostream.h>
#include <conio.h>
 
void otherSpoints(int x, int y,int xc,int yc)
{
putpixel(-x+xc,y+yc,WHITE);
putpixel(x+xc,-y+yc,WHITE);
putpixel(-x+xc,-y+yc,WHITE);
putpixel(y+xc,x+yc,WHITE);
putpixel(-y+xc,x+yc,WHITE);
putpixel(y+xc,-x+yc,WHITE);
putpixel(-y+xc,-x+yc,WHITE);
}
 
void CIRCLE(int r)
{
int x,y;
int xc=getmaxx()/2;
int yc=getmaxy()/2;
 
// Plotting the First Point
x=0;
y=r;
putpixel(xc+x,yc+y,WHITE);
 
// Other Symmetry points:
otherSpoints(x,y,xc,yc);
 
//Initializing Decision Variable
int d=3-2*r;
 
 
//Plotting Other Points of Circle
while(x<y)
{
 
if(d<0)
{
d=d+4*x+6;
 
x+=1;
}
else
{
d=d+4*(x-y)+10;
 
x+=1;
y-=1;
}
 
putpixel(x+xc,y+yc,WHITE);  otherSpoints(x,y,xc,yc);
} //end of while
 
} //end of function
 
 
void main()
{
int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "C:/TC/BGI");
 
setbkcolor(DARKGRAY);
setcolor(WHITE);
 
int r;
 
cout<<"\n Enter the radius: ";
cin>>r;
 
cout<<"\n Press Enter to draw Circle.";
getch();
 
CIRCLE(r);
 
getch();
closegraph();
}


OUTPUT:





Comments