0% found this document useful (0 votes)
18 views

Computer Graphics - PPT - CH2

Uploaded by

vap3x97
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Computer Graphics - PPT - CH2

Uploaded by

vap3x97
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 122

COMPUTER GRAPHICS

Chapter 2
OUTPUT
PRIMITIVES
Topics covered

 Scan conversions of point, line, circle and ellipse


 Line Drawing Algorithms
 DDA algorithm and
 Bresenham’s algorithm
 Midpoint Circle drawing algorithm
 Midpoint Ellipse drawing algorithm
 Aliasing, Antialiasing techniques :
 Pre and post filtering,
 super sampling,
 and pixel phasing.
 Filled Area Primitive:
 Scan line Polygon Fill algorithm
 Inside outside tests
 Boundary Fill and Flood fill algorithm
 Thischapter deals with basic raster graphics
algorithms for drawing 2D objects such as
line, circle, ellipse etc.
What do you mean by output
primitive?

 Those functions in a graphics package that we use to describe the


various picture components are called the graphics output
primitives, or simply primitives.
 The basic graphics primitive are
 Point
 Line
 Circle
 ellipse
Scan conversion

 Computer screen is composed of dots or pixels which are the


smallest unit of the screen.
 Scan conversion is the process of converting continuous objects
in to set of discrete pixels that can be plotted on display device.
 Mathematically a line can be defined as a set of points drawn in
particular direction.
What is a line ?

 A line in computer graphics is typically refers to the line


segment.
 A line can be represented using different forms
 Given two end points of the line (x1,y1) and (x2,y2)
1) Slope intercept form
Y = mx + c
Where m = slope of the line = dy / dx = (y2-
y1)/ (x2-x1)
C = the y – intercept of line
2) Another form of a line equation is
y – y1 = m * (x-x1) where m = slope
Different slopes of line

Slope m = dy /
dx

(2,8)
Slope of first line
(7,7) m = y2-y1 / x2-x2
m = ( 7-2) / (7-2) =1

(7,3)
Slope of Second line
(2,2)
m = y2-y1 / x2-x2
m = (3-8)/(7-2) =-5/5 = -
1
Slopes of Line
Lines with all slopes
Line with dy = 0 (Horizontal line)

 Xk+1 = xk + xinc => Xk+1 = Xk +1


Example
Yk+1 = yk + yinc => Yk+1 = Yk +
 (x1 ,y1) = (2,2) x Y
(dy/steps)
2 2 y
 (x2,y2) = (9,2) Yk+1 = Yk + (dy/dx)
3 2 Yk+1 = Yk + m
 dx = x2-x1 = 7
4 2
 dy = y2 – y1 = 0
5 2
 Slope m = dy / dx = 0
6 2
 Steps = max (dx, dy)
7 2
 (2,2) (9,2)
= max ( 7, 0) = 7 8 2
 Xinc = dx /steps = 7/7 =1 9 2
 x
Yinc = dy /steps = 0
Line with dx =0 (Vertical line)

 Example Yk+1 = Yk + Yinc => Yk+1 = Yk +1


X = Xk + Xinc => Xk+1 = Xk +
 (x1 ,y1) = (2,5) x Y y k+1
(dx/steps)
 (x2,y2) = (2,12) 2 5 Xk+1 = Xk + (dx/dy)
 dx = x2-x1 = 2-2=0 2 6 Xk+1 = Xk + 1/m
2 7 (2,12)
 dy = y2 – y1 = 12-5 = 7

2 8
Slope m = dy / dx = infinity
2 9
 Steps = max(dx,dy) = dy
2 10
 = max (0 , 7) = 7
2 11 (2,5) x
 Xinc = dx /steps = 0/7 = 0
2 12
 Yinc = dy /steps = 7/7 = 1
Line with Positive slope m < 1

 Example x Y Round(

y)
(x1 ,y1) = (5,4)
5 4 4
 (x2,y2) = (12,7)
6 4.4 4
 dx = x2-x1 = 12-5=7
7 4.8 5
 dy = y2 – y1 = 7-4 = 3 8 5.2 5
 m = dy / dx = 3/7 = 0.4 9 5.6 6 (12,
 7)
Steps = max (dx ,dy) = 7 10 6.0 6
 Xinc = dx/steps = 7/7 = 1 11 6.4 6
(5,4)
 Yinc = dy /steps = 3/7 =0.412 6.8 7
Xk+1 = Xk + xinc => Xk+1 = Xk +1
Yk+1 = Yk + yinc => Yk+1 = Yk + (dy/steps)
Yk+1 = Yk + (dy/dx) = Yk + m
Line with Positive slope m >= 1

 Example x Round( Y

x)
(x1 ,y1) = (5,7)
5 5 7
 (x2,y2) = (10,15)
5.6 6 8
 dx = x2-x1 = 10-5=5
6.2 6 9
 dy = y2 – y1 = 15 - 7 = 8 6.8 7 10
 m = dy / dx = 8 / 5 = 1.6
7.4 7 11
 Steps = max (dx ,dy) = 8 8.0 8 12
 Xinc = dx /steps = 5/8 = 0.6
8.6 9 13
 Yinc = dy /steps = 8/8 =1 9.2 9 14
9.8 10 15
Yk+1 = Yk + Yinc => Yk+1 = Yk +1
Xk+1 = Xk + Xinc => Xk+1 = Xk + (dx/steps)
Lines with Slope m = 0

Case 1: If dy = 0 (Horizontal line)


dy =0
 Xk+1 = Xk + 1
 Yk+1 = Yk (x1,y1) (x2,y2)

Case 2: If dx = 0 (Vertical line – infinity


slope)
(x2,y2) dx =0
 Xk+1 = Xk
 Yk+1 = Yk + 1
(x1,y1)
Line With positive slopes

y
m<1
Case 1: If m < 1
 Xk+1 = Xk + 1 (step along the X axis) (x2,y2)

 Yk+1 = Yk + m (x1,y1)
x
Case 2 : If m>=1 y
(x2,y2)
 Xk+1 = Xk + (1/m)
m >= 1
 Yk+1 = Yk + 1 (step along Y axis)

(x1,y1)
x
Lines with negative slope

y
Case 1 : If |m| < 1 |m| < 1
(x1,y1)
 Xk+1 = Xk + 1 (step along the X axis)
(x2,y2)
 Yk+1 = Yk - m
x
Case 2 : If |m| >=1 y
(x1,y1)
4,12
 Xk+1 = Xk + (1/m) |m| >= 1

 Yk+1 = Yk - 1 (step along Y axis) (x2,y2) 7,3


x
DDA Line Drawing Algorithm
(Theory)
 DDA stands for Digital Differential Analyzer.
 It is an incremental method of scan conversion of line.
 In this method calculation is performed at each step but by using results
of previous steps.
 DDA algorithm calculates pixel positions along a line using equation
Δ y= m Δx
 This can be done by taking unit step along the one coordinate value and
calculating the other coordinate value.
 Consider the line segment specified by two end points (x1,y1) and (x2,y2).
DDA line drawing algorithm

For the Lines with Positive slope: y



m<1
If the slope m <= 1 , change X – coordinate value by 1 and
calculate corresponding y value as (x2,y2)
Yk+1 = Yk + m
(x1,y1)
x
 If the slope m > 1, change Y - coordinate value by 1 (step
along y axis) and calculate each succeeding x value as, y (x2,y2)
Xk+1 = Xk + (1/m)
m >= 1

 Here we have assumed that left end point is starting point i.e.
(x1,y1)
(x1,y1) and right end points is ending point i.e. (x2,y2). x
DDA Line Drawing Algorithm

 If these end points are reversed then, y


m<1
If m <1 then
Xk+1 = Xk-1 (step right to left) (x1,y1)
and Yk+1 = Yk - m
(x2,y2)
x
y
(x1,y1)
If m>1 then
Yk+1 = Yk-1 (step downwords) m >= 1
and Xk+1 = Xk – (1/m)
(x2,y2)
I x
DDA Line Drawing Algorithm

y
 For the Lines with Negative slope:
|m| < 1
 If |m| < 1 then
(x1,y1)

Xk+1 = Xk + 1 (step left to Right) (x2,y2)


and Yk+1 = Yk - m x

y
 If |m| >= 1 then (x1,y1)
Yk+1 = Yk -1 (step down words) |m| >= 1
and Xk+1 = Xk + (1/m)
(x2,y2)
x
DDA Line Drawing Algorithm

 If these end points are reversed then, y |m| < 1


 If |m| < 1 then
(x2,y2)
Xk+1 = Xk -1 (step right to left)
(x1,y1)
and Yk+1 = Yk + m
x

 If |m|>=1 Then y
(x2,y2)
Yk+1 = Yk + 1 (step upwords) |m| >= 1
and Xk+1 = Xk - (1/m)
(x1,y1)
x
Generalized DDA line drawing Algorithm

1. Input two endpoints of a line (x1,y1) and (x2,y2)


