0% found this document useful (0 votes)
56 views40 pages

Computer Graphics (CSE 4103)

The document discusses polygon clipping and filling algorithms. It covers the Sutherland-Hodgman polygon clipping algorithm, which clips a polygon against the edges of a viewport one edge at a time. Each vertex is considered in turn, and the edge from the previous to current vertex is handled according to four cases that determine whether vertices and intersections should be added to the output clipped polygon. After processing all edges, the polygon is fully clipped to the viewport region.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views40 pages

Computer Graphics (CSE 4103)

The document discusses polygon clipping and filling algorithms. It covers the Sutherland-Hodgman polygon clipping algorithm, which clips a polygon against the edges of a viewport one edge at a time. Each vertex is considered in turn, and the edge from the previous to current vertex is handled according to four cases that determine whether vertices and intersections should be added to the output clipped polygon. After processing all edges, the polygon is fully clipped to the viewport region.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Department of Computer Science and Engineering,

Computer Graphics (CSE 4103)

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

Polygon Clipping and Polygon Filling


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
This Class
1. Polygon Clipping (Sutherland-Hodgman) Algorithm
2. Polygon Filling Algorithm
What is Clipping
From Computer Graphic’s context, Clipping “is a method to
selectively enable or disable rendering operations within a
defined region of interest ”[Wiki].

In general, Clipping is a technique to trimming down a given


primitive with respect to an object.
Clipping Polygons
• Clipping polygons is more complex than
clipping the individual lines
– Input: polygon
– Output: original polygon, new polygon, or
nothing
• When can we trivially accept/reject a
polygon as opposed to the line segments
that make up the polygon?
Why Is Clipping Hard?
• What happens to a triangle during clipping?
• Possible outcomes:

triangle  triangle triangle  quad triangle  5-gon

How many sides can a clipped triangle


have?
How many sides?
• Seven…
Why Is Clipping Hard?
• A really tough case:
Why Is Clipping Hard?
• A really tough case:

concave polygon  multiple polygons


Sutherland-Hodgman Polygon Clipping
Algorithm
• Basic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the viewport edge’s
equation
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Basic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the edge equation
– After doing all edges, the polygon is fully clipped
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Basic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the edge equation
– After doing all edges, the polygon is fully clipped
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Basic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the edge equation
– After doing all edges, the polygon is fully clipped
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Basic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the edge equation
– After doing all edges, the polygon is fully clipped
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Basic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the edge equation
– After doing all edges, the polygon is fully clipped
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Basic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the edge equation
– After doing all edges, the polygon is fully clipped
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Basic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the edge equation
– After doing all edges, the polygon is fully clipped
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Basic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the edge equation
– After doing all edges, the polygon is fully clipped
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Basic idea:
– Consider each edge of the viewport individually
– Clip the polygon against the edge equation
– After doing all edges, the polygon is fully clipped
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Input/output for algorithm:
– Input: list of polygon vertices in order
– Output: list of clipped poygon vertices
consisting of old vertices (maybe) and new
vertices (maybe)
• Note: this is exactly what we expect from
the clipping operation against each edge
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Sutherland-Hodgman basic routine:
– Go around polygon one vertex at a time
– Current vertex has position p
– Previous vertex had position s, and it has been
added to the output if appropriate
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Edge from s to p takes one of four cases:

inside outside inside outside inside outside inside outside


p s
p
s

p s
p s

p output i output no output i output


p output
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Four cases:
1. s inside plane and p inside plane inside outside
• Add p to output
• Note: s has already been added
s
2. s inside plane and p outside plane
• Find intersection point i
• Add i to output
3. s outside plane and p outside plane
• Add nothing
4. s outside plane and p inside plane p
• Find intersection point i
• Add i to output, followed by p
p output
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Four cases:
1. s inside plane and p inside plane inside outside
• Add p to output
• Note: s has already been added
2. s inside plane and p outside plane
• Find intersection point i
• Add i to output
3. s outside plane and p outside plane
• Add nothing p
4. s outside plane and p inside plane s
• Find intersection point i
• Add i to output, followed by p
i output
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Four cases:
1. s inside plane and p inside plane
inside outside
• Add p to output
p
• Note: s has already been added
2. s inside plane and p outside plane
• Find intersection point i
• Add i to output
3. s outside plane and p outside plane
• Add nothing
s
4. s outside plane and p inside plane
• Find intersection point i
• Add i to output, followed by p
no output
Sutherland-Hodgman Polygon Clipping
Algorithm..
• Four cases:
1. s inside plane and p inside plane
• Add p to output inside outside
• Note: s has already been added p s
2. s inside plane and p outside plane
• Find intersection point i
• Add i to output
3. s outside plane and p outside plane
• Add nothing
4. s outside plane and p inside plane
• Find intersection point i
• Add i to output, followed by p
i output
p output
Sutherland-Hodgman Polygon Clipping
Algorithm.. Another Example

• Four cases:
1. s inside plane and p inside plane
• Add p to output
Before Clipping • Note: s has already been
added
2. s inside plane and p outside plane
• Find intersection point i
• Add i to output
3. s outside plane and p outside plane
• Add nothing
4. s outside plane and p inside plane
• Find intersection point i
• Add i to output, followed
by p

After Clipping
Sutherland-Hodgman Polygon Clipping
Algorithm.. Another Example
s2 s3
E1

s4
s5 • Four cases:
s1 1. s inside plane and p inside plane
• Add p to output
s2 s3 • Note: s has already been
added
E1 2. s inside plane and p outside plane
i1 i2 • Find intersection point i
• Add i to output
3. s outside plane and p outside plane
s4 • Add nothing
4. s outside plane and p inside plane
s5 • Find intersection point i
s1 s • Add i to output, followed
2 s3 by p

i1 i2

s4
s5
s1
Sutherland-Hodgman Polygon Clipping
i
Algorithm.. Another Example
2 i3

i4
i5
i1
• Four cases:
1. s inside plane and p inside plane
i2 i3 • Add p to output
j1 • Note: s has already been
j2 added
2. s inside plane and p outside plane
i4
j3 • Find intersection point i
• Add i to output
i5 3. s outside plane and p outside plane
i1 i6 • Add nothing
4. s outside plane and p inside plane
• Find intersection point i
• Add i to output, followed
by p
j1 i3
j2
i4
j3
i5
i6
Sutherland-Hodgman Polygon Clipping
Algorithm.. Another Example

• Four cases:
1. s inside plane and p inside plane
• Add p to output
j1 • Note: s has already been
j2 added
2. s inside plane and p outside plane
j3 • Find intersection point i
i4 j4 • Add i to output
3. s outside plane and p outside plane
j6 j5 • Add nothing
4. s outside plane and p inside plane
• Find intersection point i
• Add i to output, followed
by p
Sutherland-Hodgman Polygon Clipping
Algorithm..

• Four cases:
1. s inside plane and p inside plane
• Add p to output
• Note: s has already been
added
2. s inside plane and p outside plane
• Find intersection point i
• Add i to output
3. s outside plane and p outside plane
• Add nothing
4. s outside plane and p inside plane
• Find intersection point i
• Add i to output, followed
by p
Sutherland-Hodgman Polygon Clipping
Algorithm..

• Four cases:
1. s inside plane and p inside plane
• Add p to output
• Note: s has already been
added
2. s inside plane and p outside plane
• Find intersection point i
• Add i to output
3. s outside plane and p outside plane
• Add nothing
4. s outside plane and p inside plane
• Find intersection point i
• Add i to output, followed
by p
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
Thank you

You might also like