Computer Graphics - PPT - CH2
Computer Graphics - PPT - CH2
Chapter 2
OUTPUT
PRIMITIVES
Topics covered
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)
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
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
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
y
For the Lines with Negative slope:
|m| < 1
If |m| < 1 then
(x1,y1)
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 |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
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:
calculated as
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 this parameter has a problem, It has m = ΔY / ΔX (which involves floating point
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
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)
………………..(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
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
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)
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)
parameter P0
We know that,
Pk = (Xk + 1)2 + (Yk – ½)2 – r2
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
otherwise,
Next point along the circle is ( Xk + 1, Yk - 1)
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
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
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.
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:
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
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
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.
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.
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
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
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