2. Plot the first point (x1,y1)
3. Compute dx = x2 – x1 and dy = y2 – y1
4. If abs(dx) > abs(dy) then
steps = abs (dx)
Else
steps = abs (dy)
5. Compute Δx = dx / steps
Δy = dy /steps
6. Starting k =1 and incrementing k by 1 each time perform the following
Xk+1 = Xk + Δx
Yk+1 = Yk + Δy
Plot (Round(Xk+1) , Round(Yk+1))
7. Repeat step 6 for steps times.
Limitations of DDA algorithm

1. The algorithm is orientation dependent. Hence the endpoint accuracy is


sometimes not attained.
2. The algorithm runs slowly as it involves floating point calculation and
divisions that takes time.
3. The accumulation of round off error in successive additions of floating point
increment can cause the calculates pixels to drift away from the true line
path for a long line segment
Problems on DDA Line drawing
algorithm
Problem 1: Draw a straight line from (0,0) to (4,5) using DDA line
algorithm
Here x1 = 0 , y1 =0 , x2 = 4, y2 =5 Step 6 : compute X = X + Xinc = X +
0.8
Step 1: calculate dx = x2 – x1 = (4 - 0) = 4
Y = Y + Yinc = Y
calculate dy = y2 – y1 = (5 - 0 ) = 5
+1
Step 2 : Here abs(dy) > abs (dx)
plot(round(x), (round(y))
so steps = abs(dy) = 5
Repeat step 6 for steps = 5 times
Step 3 : Compute xinc = dx / steps = 4 / 5 =
0.8
Compute yinc = dy /steps = 5 / 5 =1
Step 4 : set x = x1 , y = y1 i.e. x = 0 , y = 0
Step 5 : plot (x ,y ) i.e. plot(0 ,0)
Solution : Problem 1

K x y Round(X) Round(Y) (x,y)


0 0 0 0 (0,0)
1 0.8 1 1 1 (1,1)
2 1.6 2 2 2 (2,2)
3 2.4 3 2 3 (2,3)
4 3.2 4 3 4 (3,4)
5 4 5 4 5 (4,5)
Problem 2 : DDA Line

 Draw line from (10,6) to (15,9) using DDA


Here x1 = 10 , y1 = 6 , x2 = 15 , y2 = 9
Step 1: calculate dx = x2 – x1 = (15-10) = 5
calculate dy = y2 – y1 = (9 -6 ) = 3
Step 2: Here abd(dx) > abs (dy) so steps = abs(dx) = 5
Step 3: Compute xinc = dx / steps = 5 / 5 = 1
Compute yinc = dy /steps = 3/5 = 0.6
Step 4: set x = x1 and y = y1
x = 10 and y = 6
Step 5: plot (x ,y ) = ( 10,6)
Step 6: x = x + xinc = X + 1
y = y + yinc = Y + 0.6
plot (round(x), round(y)
Repeat step 6 for steps = 5 times
Solution : problem 2

x y Round(X) Round(Y (x,y)


)
10 6 10 6 (10,6
)
11 6.6 11 7 (11,7
)
12 7.2 12 7 (12,7
)
13 7.8 13 8 (13,8
)
14 8.4 14 8 (14,8
)
15 9.0 15 9 (15,9
)
Problem 3: DDA line

 Plot points for line AB - A (10,15) to B (5, 25) (Dec-19)


Here x1 = 10 , y1 = 15 , x2 = 5 , y2 = 25
Step 1: calculate dx = x2 – x1 = (5-10) = - 5
calculate dy = y2 – y1 = (25 -15 ) = 10
Step 2: Here abs(dy) > abs (dx) so steps = abs(dy) = 10
Step 3: Compute xinc = dx / steps = - 5 / 10 = - 0.5
Compute yinc = dy /steps = 10/10 = 1
Step 4: set x = x1 and y = y1
x = 10 and y = 15
Step 5: plot (x ,y ) = (10,15)
Step 6: x = x + xinc = x - 0.5
y = y + yinc = y + 1
plot (round(x), round(y)
Repeat step 6 for steps = 5 times
Solution : problem3
X y Round(X) Round(Y) (x,y) 2
5
10 15 10 15 (10,15 2
) 4
2
9.5 16 10 16 (10,16 3
) 2
9.0 17 9 17 (9,17) 2
2
8.5 18 9 18 (9,18) 1
2
8.0 19 8 19 (8,19) 0
1
7.5 20 8 20 (8,20) 9
1
7.0 21 7 21 (7,21) 8
1
6.5 22 7 22 (7,22) 7

6.0 23 6 23 (6,23) 1
6
5.5 24 6 24 (6,24) 1
5
5.0 25 5 25 (5,25) 1
1 2 3 4 5 6 7 8 9 10

4
Problem 4:

Plot points for line AB - A (5,5) to B (-10,-8)


Her x1 =5 , y1=5, x2 = -10 , y2 = -8
dx = x2-x1 = -10 – 5 = -15
dy = y2 –y1 = -8 -5 = -13
abs (dx) = 15 > abs (dy) =13
So steps = abs (dx) = 15
Now xinc = dx / steps = -15 /15 = -1
Yinc = dy / steps = -8 /15= -0.866
X y Round(X) Round(Y) (x,y)
5 5 5 5 (5,5)
1 4 4.134 4 4 (4,4)
2 3 3.267 3 3 (3,3)
3 2 2.401 2 2 (2,2)
4 1 1.535 1 2 (1,2)
5 0 0.669 0 1 (0,1)
6 -1 -0.196 -1 0 (-1,0)
7 -2 -1.063 -2 -1 (-2,-1)
8 -3 -1.929 -3 -2 (-3,-2)
9 -4 -2.7964 -4 -3 (-4,-3)
10 -5 -3.663 -5 -4 (-5,-4)
11 -6 -4.529 -6 -5 (-6,-5)
12 -7 -5.396 -7 -5 (-7,-5)
13 -8 -6.2626 -8 -6 (-8,-6)
14 -9 -7.1292 -9 -7 (-9,-7)
15 -10 -7.995 -10 -8 (-10,-8)
Bresenham’s Line Drawing

 Developed by Jack Bresenham.


 Unlike DDA, this algorithm uses Integer arithmetic.
 The pixel position is determined by finding the closest
integer coordinates to the actual line path.
Bresenham’s Line Drawing (Theory)

 Consider the starting and ending points of a


line are (x1,y1) & (x2,y2) resp.
 We compute slope m = Δy / Δx
 The equation of straight line : y= mx + c
 Since m<1 , we step along x axis
(i.e. increment X by 1 always).
 So, for x = Xk+1, value of Y on true line is

calculated as

Y = m (Xk+1) + C ------- (i)


 In the diagram there are two pints at Xk+1

1) A (Xk+1 , Yk+1)

2) B (X ,Y)
 Point C represents the point on the true line.
 We compute the distance of point A and B from C
BC = d1 and AC = d2
 If d1< d2 then point B(Xk+1, Yk) is plotted
Otherwise A (Xk+1, Yk+1) is plotted.
 By computing (d1- d2) we can know which point is
closer to true line.
if (d1 – d2 ) < 0 => d1 < d2 => Plot B(Xk+1, Yk)
if (d1 – d2 ) > 0 => d2 < d1 => Plot A(Xk+1 ,
Yk+1)
 So we compute d1 – d2
d1 = Y – Yk and d2 = Yk+1 – Y

But we know that for x = Xk+1 , value of Y is calculated as

Y = m (Xk+1) + C ------- (1)


Substitute Y value in equations of d1 and d2
d1 = m (Xk+1) + C – Yk …………..(ii)
d2 = Yk+1 - m (Xk+1) – C ………….(iii)
So, d1- d2 = m (Xk+1) + C – Yk – [Yk+1 - m (Xk+1) – C]
d1- d2 = m (Xk+1) + C – Yk – Yk - 1 + m (Xk+1) + C
d1-d2 = 2 m (Xk+1) – 2 Yk +2C -1

 But this parameter has a problem, It has m = ΔY / ΔX (which involves floating point

calculations. To remove this division we multiply both sides of equation by ΔX.

 So we define new decision parameter Pk = ΔX (d1 –d2)


Derive the equation of decision
parameter Pk

Rearranging above equation


Pk = 2 ΔY Xk - 2 ΔX Yk + [2 ΔY + ΔX (2C-1)]
………(ii)
Here, term 2 ΔY + ΔX (2C-1) is constant.

So, Pk = 2 ΔY Xk - 2 ΔX Yk ………….(iii)
So if Pk < 0 then we choose point B

so Xk+1 = Xk + 1

Yk+1 = Yk

If Pk >=0 then we choose point A

so, Xk+1 = Xk + 1

Yk+1 = Yk + 1
Derive the equation of Next decision
parameter Pk+1
 Next decision parameter Pk+1 at (Xk+1 , Yk+1) is calculated as,
 Pk+1 = 2 ΔY Xk +1 - 2 ΔX Yk+1 (Replace Xk by Xk+1 And Yk by Yk+1 In eq of Pk )

 Pk+1 – Pk = [2 ΔY Xk +1 - 2 ΔX Yk+1]- [2 ΔY Xk - 2 ΔX Yk ]
= 2 ΔY Xk +1 - 2 ΔX Yk+1 - 2 ΔY Xk + 2 ΔX Yk
= 2 ΔY (Xk +1 – Xk ) - 2 ΔX (Yk+1 – Yk)
 But we know Xk+1 = Xk +1 always ( for m < 1 we take unit step in X direction)
= 2 ΔY (Xk + 1 – Xk ) - 2 ΔX (Yk+1 – Yk)
= 2 ΔY - 2 ΔX (Yk+1 – Yk)

 Pk+1 = Pk + 2 ΔY - 2 ΔX (Yk+1 – Yk) ………………. (iv)


Pk+1 = Pk + 2 ΔY - 2 ΔX (Yk+1 – Yk) Points to remember:

………………..(v) If pk < 0
Xk+1 = Xk +1
If pk < 0 means we have chosen Yk+1 = Yk Yk+1 = Yk
Pk+1 = Pk + 2 ΔY
so Pk+1 = Pk + 2 ΔY - 2 ΔX (Yk – Yk)
Else
Pk+1 = Pk + 2 ΔY Xk+1 = Xk +1
Yk+1 = Yk +1
Else we chose Yk+1 = Yk + 1 Pk+1 = Pk + 2 ΔY - 2 ΔX

soabove
The Pk+1 =derivation
Pk + 2 ΔYis-restricted
2 ΔX (Yk +to1– Yk)line whose slope is < 1. How ever similar derivation can
the
be obtained for the line whose slope is >= 1.
Derive the Initial value of Pk

Let initial point be (x0, y0)

And Pk = 2 ΔY Xk - 2 ΔX Yk + [2 ΔY + ΔX (2C-1)] (Eq


(ii))
So P0 = 2 ΔY X0 - 2 ΔX Y0 + [2 ΔY + ΔX (2C-1)]
Substitute c = y – mx
P0 = 2 ΔY X0 - 2 ΔX Y0 + 2 ΔY + ΔX [2 (Y0-mX0) -1]
substitute m = ΔY / ΔX
P0 = 2 ΔY X0 - 2 ΔX Y0 + 2 ΔY + ΔX [2(Y0- (ΔY / ΔX) X0) - 1]

P0 = 2 ΔY X0 - 2 ΔX Y0 + 2 ΔY + 2 ΔX Y0 – 2 ΔY X0 – ΔX

P0 = 2 ΔY – ΔX
Bresenham’s Line drawing
algorithm(m<1)
1. Input two end points of a line (x1,y1) and (x2,y2)
2. Plot the first point (x1,y1)
3. Calculate ΔX = |x2 – x1 | and ΔY = |Y2 –Y1|
4. Evaluate Initial decision parameter P1 = 2Δy – ΔX
5. At each Xk along the line, starting at k=1, perform the following
6. If (Pk < 0) plot (Xk+1, Yk+1 ) = (Xk+1 , Yk)
and Pk+1 = Pk + 2 ΔY
Else plot (Xk+1, Yk+1 ) = (Xk+1 , Yk + 1)
And Pk+1 = Pk + 2 ΔY - 2ΔX
7. Repeat step 6 for ΔX times
Yk+
Yk+
1
1
Yk Yk

Xk Xk+ Xk
1 Xk+
1

m < 1 ( dy < dx ) m > 1 ( dy > dx )

If Pk < 0 => (Xk + s1, Yk) If Pk < 0 => (Xk, Yk + s2)


If Pk>=0 => (Xk + s1, Yk If Pk>=0 => (Xk + s1, Yk +
+s2 ) s2 )
Generalized Bresenham’s Line
drawing algorithm

1. Input two end points of line (x1,y1) and (x2,y2)


2. Plot the first point (x1,y1)
3. Calculate ΔX = |x2 – x1 | and ΔY = |Y2 –Y1|
4. Initialize s1 and s2 as
If(x2 - x1) < 0 then s1 = -1 // Moving from Right to Left
otherwise
If(x2-x1) >= 0 then s1 = +1 // Moving from Left to right
otherwise
If(Y2-Y1) < 0 then s2 = -1 // Moving from Top to bottom
otherwise
If(Y2-Y1) >=0 then s2 = +1 // Moving from Bottom to top
5. If (ΔY > ΔX) , then swap the content of ΔX and ΔY //(for m>1swap the dy and dx)
6. Evaluate decision parameter Pk = 2Δy – ΔX
7. At each Xk along the line, staring at k=1, perform the following
If (Pk < 0) , then plot the next point as (Xk+1, Yk+1) = (Xk+ s1 , Yk)
(If swapping is done then plot next point as (Xk+1, Yk+1) = (Xk , Yk+ s2)
and Pk+1 = Pk + 2 ΔY
Otherwise If (Pk >= 0) , then plot the next point as (Xk+1, Yk+1) = (Xk+ s1 , Yk + s2)
and Pk+1 = Pk + 2 ΔY - 2 ΔX
8. Repeat step 7 for ΔX times.
Advantages / Disadvantage of Bresenham Line drawing algo.

Advantage:
1. It involves only integer arithmetic, so it is simple.
2. It can be implemented using hardware because it does not use multiplication
and division.
3. It is faster as compared to DDA (Digital Differential Analyzer) because it does
not involve floating point calculations like DDA Algorithm.
Disadvantage:
This algorithm is meant for basic line drawing only. Still, it cannot avoid Jagged
effect.
Problem : Bresenham’s Line

Problem 1: Calculate the pixel positions along a straight line between P1(20,20) and
P2(10,12)
y
Solution: x1= 20, Y1 = 20 , X2 = 10 , Y2 =12
P1(20,20)
Step 1: Compute ΔX = |x2 – x1| = |10 - 20| = 10
ΔY = |Y2 – Y1| = |12 – 20| = 8
Since (ΔX > ΔY ) this is the case of m <1.
Step 2: since X2 – X1 < 0 => s1 = -1
and Y2 – Y1 < 0 => s2 = -1 P2
(10,12)
Step 3: Plot the first point (X1,Y1) = (20,20)
x
Step 4: Compute initial decision parameter
P1 = 2ΔY – ΔX = 2*8 – 10 = 6
Iterat Pk Xk+1 Yk+1 Plot Pk+1 (next Decision If Pk < 0
ion parameter) Xk+1 = Xk -1
No. Yk+1 = Yk
Initially 20 20 (20,20) P1= 2ΔY – ΔX = 2*8 – 10 = 6 Pk+1 = Pk + 2 ΔY
1 P1 = 6 > 0 19 19 (19,19) P2 = P1 + 2ΔY - 2ΔX
= 6 + 16 – 20 = 2 Else
2 P2 =2 > 0 18 18 (18,18) P3 = P2 + 2ΔY - 2ΔX Xk+1 = Xk - 1
= 2 + 16 -20 = -2 Yk+1 = Yk - 1
3 P3 = -2 < 0 17 18 (17,18) P4 = P3 + 2ΔY Pk+1 = Pk + 2 ΔY - 2
= -2 + 16 = 14 ΔX
4 P4 = 14 > 16 17 (16,17) P5 = P4 + 2ΔY - 2ΔX y
0 =14 + 16 – 20 =10
5 P5 = 10 > 15 16 (15,16) P6 = P5 + 2ΔY - 2ΔX 20 (20,20)

0 = 10 + 16 -20 =6
6 P6 = 6 > 0 14 15 (14,15) P7 = P6+ 2ΔY - 2ΔX
= 6 +16 -20 = 2
7 P7 = 2 > 0 13 14 (13,14) P8 = P7 + 2ΔY - 2ΔX
= 2 +16 -20 = -2 (10,12)
8 P8= -2 < 0 12 14 (12,14) P9 = P8+ 2ΔY 10
= -2 + 16 = 14
10 20
9 P9 = 14 > 11 13 (11,13) P10 = P9 + 2ΔY - 2ΔX x
0 = 14 + 16 -20 = 10
10 P = 10 10 12 (10,12) P = P + 2ΔY - ΔX
 Problem 2: Identify the pixel positions along a line between A(10,10) and B(18,16)
using Bresenham's line algorithms (MAY 2018)
y
Solution: x1= 10, Y1 = 10 , X2 = 18 , Y2 =16
Step 1: Compute ΔX = |x2 – x1| = |18 - 10| = 8
ΔY = |Y2 – Y1| = |16 – 10| = 6 B
(18,16)
Since (ΔX > ΔY ) this is the case of m <1.
Step 2: since X2 – X1 > 0 => s1 = 1
and Y2 – Y1 > 0 => s2 = 1 A (10,10)

Step 3: Plot the first point (X1,Y1) = (10,10)


x
Step 4: Compute initial decision parameter
P1 = 2ΔY – ΔX = 2*6 – 8 = 4
Itera Pk Xk+1 Yk+1 Plot Pk+1 (next Decision If pk < 0
tion parameter) Xk+1 = Xk +1
No.
Yk+1 = Yk
Initailly 10 10 (10,10) P1 =2ΔY – ΔX = 2*6 – 8 = 4
Pk+1 = Pk + 2 ΔY
1 P1 = 4 > 0 11 11 (11,11) P2 = P1 + 2ΔY - 2ΔX
= 4 + 12 – 16 = 0
Else
2 P2 = 0 >= 12 12 (12,12) P3 = P2 + 2ΔY - 2ΔX Xk+1 = Xk +1
0 = 0 + 12 - 16 = -4
Yk+1 = Yk +1
3 P3 = -4 < 13 12 (13,12) P4 = P3 + 2ΔY Pk+1 = Pk + 2 ΔY - 2 ΔX
0 = -4 + 12 = 8
4 P4 = 8 > 0 14 13 (14,13) P5 = P4 + 2ΔY - 2ΔX y
=8 + 12 – 16 = 4
5 P5 = 4 > 0 15 14 (15,14) P6 = P5 + 2ΔY - 2ΔX
(18,16
= 4 + 12 - 16 = 0 )
6 P6 = 0 >= 16 15 (16,15) P7 = P6 + 2ΔY - 2ΔX
0 = 0 +12 -16 = -4
7 P7 = -4 < 17 15 (17,15) P8 = P7 + 2ΔY
0 = -4 +12 = 8
(10,10)
8 P8 = 8 18 16 (18,16) P9 = P8 + 2ΔY - 2ΔX
>= 0 = 8 + 12 - 16 = 4 x
 Problem 3: Calculate the pixel positions along a straight line between P1(10,8) and P2(5,15)
 Here x1 = 10 , y1 =8, x2=5, y2 = 15
Step1: Compute ΔX = |x2 – x1| = |5 - 10| = 5
ΔY = |Y2 – Y1| = |15 – 8| = 7
Since ΔY > ΔX => m>1 P2(5,1
Step 2: (decide sign of xinc and yinc) 5)

Here x2 - x1 < 0 => s1 = -1


And y2 - y1 > 0 => s2 = +1 P1(10,
8)
So x value will be decremented and y value will be incremented.
Step 3: If ΔY > ΔX (i.e. |m| >1 ) then swap the values
So, ΔX = 7 , ΔY = 5
Step 4 : Compute initial decision parameter
P1 = 2 ΔY – ΔX = (2*5 ) – 7 = 3
Itera Pk Xk+1 Yk+1 Plot Pk+1 (next Decision
tion
After swap ΔX =7 , ΔY =5
parameter)
No.
Initially 10 8 (10,8) P1 =2ΔY – ΔX = 2*5 – 7 = 3
Pk>=0
Xk+1 = Xk + s1 = Xk -1
1 P1 = 3 > 0 9 9 (9,9) P2 = P1 + 2ΔY - 2ΔX
Yk+1 = Yk + s2 = Yk+1
= 3 + 10 – 14 = -1
Pk+1 = Pk + 2 ΔY - 2 ΔX
2 P2 = -1 9 10 (9,10) P3 = P2 + 2ΔY
<0 = -1 + 10 = 9
Pk<0
3 P3 = 9 > 0 8 11 (8,11) P4 = P3 + 2ΔY - 2ΔX
(if m>1) then,
= 9 + 10 - 14 = 5
Xk+1 = Xk
4 P4 = 5 > 0 7 12 (7,12) P5 = P4 + 2ΔY - 2ΔX
Yk+1 = Yk + s2 = Yk+1
=5 + 10-14 = 1
and Pk+1 = Pk + 2 ΔY
5 P5 = 1 > 0 6 13 (6,13) P6 = P5 + 2ΔY - 2ΔX
= 1 + 10 - 14 = -3
6 P6 = -3 < 6 14 (6,14) P7 = P6 + 2ΔY
0 = -3 +10= 7
7 P7 = 7 > 0 5 15 (5,15) P8 = P7 + 2ΔY - 2ΔX
= -4 +10 -14 = -8
Points To 15
Plot 14
(10,8)
(9,9)
13
12
(9,10) 11
(8,11) 10
(7,12)
9
(6,13)
8
(6,14)
1 2 3 4 5 6 7 8 9 10
(5,15)
Properties of a circle

 Equation of a circle with center (Xc,Yc) and radius r is,


(X – Xc) 2
+ (Y – Yc) 2
= r2
 We can first set up our algorithm to calculate pixel positions around a circle with
origin (0, 0).
 Then each calculated position (x, y) is moved to its proper screen position by
adding
Xc to X and Yc to Y
 In the first octant of a circle (i.e. from x = 0 to x = y), the slope of the curve
varies from 0 to −1 i.e. (|m|<1) .
 Therefore, we can take unit steps in the positive x direction over this octant and
use a decision parameter to determine which of the two possible Y values.
 To apply the midpoint method, we define a circle function as
fcircle (x, y) = x2 + y2 − r 2

1. If fcircle (x, y) < 0, then (x, y) is inside the circle boundary


2. If fcircle (x, y) = 0, then (x, y) is on the circle boundary
3. If fcircle (x, y) > 0, then (x, y) is outside the circle boundary
Mid Point Circle Drawing Algorithm
(Theory)
Basic Concept:

 For the first octant x< y, and also X is increasing and Y is


decreasing.
 We increase X as unit interval and corresponding Y value is
computed.
 So the next point to be plotted is either
(Xm,Ym
(xk + 1 , Yk ) or (Xk+1 , Yk - 1) )

 We compute the mid point of these two points (Xm,Ym)


Xm = (xk + 1 + xk + 1)/2
Ym = (Yk – 1 + Yk) / 2
(Xm , Ym ) = (Xk + 1 , Yk – ½)
Deriving equation of Calculating Decision Parameter
Calculating Next Decision Parameter Pk+1

Pk+1 = Pk + 2(Xk+1) + { (Yk +1) 2


– (Yk)2} – (Yk+1 – Yk) +1

Where Yk+1 is either Yk or (Yk-1) depending upon the sign of Pk


 Case 1: If Pk< 0 then we substitute Yk+1 = Yk
Pk+1 = Pk + 2(Xk+1) + { Yk2 – Yk2 ) } - (Yk–Yk) +1
Pk+1 = Pk + 2Xk + 3
 Case 2: If Pk>=0 then we substitute Yk+1 = Yk – 1
Pk+1 = Pk + 2(Xk+1) + { (Yk-1)2 – Yk2 ) } – ( Yk – 1 – Yk) + 1
= Pk + 2(Xk+1) + { Yk2 – 2Yk + 1 - Yk2} + 1 + 1
= Pk + 2(Xk+1) – 2Yk + 1 + 1 + 1
Pk+1 = Pk + 2Xk – 2Yk + 5
(Xk+1 , Yk)
 If Pk< 0
Pk+1 = Pk + 2Xk + 3 Pk < 0
Xk+1 = Xk + 1
Yk+1 = Yk (Xk+1 , Yk-1)

 Other wise
Pk+1 = Pk + 2Xk – 2Yk + 5 (Xk+1 , Yk)

Xk+1 = Xk + 1 Pk > 0
Yk+1 = Yk - 1
(Xk+1 , Yk-1)
Calculating Initial Decision Parameter
P0
Starting point to plot is (X0,Y0) = (0,r)

Substitute X0 = 0 and Y0 = r in the equation of Pk to compute initial

parameter P0
We know that,
Pk = (Xk + 1)2 + (Yk – ½)2 – r2

P0 = (X0 + 1)2 + (Y0 – ½)2 – r2


Mid Point Circle Drawing Algorithm

Step 1: Input center(Xc , Yc) and Radius r of a circle.


Step 2: Set the starting point as (X0,Y0) = (0,r)

Step 3: Plot the first point (0,r) and the symmetrical points in other seven quadrants.
Step 4: Computer initial decision parameter P 0 = 1 – r

Step 5: starting at K = 0 and incrementing k each time by 1, performing the following.


If (Pk < 0 ) then next point along the circle centered at (0,0) is (X k + 1, Yk)

and Pk+1 = Pk + 2Xk+ 3

otherwise,
Next point along the circle is ( Xk + 1, Yk - 1)

and Pk+1 = Pk + 2Xk - 2Yk +5

Step 6: Determine the symmetry points in the other seven quadrants.


Step 7: Move each calculated pixel (x,y) onto the circular path centered at (Xc, Yc).
Step 8: Repeat step 5 TO 7 WHILE X <= Y
Input center (Xc , Yc) and Radius r
x = 0 , y = r ; // set the initial point
P= 1–r // initial decision parameter
do {
Plot (Xc + X , Yc + y)
Plot (Xc + X , Yc - y)
Plot (Xc - X , Yc + y)
Plot (Xc - X , Yc - y)
Plot (Xc + y ,Yc + X)
Plot (Xc + y , Yc - X)
Plot (Xc - y , Yc + X)
Plot (Xc - y , Yc - X)
If (P < 0 )
{ P = P + 2 * X+ 3
x=x+1 }
Else
{ P=P+2*X -2*Y +5
x= x+1
Y=Y–1
}
} While (x<= y)
8-way Symmetry of a circle

 Plotting 8 – way Symmetry


Plot (Xc + X , Yc + y)
Plot (Xc + X , Yc - y)
Plot (Xc - X , Yc + y)
Plot (Xc - X , Yc - y)
Plot (Xc + y ,Yc + X)
Plot (Xc + y , Yc - X)
Plot (Xc - y , Yc + X)
Plot (Xc - y , Yc - X)
Problem on Mid point Circle

Problem 1 : Given the center point coordinates


(0, 0) and radius as 10, generate all the points
to form a circle using Midpoint algorithm
Solution (Problem 1) : Here center Xc =0 , Yc =0 and radius r =10

No. Pk Xk+1 Yk+1 Plot Pk+1  If Pk< 0


0 - X0 = Y0 = (0,10) P0 = 1 – r = 1 – 10 = -9
Xk+1 = Xk + 1
0 10
1 P0 = -9 < X1 = Y1 = (1,10) P1 = P0 + 2X0 + 3 Yk+1 = Yk
0 1 10 = -9 + (2*0) + 3 = -6
And Pk+1 = Pk + 2Xk +
2 P1 = -6 < X2 Y2=10 (2,10) P2 = P1 + 2X1 + 3
3
0 =2 = -6 + (2*1) + 3 = -1
3 P2 = -1 < X3 = Y3 (3,10) P3 = P2 + 2X2 + 3
0 3 =10 = -1 + (2*2) + 3 = 6  Other wise
4 P3 = 6 > X4 = Y4 = 9 (4, 9) P4 = P3 + 2X3 – 2Y3 + 5
Xk+1 = Xk + 1
0 4 = 6 + (2*3) –(2*10)+5
= -3 Yk+1 = Yk - 1
5 P4 = -3 < X5 = Y5 = 9 (5, 9) P5 = P4 + 2X4 + 3
0 5 = -3 + (2*4) + 3 = 8 And Pk+1 = Pk + 2Xk –
6 P5 = 8 > X6 Y6 = 8 (6, 8) P6 = P5 + 2X5 – 2Y5 + 5 2Yk + 5
0 =6 = 8 + (2*5) –(2*9) + 5=
Problem 1 : Final plot

No. Plot
0 (0,10)
1 (1,10)
2 (2,10)
3 (3,10)
4 (4, 9)
5 (5, 9)
6 (6, 8)
7 (7, 7)
Problem 2: Specify midpoint circle algorithm.
Using the same , plot the circle whose radius is 8 and center is
(10,10) (Dec – 2018)

Here radius r = 8

center (Xc,Yc) = (10,10)

We first compute pixel positions assuming center as (0,0) and then


each calculated pixel can be moved to circle centered around
(10,10)

Set X0 = 0 and Y0 = r =8
Solution: Problem 2
No. Pk Xk+1 Yk+1 Plot Pk+1  If Pk< 0
0 - X0 = Y0 = 8 (0 , 8) P0 = 1 – r = 1 – 8 = -7 Xk+1 = Xk + 1
0
1 P0 = -7 < X1 = Y1 = 8 (1, 8 ) P1 = P0 + 2X0 + 3 Yk+1 = Yk
0 1 = -7 + (2*0) + 3 = -4 And Pk+1 = Pk + 2Xk + 3
2 P1 = -4 < X2 = Y2= 8 (2, 8 ) P2 = P1 + 2X1 + 3
0 2 = -4 + (2*1) + 3 = 1
3 P2 = 1 > X3 = Y3 =7 (3, 7) P3 = P2 + 2X2 – 2Y2 + 5  Other wise
0 3 = 1 + (2*2) – (2*8) +5 Xk+1 = Xk + 1
=-6
4 P3 = -6 < X4 = Y4 = 7 (4, 7) P4 = P3 + 2X3 + 3 Yk+1 = Yk - 1
0 4 = -6 + (2*3) + 3 = 3
And Pk+1 = Pk + 2Xk –
5 P4 = 3 > X5 = Y5 = 6 (5, 6) P5 = P4 + 2X4 – 2Y4+ 5 2Yk + 5
0 5 = 3 + (2*4) – ( 2*7) +
5=2
6 P5 = 2 > X6 Y6 = 5 (6, 5) We stop here as x>y. we
0 =6 don’t plot point (X6, Y6)
Final Plot

All Calculated pixel positions are for the circle


centered at (0,0). So next step is to move all
these pixels on the circle path centered around 18
(10,10). 17
This can be done by adding
Xc = 10 to X and Yc=10 to Y values. 16

No. (x,y) Plot (X+Xc , Y+Yc) 15

0 (0 , 8) (10,18) 14

1 (1, 8 ) (11,18) 13

2 (2, 8 ) (12,18) 12

3 (3, 7) (13,17) 11

4 (4, 7) (14,17) 10

5 (5, 6) (15,16) 10 11 12 13 14 15 16 17 18
Mid Point ellipse Drawing Algorithm
(Theory)
 We assume that an ellipse is centered around origin (0,0).
 Unlike circle, an ellipse has 4- way symmetry.

 We plot the points for the first quadrant. Points in the


y
other quadrants can be then computed using reflection. m <-1
 The first quadrant is divided into two regions. (Ref. fig)
m = -1

 If we draw tangent at any point in region 1 , the slope of ry region1


m > -1
the tangent m < -1 (Shown by Blue line) region2
 So in region1, we increment x by unit interval and find
rx x
corresponding Y values.

 Similarly, if we draw a tangent at any point in region 2,


then slope of that tangent m > -1 (Shown by Green line).
 So we decrement Y by unit interval and find corresponding
X values.

 And at the boundary of region 1 and region2 , slope = -1


(shown by Red line)
Equation of an ellipse with center (0,0) and radius on major axis r x and minor axis ry
is given as.
fellipse(x,y) = = 1

Multiplying both sides by ( rx2 . ry2) we get,

If fellipse(x,y) < 0 => point (x,y) is inside the ellipse


fellipse(x,y) = 0 => Point (x,y) is on the ellipse
fellipse(x,y) > 0 => Point (x,y ) is outside the ellipse

This function fellipse(x,y) serves as a decision parameter.


Calculating slope of the curve
(optional for theory)
 Equation of the ellipse with major axis ‘a’ and minor
axis ‘b’ is
 Take derivative w.r.t.

 Replace ‘a’ by ‘rx’ and ‘b’ by ‘ry’


 m=
 We start plotting at (0, ry).
 we take unit steps in the x direction until we reach the boundary between region 1
and region 2
 Then we switch to region 2 and we take unit steps in the y direction over the
remainder of the curve in the first quadrant.
 At each step, we need to test the value of the slope of the curve.
 The ellipse slope is calculated as

 At the boundary between region 1 and 2 slope becomes -1 and

 Therefore we move out of region 1 when


Region 1: Derivation
In region 1, we choose (Xk+1,Yk) or (Xk+1 , Yk-1) => Midpoint ((xk+1, yk – ½))
We substitute midpoint (xk+1, yk – ½) in the equation of ellipse.
Eqn of ellipse : ry2 x2 + rx2 y2 – rx2 ry2 = 0
P1K = ry2 (xk+1)2+ rx2 (yk – ½)2 – rx2ry2 --------------------- (1)

P1k+1 = ry2 (xk+1+1)2+ rx2 (yk+1 – ½)2 – rx2ry2


P1k+1 = ry2 ((xk+1)+1)2 + rx2 (yk+1 – ½)2 – rx2ry2 --------------------(2)
{ Substitute (xk+1 = xk+1) }
P1k+1 - P1K = ry2 ((xk+1)+1)2 - ry2 (xk+1)2 + rx2 (yk+1 – ½)2 - rx2 (yk – ½)2} - rx2ry2 + rx2ry2
= ry2 {(xk+1)2 + 1 + 2(xk+1)} - ry2 {(xk2+1 + 2Xk) } + rx2 ( Yk+12 + ¼ - Yk+1 ) – rx2 ( yk2+ ¼ –
Yk )
= ry2 ( xk2+1 + 2Xk + 1 + 2xk+2) - ry2 (xk2+1 + 2Xk ) + rx2 ( Yk+12 + ¼ - Yk+1 ) – rx2 ( yk2+ ¼ –Yk)
=ry2 (xk2 + 1 + 2Xk + 1 + 2xk+2 – xk2 -1-2Xk) + rx2 ( Yk+12 + ¼ - Yk+1 - yk2- ¼ +Yk )
P1k+1 = P1k + ry2 ( 2 (xk+1) +1) + rx2 (Yk+12 - yk2 - Yk+1 +Yk )
 P1k+1 = P1K + ry2 (2(xk+1) +1) + rx2 { (Yk+12 - yk2) – (Yk+1 - Yk ) }
