CSE4203: Computer Graphics
Chapter – 8 (part - B)
Graphics Pipeline
Mohammad Imrul Jubair
Outline
• Bresenham’s Circle Drawing Algorithm
2
Assumptions
Given,
Radius R circumference
We have to develop an algorithmthat
generates this circumference
3
Assumptions
The first pixel of the circumference is plotted on (0, R)
Given,
Radius R
(0, R)
(0,0)
4
Observation
The first pixel of the circumference is plotted on (0, R)
Then the plotting of next pixels starts clock-wise….
That means the plotting
(X, Y) = (0, R) 2 starts from (0,R) and
3 moving into the 2nd Octant
4 1
(0,0)
8
5
6 7
5
Observation
while moving through the 2nd octant, theXvalue
is increasing and Y value is decreasing
6
Observation
(0,R)
(R,0)
7
Observation
(0,R) (X,Y)
(R,0)
8
Observation
(0,R) (X,Y)
X=Y
45o
(R,0)
9
Observation
(0,R) (X,Y)
X=Y
45o (Y,X)
(R,0)
10
Observation
(0,R) (X,Y) In 2nd Octant : X<Y
But, in 1 st Octant : X>Y
X=Y
45o (Y,X)
(R,0)
11
Observation
(X,Y)
(-X,Y)
So, if we can obtain (X,Y) in 2nd
octant, we can calculate the
(Y,X) points-
• 7th Octant : (X,-Y)
• 6th Octant : (-X, -Y)
• 3rd Octant : (-X, Y)
(X,-Y)
(-X,-Y)
12
Observation
(X,Y)
(-X,Y)
So, if we can obtain (X,Y) in 2nd
octant, we can simply swap X
(Y,X) and Y to get the points-
(-Y,X) • 1 st Octant : (Y, X)
• 8th Octant : (Y, -X)
• 5th Octant : (-Y, -X)
(-Y,-X) (Y,-X) • 4th Octant : (-Y, X)
(X,-Y)
(-X,-Y)
13
Drawing in all the octants
So, if we can obtain
(X,Y) in 2nd octant, we
can calculate the (0,R)
points in other 7 So, our target is
octants to get the pixels
of only 2nd octant
of the
(0,0)
circumference
14
Bresenham’s Circle Drawing Algorithm: How it works
(0, R) C
15
Bresenham’s Circle Drawing Algorithm: How it works
(0, R) C E Next pixel is chosen
(from E or SE) to build
the line successively
SE
16
Bresenham’s Circle Drawing Algorithm: How it works
(0, R) C E Next pixel is chosen
(from E or SE) to build
the line successively
SE
17
Bresenham’s Circle Drawing Algorithm: How it works
(0, R) C E Next pixel is chosen
(from E or SE) to build
the line successively
SE
18
Bresenham’s Circle Drawing Algorithm: How it works
(0, R) C E Next pixel is chosen
(from E or SE) to build
the line successively
SE
19
Bresenham’s Circle Drawing Algorithm: How it works
(0, R)
Next pixel is chosen
(from E or SE) to build
the line successively
E
SE
20
Bresenham’s Circle Drawing Algorithm: How it works
(0, R)
Next pixel is chosen
(from E or SE) to build
the line successively
E
SE
21
Bresenham’s Circle Drawing Algorithm: How it works
(0, R)
As we know that,
In 2nd Octant : X < Y
In 1st Octant : X > Y
We will stop when X > Y,
C that means when 2nd octant
is completed
22
Equation of Circle and its function representation
(X,Y)
23
Equation of Circle and its function representation
If F(X,Y) = 0, the point (X,Y) on
the circle
If F( X, Y ) > 0, the point (X,Y)
is outside the circle
If F( X, Y ) < 0, the point (X,Y)
is inside the circle
24
Selecting E or SE
C(XP,YP)
25
Selecting E or SE
Selecting E or SE depends
on closeness to the
circumference.
C(XP,YP) E(XP+1,YP)
If E is closer to
circumference,
then E is selected
If SE is closer,
then SE is selected
SE(XP+1,YP-1)
26
Selecting E or SE using Mid Point Criteria
If midpoint M is outside
the circle, SE is closer to
the circumference,
So, SE is selected
If midpoint M is inside
the circle, E is closer to
the circumference,
So, E is selected
27
Selecting E or SE using Mid Point Criteria
We know,
Lets put the mid point M’s coordinate in function F(X,Y)
F(M) = F(XP + 1, YP – 0.5) = (XP + 1)2 + (YP – 0.5)2 – R2
C (XP ,YP) E(XP+1, YP)
Lets store F(M) in a variable d
(XP+1, YP-0.5)M So, d = F(M)
d is called ‘decision variable’
SE(XP+1,YP-1)
28
Selecting E or SE using Mid Point Criteria
If d >= 0, then midpoint M is
outside the circle, SE is closer
to the circumference,
So, SE is selected
If d < 0, then midpoint M is
inside the circle, E is closer
to the circumference,
So, E is selected
29
Bresenham’s Mid Point Criteria : Successive Updating (for selecting E)
d1 =F(M1)
= F(XP+1, YP- 0.5)
= (XP+1)2 + (YP- 0.5)2 - R2
C(XP,YP) E
M1
SE
30
Bresenham’s Mid Point Criteria : Successive Updating (for selecting E)
d1 =F(M1)
= F(XP+1, YP- 0.5)
= (XP+1)2 + (YP- 0.5)2 - R2
C(XP,YP) E
If d1 < 0, E(XP=XP+1, YP)
M1
SE
31
Bresenham’s Mid Point Criteria : Successive Updating (for selecting E)
d1 =F(M1)
= F(XP+1, YP- 0.5)
= (XP+1)2 + (YP- 0.5)2 - R2
C E
If d1 < 0, E(XP=XP+1, YP)
d2 = F(M2)
M2
= F(XP+2, YP-0.5)
= (XP+2)2 + (YP- 0.5)2 - R2
= XP2+ 4XP + 4 + (Y P- 0.5)2 -R2
SE = XP2+ 2XP + 1 + (YP - 0.5)2 - R2 + 2XP + 3
= d1 + (2XP+3)
32
Bresenham’s Mid Point Criteria : Successive Updating (for selecting E)
d1 =F(M1)
= F(XP+1, YP- 0.5)
= (XP+1)2 + (YP- 0.5)2 - R2
C E
If d1 < 0, E(XP=XP+1, YP)
d2 = F(M2)
M2
= F(XP+2, YP-0.5)
= (XP+2)2 + (YP- 0.5)2 - R2
= XP2+ 4XP + 4 + (Y P- 0.5)2 -R2
SE = XP2+ 2XP + 1 + (YP - 0.5)2 - R2 + 2XP + 3
= d1 + (2XP+3)
33
Bresenham’s Mid Point Criteria : Successive Updating (for selecting E)
Every iteration after selecting E, we can
successively update our decision variable with-
dNEW = dOLD+ (2XP + 3)
34
Bresenham’s Mid Point Criteria : Successive Updating (for selecting SE)
d1 =F(M1)
= F(XP+1, YP- 0.5)
= (XP+1)2 + (YP- 0.5)2 - R2
C(XP,YP) E
M1
SE
35
Bresenham’s Mid Point Criteria : Successive Updating (for selecting SE)
d1 =F(M1)
= F(XP+1, YP- 0.5)
= (XP+1)2 + (YP- 0.5)2 - R2
C(XP,YP) If d1 >= 0, SE(XP=XP+1, YP-1)
E
M1
SE
36
Bresenham’s Mid Point Criteria : Successive Updating (for selecting SE)
d1 =F(M1)
= F(XP+1, YP- 0.5)
= (XP+1)2 + (YP- 0.5)2 - R2
If d1 >= 0, SE(XP=XP+1, YP-1)
d2 = F(M2)
C E
…. DIY….
M2
= d1 + (2XP- 2YP+5)
SE
37
Bresenham’s Mid Point Criteria : Successive Updating (for selecting SE)
Every iteration after selecting NE, we can
successively update our decision variable with-
dNEW = dOLD + (2XP- 2YP+5)
38
Bresenham’s Mid Point Criteria : Successive Updating (summary)
If d < 0, then midpoint M is
inside the circle, E is closer
to the circumference,
So, E is selected and do-
d = d + ΔE
Where, ΔE = 2XP + 3
If d >= 0, then midpoint M is
outside the circle, SE is closer
to the circumference,
So, SE is selected and do-
d = d + ΔSE
Where, ΔSE = 2XP - 2YP + 5
39
Initialization
C(0,R) E(XP+1, YP) dINIT= F(M1)
= F(1, R-0.5)
= (1)2 + (R –0.5)2 –R2
(1, R-0.5) M1 = 1 + R2–R + 0.25 –R2
=1.25 –R
SE(XP+1, YP-1)
40
Initialization
C(0,R) E(XP+1, YP) dINIT= F(M1)
= F(1, R-0.5)
= (1)2 + (R –0.5)2 –R2
(1, R-0.5) M1 = 1 + R2–R + 0.25 –R2
=1.25 –R
≈ 1 -R
SE(XP+1, YP-1)
41
Initialization
R=2
d = 1.25 – R = – 0.75
R=2
d = 1 – R = –1
42
Summary
So, finally…..
dINIT = 1 – R
If d < 0, then E is selected, d = d + ΔE
If d >= 0, then SE is selected, d = d + ΔSE
Where,
ΔE = 2XP + 3
ΔSE = 2XP – 2YP + 5
43
Algorithm
void MidpointCircle(int radius)
{
int x = 0;
int y = radius ;
int d = 1 –radius ;
CirclePoints(x, y);
while (y > x)
{
if (d < 0) /* Select E*/
d = d + 2 * x + 3;
else
{ /* Select SE*/
d = d + 2 * ( x –y ) + 5;
y = y –1;
}
x = x + 1;
CirclePoints(x, y);
}
}
44
Algorithm
void MidpointCircle(int radius)
{
int x = 0;
int y = radius ;
int d = 1 –radius ; CirclePoints (x,y)
CirclePoints(x, y); Plotpoint(x,y) ;
while (y > x) Plotpoint (x,-y) ;
{ Plotpoint(-x,y) ;
if (d < 0) /* Select E*/ Plotpoint(-x, -y) ;
d = d + 2 * x + 3; Plotpoint(y,x) ;
else Plotpoint(y, -x) ;
{ /* Select SE*/ Plotpoint(-y, x) ;
d = d + 2 * ( x –y ) + 5; Plotpoint( -y, -x) ;
y = y –1; end
}
x = x + 1;
CirclePoints(x, y);
}
}
45
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9
8
7
6
5
4
46
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7
6
5
4
47
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7
6
5
4
K 1
2x 0
2y 20
h
(x,y)
48
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7
6
5
4
K 1
2x 0
2y 20
h
(x,y) E(1,10)
h <= 0,E
49
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7 h = h+ΔE=h+2x+3
6 =-9+0+3
=-6
5
4
K 1
2x 0
2y 20
h -6
(x,y) E(1,10)
50
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7
6
5
4
K 1 2
2x 0 2
2y 20 20
h -6
(x,y) E(1,10)
51
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7
6
5
4
K 1 2
2x 0 2
2y 20 20
h -6
(x,y) E(1,10) E(2,1 0)
h <= 0,E
52
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7 h = h+ΔE=h+2x+3
6 =-6+2+3
=-1
5
4
K 1 2
2x 0 2
2y 20 20
h -6 -1
(x,y) E(1,10) E(2,1 0)
53
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7
6
5
4
K 1 2 3
2x 0 2 4
2y 20 20 20
h -6 -1
(x,y) E(1,10) E(2,1 0)
54
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7
6
5
4
K 1 2 3
2x 0 2 4
2y 20 20 20
h -6 -1
(x,y) E(1,10) E(2,1 0) E(3,1 0)
h <= 0,E
55
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7 h = h+ΔE=h+2x+3
6 =-1+4+3
=6
5
4
K 1 2 3
2x 0 2 4
2y 20 20 20
h -6 -1 6
(x,y) E(1,10) E(2,1 0) E(3,1 0)
56
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7
6
5
4
K 1 2 3 4
2x 0 2 4 6
2y 20 20 20 20
h -6 -1 6
(x,y) E(1,10) E(2,1 0) E(3,1 0) S(4,9)
h > 0,SE
57
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7 h = h+ΔSE=h+2x-2y+5
6 =6+6-20+5
=-3
5
4
K 1 2 3 4
2x 0 2 4 6
2y 20 20 20 20
h -6 -1 6 -3
(x,y) E(1,10) E(2,1 0) E(3,1 0) S(4,9)
58
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7
6
5
4
K 1 2 3 4 5 6 7
2x 0 2 4 6 8 10 12
2y 20 20 20 20 18 18 16
h -6 -1 6 -3 8 5 6
(x, y) E(1,10) E(2,10) E(3,10) S(4,9) E(5,9) S(6,8) S(7,7)
59
Example
0 1 2 3 4 5 6 7 Given:
10 Radius , R = 10
9 (x,y)=(0,10)
8 h = 1 –R= -9
7
6
5
4
Untill y >x
K 1 2 3 4 5 6 7
2x 0 2 4 6 8 10 12
2y 20 20 20 20 18 18 16
h -6 -1 6 -3 8 5 6
(x, y) E(1,10) E(2,10) E(3,10) S(4,9) E(5,9) S(6,8) S(7,7)
60
Practice Problem
• Perform the midpoint algorithm to draw a circle’s portion at 7th octant
which has center at (2,-3) and a radius of 7 pixels. Show each
iterations and plot the points.
61