Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

Department of Computer Science and Engineering,

Computer Graphics (CSE 4103)

University of Dhaka
Lecture 7
Basic Raster Graphics Algorithm for
Drawing 2D Primitives

Polygon Filling and Antialiasing


Last Classes
1. Incremental Line-drawing Algorithm
2. Mid-point Algorithm for Line-drawing
i) In only one Octant
ii) In all 8 octants
iii) 8-way Symmetry
iv) Tangent Independent mid-point line drawing algorithm
3. Mid-point Algorithm for drawing Circle
4. Mid-point Algorithm for drawing Ellipse
5. Line Clipping Algorithms
i) Cohen-Sutherland Line Clipping Algorithm
ii) Cyrus-Beck Line Clipping Algorithm
6. Polygon Clipping (Sutherland-Hodgman) Algorithm
7. Polygon Filling Algorithm
This Class
1. Details about Polygon Filling Algorithm (PolyFill Algorithm)
2. Antialyasing
Polygon Filling (Polyfill)
Polygon Filling (Polyfill)..

1. Find the minimum enclosed rectangle


2. Calculate number of scanlines (Ymax-Ymin +1)
3. For each scanline Do
a. Obtain intersection points of scanline with polygon edges
b. Sort intersections from left to right
c. For pairs of intersections from the list
d. Fill within pairs
e. Intersections points are updated for each scanline

3. c.d We can consider odd-parity concept to fill the line segment which is inside the
polygon (initially, parity bit is 0, when an intersecting point is occurred, parity is
changed to 1, and then filling is performed before getting a new intersecting point.
Once another intersecting point is occurred, parity is converted to even and no filling is
performed until a new intersecting point is obtained.
Polygon Filling (Polyfill)..
Basic Algorithm (without exception handling)
1. Find the minimum enclosed rectangle
2. Calculate number of scanlines (Ymax-Ymin +1)
3. For each scanline Do
a. Obtain intersection points of scanline with polygon edges
b. Sort intersections from left to right
c. For pairs of intersections from the list
d. Fill within pairs
e. Intersections points are updated for each scanline

3. c.d We can consider odd-parity concept to fill the line segment which is inside the
polygon (initially, parity bit is 0, when an intersecting point is occurred, parity is
changed to 1, and then filling is performed before getting a new intersecting point.
Once another intersecting point is occurred, parity is converted to even and no filling is
performed until a new intersecting point is obtained.
Polygon Filling (Polyfill)..

Normal Case
Problematic
Polygon Filling (Polyfill).. intersection
point
Special Case-1
Problem Solution
Number of Add an extra
intersecting points is intersection point
odd which is the same
where two polygon
edges are meeting
Problematic
intersection
point
Special Case-2
Problem Solution
Number of No need to add any
intersecting points is extra intersecting
even, but one point
intersecting point is
the vertex itself
Polygon Filling (Polyfill)..
Differentiating Special Case-1 and Special Case-2

If the intersecting point is a vertex and two sides of that vertex are in same
side, must add that point again in the list of intersecting points

If the intersecting point is a vertex and two sides of that vertex are in two
sides, don’t add that point again in the list of intersecting points
Span Extrema
• Only turn on pixels whose centers are interior to the polygon:
– Otherwise will intrude other adjacent polygons
• round up values on the left edge of a span, round down on
the right edge

midpoint algorithm interior


Scanline Coherence
Values do not change much from one scanline to the next --- the
coverage of a face on one scanline typically differs little from the
previous one

Edge Coherence
Edges intersected by scanline “i” are typically intersected by
scanline “i+1” (except you are entering or leaving)
Edge Coherence
• Computing the intersections between scan lines and
edges can be costly
• Use a method similar to the midpoint algorithm

• y = mx + b, x = (y – b) / m
• At y = s, xs = (s – b ) / m
At y = s + 1, xs+1 = (s+1 - b) / m = xs + 1 / m
• Incremental calculation: xs+1 = xs + 1 / m
Pseudo code of computing the left

span extrema (m > 1)


1 x − x min
= max
m y max − y min

int numerator = x max − x min


int denomenato r = y max − y min
int increment = 0
for ( y = y min ; y < y max ; y + + ){
Writ ePixel ( x,y );
increment + = numerator ;
if (increment > denomenat or ) {
/ * Overflow * /
x + +;
increme nt - = denomenat or;
}
}
Sorted Edge Table (SET)
Contains all information necessary to process the scanline efficiently
SET is built using a bucket sort with number of buckets = number of scanlines
All edges are sorted by their Ymin coordinates with a separate Y bucket for each
scanline
Within each bucket, edges are sorted by increasing X of Ymin point
Only non-horizontal edges are sorted
Edge Structure
Ymax Xmin dx/dy To next edge
Sorted Edge Table (SET)

11
10
9
8 (EF)
7 9 7 -5/2 11 7 6/4
6 (DE)
(CD)
5 11 13 0
4
(FA)
3
9 2 0
2
1 3 7 -5/2 5 7 6/4
0 (AB) (BC)
Active Edge List (AEL)
Contains all the edges crossed by a scanline at the current
stage of iteration
This is the list of edges that are active for this scanline, sorted
by increasing X intersection
Active Edge List (AEL)

FA EF
AET (8) 9 2 0 9 4 -5/2

DE CD
11 9 6/4 11 13 0
Active Edge List (AEL)

Set Y to smallest Y in SET entry


Initiallize AET to be empty
Repeat Until both AET and SET are empty
i) Move from SET bucket Y to AET, those edges whose Ymin=Y
ii)Sort AET on X
iii)Fill Pixels in scanline Y using pairs of X-coordinates from AET
iv)Increase scanline by 1
v) Remove from those AET those entries fro which Y = Ymax
vi) For each non-vertical edge in AET, update X for new Y
End
Scan-fill Algorithm

Construct the SET


Ymin= Min (All Y in SET)
AET = NULL
For Y = Ymin=to Ymax
Merge-sort SET(Y) into AET by X value
Fill between pairs of X in AET
for each edge in AET
if Edge.Ymax= Y
Remove Edge from AET
else
Edge.X = Edge.X + dx/dy
end if
End For
Sort AET by X value
End For
Scan-fill Algorithm

Practice:
http://www.cs.rit.edu/~icss571/filling/example.html
Antialiasing
Antialiasing..
Simple idea for lines: Intersect thin (1 pixel wide) rectangle with
grid of pixels (known as unweighted are sampling)

Somewhat better results than on-off


Looks slightly blurred close up
Does not take into account distance from center
Antialiasing..
A better technique, weighted area sampling, involves convolving
with a weighting function centered on each pixel (a cone shape)

Implement efficiently by pre computing overlap table


parameterized by distance and angle
Antialiasing..
Thank you

You might also like