Case 1: If P1k < 0 we choose A(Xk+1,Yk) , substitute Yk+1 = Yk
 P1k+1 = P1K + ry2 (2(xk+1) +1) + 0
 P1k+1 = P1K + ry2 (2xk + 3)
 P1k+1 = P1K + 2 ry2 xk + 3ry2
Case 2: If P1k >= 0 we choose , B (Xk+1,Yk - 1), substitute Yk+1 = Yk -1
 P1k+1 = P1K + ry2 (2 (xk+1) +1) + rx2 { ((Yk-1)2 - yk2) – (Yk-1 - Yk ) }
 = P1K + ry2 (2xk+ 3) + rx2 { Yk2 – 2Yk + 1 –Yk2 – Yk + 1 + Yk}
 = P1K + ry2 (2xk+3) + rx2 { -2Yk + 2}
 = P1K + 2ry2 xk + 3ry2 - 2 rx2 Yk + 2rx2
 P1k+1 = P1K + 2ry2 xk– 2 rx2 Yk + 3ry2 + 2rx2
Initial Decision parameter for Region1:
 Put (0,ry) in equation of P1K = ry2 (xk+1)2+ rx2 (yk – ½)2 – rx2ry2
 P10 = ry2 (0+1)2+ rx2 (ry – ½)2 – rx2ry2
 = ry2 + rx2 ( ry2 + ¼ - ry ) - rx2ry2
 =
ry2 + rx2 ry2 + rx2/4 – rx2 ry - rx2ry2
 P10 = ry2 + rx2/4 – ry rx2
Region 2: Derivation
In region 2, we choose (Xk,Yk - 1) or (Xk+1 , Yk-1) => Midpoint (xk+1/2, yk – 1)
We substitute midpoint (xk+1/2, yk – 1) in the equation of ellipse.
Eqn of ellipse : ry2 x2 + rx2 y2 – rx2 ry2 = 0
 P2k = ry2 (xk + ½)2 + rx2 (yk-1)2 – rx2 ry2 ---------------- (1)
 P2k+1 = ry2 (xk+1 + ½)2 + rx2 (yk+1-1)2 – rx2 ry2
 P2k+1 = ry2 (xk+1 + ½)2 + rx2 ((Yk-1)-1)2 – rx2 ry2 ----------------(2)
 (2) – (1)
 P2k+1 – P2k = [ ry2 (xk+1 + ½)2 - ry2 (xk + ½)2 ] + [rx2 ((Yk-1)-1)2 - rx2 (yk-1)2 ] - rx2 ry2 + rx2 ry2
 = [ry2 (xk+12 + ¼ + xk+1) - ry2 (xk2 + ¼ + Xk) ] + [ rx2 ( (Yk – 1)2 + 1 - 2(Yk-1) ) –rx2 (Yk-1)2 ]
 = ry2 [ (xk+12 + ¼ + xk+1 - xk2 - ¼ - Xk ) ] + rx2 [ (Yk – 1)2 + 1 - 2(Yk-1) - (Yk-1)2 ]
 P2k+1= P2k + ry2 [ (xk+12 - xk2 + xk+1 - Xk ) ] + rx2 [1 - 2(Yk-1)]
 P2k+1= P2k + rx2 - 2 rx2 (Yk-1) + ry2 [ (xk+12 - xk2 + xk+1 - Xk ) ]
 P2k+1= P2k + rx2 - 2 rx2 (Yk-1) + ry2 [ (xk+12 - xk2 + xk+1 - Xk ) ]

Case 1: If P2k >=0 we choose (Xk , Yk -1), substitute Xk+1 = Xk


 P2k+1= P2k + rx2 - 2 rx2 (Yk-1) + 0
 P2k+1= P2k + rx2 - 2 rx2Yk + 2rx2
 P2k+1= P2k - 2 rx2Yk + 3rx2

Case 2: if p2k<0 we choose (Xk+1 , Yk-1 ), Substitute XK+1 = Xk +1


 P2k+1= P2k + rx2 - 2 rx2 (Yk-1) + ry2 [ ((Xk+1)2 - xk2 + (Xk+1) - Xk ) ]
 P2k+1= P2k + rx2 - 2 rx2 (Yk-1) + ry2 [ xk2 + 1 + 2Xk - xk2 + Xk+1 - Xk ) ]
 P2k+1= P2k + rx2 - 2 rx2 (Yk-1) + ry2 [2Xk+2 ]
 P2k+1= P2k + rx2 - 2 rx2 Yk + 2 rx2 + 2ry2 Xk+2 ry2
 P2k+1= P2k + 2ry2 Xk - 2 rx2 Yk + 3 rx2 +2 ry2

Initial Decision parameter for region2:


 We compute initial decision parameter for region 2 by putting the last point in region 1
in the eq P2 = r 2 (x + ½)2 + r 2 (y -1)2 – r 2 r 2
Mid point ellipse drawing algorithm

 Step 1: Input rx , ry, and ellipse center (xc , yc ), and obtain the first point on an ellipse
centered on the origin as (x0,Y0) = (0,ry).
 Step 2: Calculate the initial decision parameter in region 1 as
p10 = ry2− rx2 ry + ¼ rx2
 Step 3: At each xk position in region 1, starting at k = 0, perform the following test.
If p1k < 0, the next point along the ellipse centered on (0, 0) is (xk+1, yk) and
P1k+1 = P1K + 2 ry2 xk + 3ry2
Otherwise
Next point to be plotted is (xk+1, yk - 1)
P1k+1 = P1K + 2ry2 xk – 2 rx2 Yk + 3ry2 + 2rx2
Repeat step3 until (2 ry2 x) >= (2 rx2 y )
Step 4 : Calculate the initial value of the decision parameter in region 2 as
p20= ry2 (x0 + ½)2 + rx2 (y0 -1)2- rx2ry2
Where (x0,y0) is the last point of region 1.
Step 5: At each yk position in region 2, starting at k = 0, perform the following test.
If p2k >= 0, the next point along the ellipse centered on (0, 0) is (xk , yk − 1) and
P2k+1= P2k - 2 rx2Yk + 3rx2
Otherwise, the next point is (xk + 1, yk - 1) and
P2k+1= P2k + 2ry2 Xk - 2 rx2 Yk + 3 rx2 +2 ry2
Repeat step 5 while y>=0
Step 6: Obtain the symmetric points in the three quadrants and plot the coordinate value as:

x=x+xc and y=y+yc


Read radius rx and ry and center Xc, Yc // start of region 2
x=0 and y=ry // initial first point P = ry2 (x + ½)2 + rx2 (y-1)2 – rx2 ry2
// start of region 1 // Initial parameter for region2

Do
Algorithm
p= ry2 + ¼ rx2 – rx2 ry2 // initial parameter While (y>=0)
{ if (p >= 0 )
{
{ P = P - 2 rx2 * Y + 3 * rx2
Plot(Xc + X , Yc + Y)
y = y -1
Plot(Xc + X , Yc - Y)
}
Plot(Xc - X , Yc + Y)
Plot(Xc - X , Yc - Y) Else

if (p < 0 ) { P = P + 2ry2 X - 2 rx2 Y + 3 rx2 + 2 ry2


{ P = P + 2 * r y2 x + 3 * r y2 x = x+ 1
x = x+ 1 y = y -1
} }
Else Plot (x + Xc ,y + Yc)
{ P = P + 2* ry2 * x – 2 * rx2 Y + 3 * ry2 + 2 *rx2 Plot (x + Xc ,y - Yc)
X = X+1 Plot (x - Xc ,y + Yc)
Y = y-1 Plot (x - Xc ,y - Yc)
} }
} While ( (2*ry * x) < (2 * rx * y))
2 2
Read radius rx and ry and center Xc, Yc
// start of region 2
x=0 and y=ry // initial first point
// start of region 1 P = ry2 (x + ½)2 + rx2 (y-1)2 – rx2 ry2
p= ry2 + ¼ rx2 – rx2 ry2 // initial parameter // Initial parameter for region2
Algorithm
//plot the symmetry of first point
Plot(Xc + X , Yc + Y)
While (y>0)
Plot(Xc + X , Yc - Y) { if (p >= 0 )
Plot(Xc - X , Yc + Y)
{ P = P - 2 rx2 * Y + 3 * rx2
Plot(Xc - X , Yc - Y)
While((2*ry2 * x) < (2 * rx2 * y)) y = y -1
{
}
if (p < 0 )
{ P = P + 2 * ry2 x + 3 * ry2 Else
x = x+ 1 { P = P + 2ry2 X - 2 rx2 Y + 3 rx2 + 2 ry2
}
Else x = x+ 1
{ P = P + 2* ry2 * x – 2 * rx2 Y + 3 * ry2 + 2 *rx2
y = y -1
X = X+1
Y = y-1 }
} Plot (x + Xc ,y + Yc)
Plot(Xc + X , Yc + Y)
Plot(Xc + X , Yc - Y)
Plot (x + Xc ,y - Yc)
Plot(Xc - X , Yc + Y) Plot (x - Xc ,y + Yc)
Plot(Xc - X , Yc - Y)
Plot (x - Xc ,y - Yc)
}
} While ()
Problems Based on Ellipse

Problem 1 : Compute points in region 1 for the ellipse


centered at (0,0) with rx = 8 and ry = 6
Solution:
 Here Xc = 0 , Yc =0
 Rx = 8 and Ry =6
 For region1 we compute till (2 * ry2 * x) < = (2 * rx2 * y)
 We plot initial point as (x0 ,y0 ) = (0, ry) =(0, 6)
No Pk Xk+1 Yk+1 Plot Pk+1 2*ry2*xk+1 2*rx2*yk+1

0 - X0 = 0 Y0 = 6 (0 , 6) P10 = ry2 + 1/4 rx2 – rx2ry = (36)+ ¼*64 – (64*67) 0 0


= -332
1 P0 = -332 < X1 = 1 Y1 = 6 (1, 6 ) P11 = P0 + 2 * ry2 x0 + 3 * ry2 (2*36*1) (2*64*6)
0 = -332 + (2*36*0) + 3 *36 = -224 = 72 = 768
2 P1 = -224 < X2 = 2 Y 2= 6 (2, 6 ) P12 = P1 + 2 * ry2 x1 + 3 * ry2 (2*36*2) (2*64*6)
0 = -224 + (2*36*1) + (3*36) = -44 = 144 = 767
3 P2 = -44 < X3 = 3 Y3 = 6 (3, 6) P13 = P2 + 2 * ry2 x2 + 3 * ry2 (2*36*3) (2*64*6)
0 = -44 + (2*36*2) – (3*36) = 208 = 216 = 768
4 P3 = 208 > X4 = 4 Y4 = 5 (4, 5) P14 = P3 + 2* ry2 * x3 – 2 * rx2 Y3 + 3 * ry2 + 2 *rx2 (2*36*4) (2*64*5)
0 = 288 = 640
= 208 +(2*36*3) – (2*64*6) + (3*36) +
(2*64)
= 208 + 288 +36 – 640 = -108
5 P4 = -108 < X5 = 5 Y5 = 5 (5, 5) P15 = P4 + 2 * ry2 x4 + 3 * ry2 (2*36*5) (2*64*5)
0 = -108 + (2*36*4) + ( 3*36) = 288 = 360 640
6 P5 = 288 > X6 = 6 Y6 = 4 (5, 4) P16 = P5 + 2* ry2 * x5 – 2 * rx2 Y5 + 3 * ry2 + 2 *rx2 (2*36*6) (2*64*4)
0 = 244 = 432 = 512
7 P6 = 244 > X7= 7 Y7 = (7,3) ( End of Region 1 since 2*ry2*x > 2*rx2*y) (2*36*7) (2*64*3)
0 3 = 504 = 384
8 (Region 2) P20 = ry2 (xk + ½)22 *+ rx2 (yk-1)22 – rx2 ry2 2
2if (p>=0) : P = P + 2* ry x – 2 * 2 r x Y + 3 * ry + 2
if (p < 0 ) : P = P + 2 * ry x + 3 * ry
2
= 36 ( 7 + ½) 2
+ (64) ( 2) – (36*64) =
x = x+ 1 *r x
2
2025+256-2304= -23
X = X+1 , Y = y-1
Fill Area Premitives

 There are three algorithms to fill any 2D shape


1) Boundary Fill
2) Flood Fill
3) Scan line fill
Boundary Fill Algorithm

 This algorithm uses the recursive method.


 It start filling the region from any pixel that is inside the shape boundary
(known as seed pixel) and paint the inside towards the boundary.
 The algorithm checks whether the adjacent pixels of this seed pixel are colored
or not. If the adjacent pixel is already colored then leave it, otherwise fill it.
 The filling is done using four connected or eight connected approaches.
 The fill algorithm continues painting outward direction pixel by pixel until the
boundary colour is encountered.
 The functioning of the boundary-fill starts by accepting
1) The coordinates of an Seed Pixel (x, y),
2) Boundary colour and
3) Fill colour.
 Beginning from the seed pixel (x, y) the method checks its neighboring pixels to
find whether or not they are a part of the boundary colour.
 If they’re not from the boundary colour, then algorithm checks whether it is
already painted with the fill colour. If the neighbouring pixel is not of boundary
color and also not painted with fill color than algorithm paint that pixel with fill
color and their adjacent pixels are tested against the condition.
 The process ends when all the pixels up until the boundary colour for the world
are checked.
4- connected and 8-connected
neighbors

(x,y+1 (X-1,y+1) (x,y+1 (x+1,y+


) ) 1)

(X-1,y) (x,y) (x+1,y (X-1,y) (x,y) (x+1,y


) )

(x,y- (X-1,y-1) (x,y- (X+1,y-1)


1) 1)

Four connected approaches: In this approach, left, right, above, below pixels are tested.
Eight connected approaches: In this approach, left, right, above, below and four diagonals are selected.
Boundary Fill algorithm (pseudo
code)
Inputs: The seed Pixel (x,y), the fill color fc and Boundary color bc.
Procedure boundary_fill (x ,y, fc , bc)
if ( getpixel(x,y) <> fc and getpixel (x,y) <> bc) then
setpixel( x, y, fc)
boundary_fill( x , y +1, fc,bc)
boundary_fill( x , y-1, fc,bc)
boundary_fill( x-1, y, fc,bc)
boundary_fill( x+1, y, fc,bc)
boundary_fill( x+1, y - 1, fc,bc)
boundary_fill( x+1, y+1, fc,bc)
boundary_fill( x-1 , y -1, fc,bc)
boundary_fill( x-1, y + 1, fc,bc)
End procedure
Advantages/ Disadvantages of
Boundary fill algorithm
Advantages:
 1) Faster than flood fill algorithm
 2) Memory consumption is relatively low in Boundary-fill algorithm.

Disadvantages or Limitations
 It works only if there is only one boundary color. If the polygon is
having boundaries with different colors then boundary fill algorithm fails.
 Relatively complex. It checks for two conditions , whether the pixel is not fill_color
and not Boundary_color.
Flood Fill Algorithm

 In this method, a point or seed pixel which is inside region is selected.


This point is called a seed point.
 Then four connected approaches or eight connected approaches is used
to fill with specified color.
 The flood fill algorithm is similar to boundary fill algorithm but this
method is more suitable for filling multiple colors boundary.
 When boundary is of many colors and interior is to be filled with one
color we use this algorithm.
 This is also a recursive algorithm.
 The algorithm takes three inputs
1) Seed Pixel (x,y)
2) Old color
3) New Color
 The algorithm checks, if pixel inside the polygon is of old color then
paint it with new color (or fill color) . And recursively it applies the same
test for its neighbouring pixels.
Flood fill algorithm (Pseudo code)

Input: seed pixel (x ,y ) , previous color as ‘old_color’ , New color as ‘fill_Color’


Procedure floodfill_4 (x, y, old_color, fill_color)
If (getpixel (x, y) = old_color)
setpixel (x, y, fill_color);
floodfill_4 (x+1, y, fill_color, old_color);
floodfill_4 (x-1, y, fill_color, old_color);
floodfill_4 (x, y+1, fill_color, old_color);
floodfill_4 (x, y-1, fill_color, old_color);
End Procedure
Advantages/Disadvantages of Flood
fill algorithm

Advantages:
1) Simple and efficient

Disadvantage:
 All pixels in the region must be made the same colour when the region
is being created.
Flood Fill Vs. Boundary fill Algorithm
Flood Fill Boundary fill

Goes on filling the inside of polygon till the Goes on filling the inside of polygon till the
background color is filled with new color. boundary color reaches.
It can process the image containing more It can only process the image containing
than one boundary colours. single boundary colour.

Flood-fill algorithm is comparatively Boundary-fill algorithm is faster than the


slower than the Boundary-fill algorithm. Flood-fill algorithm.

Algorithm stops when all old color Algorithm stops when entire region
(background color) pixels are filled with covered with boundary is filled with new
new colr. color.

Memory consumption is relatively low in


It requires huge amount of memory.
Boundary-fill algorithm.
Flood-fill algorithms are simple and The complexity of Bounndary-fill algorithm
efficient. is high.
Problems with Recursive algorithms

 Recursive algorithms for seed fill methods have got two difficulties:-
1. The first difficulty is that if some inside pixels are already displayed in
fill colour then recursive branch terminates leaving further internal
pixels unfilled.
2. Another difficulty with recursive seed fill methods is that it cannot be
used for large polygons.
Scan Line Polygon Fill Algorithm

 The figure (a) illustrates the scan line algorithm for filling of a
polygon.
 For each scan line crossing a polygon, this algorithm locates
the intersection points of the scan line with the polygon
edges.
 These intersection points are then sorted from left to right,
and the corresponding positions between each intersection
pair are set to the specified fill colour.
 In figure (a), we can see that there are two stretches of
interior pixels from x =6 to x = 9 and x = 12 to x =15.
 The scan line algorithm first finds the largest Y value (Y max)
and smallest y value (Ymin) of the polygon.
 It then starts with the largest y value and works its way
down, scanning from left to right, in the manner of a raster
 The important task in the scan line algorithm is
to find the intersection points of the scan line
with the polygon boundary.
 When intersection points are even, they are
sorted from left to right, paired and pixels
between paired points are set to the fill colour.
 But in some cases intersection point is a
vertex.
 When scan line intersects polygon vertex a
special handling is required to find the exact
intersection points.
 To handle such cases, we must look at the
other endpoints of the two line segments of
the polygon which meet at this vertex.
 If these points lie on the same (up or down)
side of the scan line, then the point in question
counts as an even number of intersections.
 If they lie on opposite sides of the scan line,
then the point is counted as single
intersection. This is illustrated in figure (b).
 As shown in the figure (b) For scan line 1, the other end
points (B and D) of the two line segments of the
polygon lie on the same side of the scan line, hence
there are two intersections resulting two pairs: 1-2 and
3-4.
 For scan line 2, the other endpoints (D and F) of the
two line segments of the polygon lie on the opposite
sides of the scan line, hence there is a single
intersection resulting two pairs: 1 -2 and 3 - 4.
 We have seen that it is necessary to calculate x intersection
points for scan line with every polygon side.
 We can simplify these calculations by using edge Coherence
Properties. i.e. incremental calculations between successive (xk,yk)
scanlines. Y
Y-1
 We can determine the x intersection value on the next scan line
if the x intersection value for previous scan line is known.
 We know that,
Slope m = ΔY / ΔX = (Yk+1 – Yk) / (Xk+1– Xk)
 = 1 / (Xk+1 – Xk)
Xk+1 = Xk + 1/m
 Where Xk is the intersection of previous scanline
 Many times it is not necessary to compute the x
intersections for scan line with every polygon
side.
 We need to consider only those polygon sides
which intersects with the current scan line.
Such polygon edges are called active edges.
 Actually algorithm stores 5 attributes for each
of the polygon edges : x1, y1, x2, y2 and 1/m.
 It will be easier to identify which polygon sides
should be tested for x-intersection , if we first
sort the polygon sides in order of their
maximum y value.
 Once the sides are sorted we can process the
scan lines from the top of the polygon to its
bottom producing an active edge list for each
scan line crossing the polygon boundaries.
 The active edge list for a scan line contains all
edges crossed by that scan line.
 The figure (d) shows sorted edges of the
polygon with active edges.
Monotonically increasing (or Monotonically increasing and then
monotonically decreasing) y value , decreasing y value, Consider vertex
Consider vertex as an odd point as an even point
 Thus , a scan line algorithm for filling a polygon begins by ordering the
polygon sides on the largest y value.
 It begins with the largest y value and scans down the polygon.
 For each y, it determines active edges and finds the x – intersections
with these edges.
 It then sorts, pairs and passes these x values to a line drawing routine.
Algorithm

 Input number of vertices, n


 Read x and Y coordinates of all vertices.
 Find Ymin and Ymax.
 Set Y = Ymax
 For each scanline Y do
 Obtain intersection points of scan line with polygon edges.
 Sort intersection points form left to right
 Form pairs of intersection form the list . Intersection at vertices requires special
handeling.
 Fill within pairs.
 Intersection points are updated for each scan line.
 Stop when scanline has reached Ymin
Inside Out side Test

 Some of the algorithms requires that we give initial seed pixel that is inside the
polygon.
 Is there any way by which we can find out whether given pixel is inside polygon or
not??
 There are two methods by which we can identify whether particular point is inside
an object or outside.
1) Even-Odd method
2) Winding number method
Even – Odd method

 In this technique, we count the edge crossing along the line


from any point (x,y) to the point that is surly outside the
polygon.
 If the number of intersections is odd, then the point (x,y) is
an interior point.
 If the number of intersections is even, then the point (x,y) is
an exterior point.
 The following example depicts this concept.
(a,b)
 From the fiven figure, we can see that from the point (x,y),
the number of intersection points on the left side is 5 and on
the right side is 3.
 From both ends, the number of intersection points is odd, so
the point is considered within the object.
 Similarly for point (a,b), number of intersection points
towards right are 2, hence it is exterior point.
Special Cases

 What if the intersection point is a vertex ?


 Consider point A under consideration.
B C A  Here one of the intersections is a vertex marked as ‘C’.
 We must look at the other two end points of the line
segments that meet at this vertex C.
Outside
Count even  If these points lie on the same side of the line segment AB
point
then consider it as even.
 So there are total 3 intersections , so point A is considered
inside the polygon.
Count as odd  We want to find out whether A is inside or outside.
 In this case, the intersection point is a vertex marked
as C.
B c A  Now the two other end points of the line segments
Outside connecting vertex ‘C’ lie on opposite sides of the
point
vertex, so point c is considered as odd.
 So total intersections from A to B are odd (i.e. 1), so
point A is inside the polygon.
Non-zero Winding Number Method

 This method is also used with the simple polygons to test the given
point is interior or not.
 It can be simply understood with the help of a pin and a rubber band.
 Fix up the pin on one of the edge of the polygon and tie-up the rubber
band in it and then stretch the rubber band along the edges of the
polygon.
 When all the edges of the polygon are covered by the rubber band,
check out the pin which has been fixed up at the point to be test.
 If we find at least one wind at the point consider it within the polygon,
else we can say that the point is not inside the polygon.
 Give directions to all the edges of the polygon.
 Assign value +1 to all the edges which are going to upward
direction and -1 for all down word direction.
 Check the edge direction values from which the scan line is
passing and sum up them.
 If the total sum of this direction value is non-zero, then this
point to be tested is an interior point, otherwise it is an
exterior point.
 In the above figure, we sum up the direction values from
which the scan line is passing then the total is 1 – 1 + 1 = 1;
which is non-zero. So the point is said to be an interior
point.
Special cases

E  In case if the intersection point is a vertex, then we need to


1 check the other two endpoints of the line segments meeting
-1 at this vertex.
 If they lie on the opposite sides of the vertex
B C A
then weight of only one edge is considered as a count for
Outside
point
that vertex.
-1
+1  Here, point in question is ‘A’.
 ‘B’ is any point surly outside the polygon.
F  There is only one intersection (i.e. C) on line segment AB.
 C is a vertex. So needs special handling.
 Since other two endpoints ‘E’ and ‘F’ lie on the opposite side
of Line AB, Count of C is -1.
 The sum of count is -1 i.e. non-zero hence point is inside.
Special Case

 Point in question is ‘A’.


 In this case there are two intersection points D and C on
1 line segment AB.
 Here C is a vertex. So needs special handling.
B D C A -1  Since both the end points of the line segments meeting
Outside at vertex C are on the same side, we consider weight of
point
- both the edges. therefore count for c will be (-1+1 = 0).
1  Sum = count of D + count of C
1
 = 1 + 0 = 1 (non-zero)
 So point A is inside the polygon
1
Aliasing, Antialiasing
techniques :

 In computer graphics, the process by which smooth curves


and other lines become jagged because the resolution of the
graphics device or file is not high enough to represent a
smooth curve.
 In the line drawing algorithms, we have seen that all
rasterized locations do not match with the true line and we
have to select the optimum raster locations to represent a
straight line.
 This problem is severe in low resolution screens. In such
screens line appears like a stair-step, as shown in the figure
below. This effect is known as aliasing. It is dominant for
lines having gentle and sharp slopes.
Antialiasing Techniques

 Antialiasing methods were developed to combat the effects of


aliasing.
 There are basically four such methods.
 1) Area Sampling or Pre filtering
2) super sampling or post filtering,
3) pixel phasing
Area Sampling/ Pre filtering

 Pre filtering method treats a pixel as an area.


 It computes color of the pixel based on the
overlap of the object with the pixel area.
 This means it computes how much part of the
fraction of the pixel is covered by the object and
depending on that , it set the intensity of the
pixel.
 This method requires a large number of
calculations and approximations.
Super sampling/ Post Filtering

 Super sampling tries to reduce the aliasing effect by


sampling or taking more than one samples per pixel.

1/9 1/9 1/9
Each pixel is divided into 9 sub pixels (also called sample
points). 1/9 1/9 1/9
 In the figure , some of these sub pixels may get color of 1/9 1/9 1/9
object (if object passes through that subpixel) or
background color (if object does not pass through it).
 Suppose 3 sub pixel gets background color and rest 6
gets object color then the color of that pixel will be the
sum of (1/3) of background color and (2/3) f object color.
 So here final pixel intensity becomes the average of
several samples.
Weighted super sampling

 Here we can assign weight to the subpixels.


 For example, we may give a more weightage to center
point say 50%. And remaining 50% is distributed 1/8 1/8 1/8
among 8 sub pixels. 1/8 1/2 1/8
1/8 1/8 1/8
Pixel Phasing

 Pixel phasing is hardware based anti-aliasing technique.


 The graphics system in this case is capable of shifting individual pixels
from their normal positions in the pixel grid by a fraction (Typically ¼ 02
½) of unit distance between pixels.
 By moving pixels closure to the true line, this technique reduces the
aliasing effects.
END OF
CHAPTER 2

You might also like