0% found this document useful (0 votes)
159 views8 pages

Polygon Clipping and Filling Techniques

The document discusses polygon clipping and filling. It introduces the Sutherland-Hodgman polygon clipping algorithm, which uses a divide and conquer approach to clip a polygon against one clipping edge at a time, repeating for each edge of the clipping rectangle. The algorithm works by traversing the vertices of the input polygon and adding them to the output polygon one by one based on their inside/outside status relative to the current clipping edge.

Uploaded by

rajdecorators
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)
159 views8 pages

Polygon Clipping and Filling Techniques

The document discusses polygon clipping and filling. It introduces the Sutherland-Hodgman polygon clipping algorithm, which uses a divide and conquer approach to clip a polygon against one clipping edge at a time, repeating for each edge of the clipping rectangle. The algorithm works by traversing the vertices of the input polygon and adding them to the output polygon one by one based on their inside/outside status relative to the current clipping edge.

Uploaded by

rajdecorators
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

CS 430 Outline Polygon

Computer Graphics
• Polygon clipping
– Sutherland-Hodgman, • Ordered set of vertices (points)
Polygon Clipping and Filling – Weiler-Atherton – Usually counter-clockwise
Week 3, Lecture 5
• Polygon filling • Two consecutive vertices define an edge
David Breen, William Regli and Maxim Peysakhov – Scan filling polygons
– Flood filling polygons • Left side of edge is inside
Department of Computer Science
Drexel University • Introduction and discussion of homework #2 • Right side is outside
• Last vertex implicitly connected to first
• In 3D vertices should be co-planar
1 2 3

1 2 3

The Sutherland-Hodgman Sutherland-Hodgman Algorithm


Polygon Clipping
Polygon-Clipping Algorithm • Input:
– v1 , v2 , … vn the vertices defining the polygon
• Lots of different cases • Divide and Conquer
– Single infinite clip edge w/ inside/outside info
• Issues • Idea:
• Output:
– Edges of polygon need – Clip single polygon using
to be tested against single infinite clip edge – v’1 , v’2 , … v’m , vertices of the clipped polygon
clipping rectangle – Repeat 4 times • Do this 4 (or ne) times
– May need to add new edges • Note the generality: • Traverse vertices (edges)
– Edges discarded or divided – 2D convex n-gons can clip • Add vertices one-at-a-time to output polygon
– Multiple polygons can result arbitrary n-gons
from a single polygon – Use inside/outside info
– 3D convex polyhedra can – Edge intersections
clip arbitrary polyhedra
4 5 6
1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG

4 5 6

1
Sutherland-Hodgman Algorithm Sutherland-Hodgman Algorithm Sutherland-Hodgman Algorithm
• Can be done incrementally • foreach polygon P P’ = P
• If first point inside add. If outside, don’t add
– foreach clipping edge (there are 4) {
• Move around polygon from v1 to vn and back to v1
• Check vi,vi+1 wrt the clip edge • Clip polygon P’ to clipping edge
• Need vi,vi+1 ‘s inside/outside status –foreach edge in polygon P’
• Add vertex one at a time. There are 4 cases: » Check clipping cases (there are 4)
» Case 1 : Output vi+1
» Case 2 : Output intersection point
» Case 3 : No output
» Case 4 : Output intersection point
& vi+1}
7 8 9
1994 Foley/VanDam/Finer/Huges/Phillips ICG
Anim ated by M ax Peysakhov @ Drexel University

7 8 9

Sutherland-Hodgman Algorithm Final Result Issues with Sutherland-


Hodgman Algorithm
• Clipping a concave polygon
• Can produce two CONNECTED areas
Note: Edges
XY and ZW!

10 11 12
1994 Foley/VanDam/Finer/Huges/Phillips ICG
Anim ated by M ax Peysakhov @ Drexel University

10 11 12

2
Weiler-Atherton Algorithm
• General clipping algorithm for concave Weiler-Atherton Algorithm
polygons with holes Linked
• Produces multiple polygons (with holes)
• Given polygons A and B as linked list of List Data
vertices (counter-clockwise order)
• Make linked list data structure Structure
• Find all edge intersections & place in list
• Traverse to make new polygon(s)
• Insert as “intersection” nodes
Intersection
• Nodes point to A & B
Nodes
• Determine in/out
status of vertices
13 14 15
1994 Foley/VanDam/Finer/Huges/Phillips ICG

13 14 15

Intersection Special Cases Weiler-Atherton Algorithm: Example: Union


Union
• If “intersecting” edges are parallel, ignore • Find a vertex of A outside of B
• Intersection point is a vertex • Traverse linked list
– Vertex of A lies on a vertex or edge of B • At each intersection point switch to
– Edge of A runs through a vertex of B other polygon
– Replace vertex with an intersection node • Do until return to
starting vertex
• All visited vertices and
nodes define union’ed polygon {V1, V2, V3, P0, V8, V4, P3, V0}, {V6, P1, P2}
16 17 18

16 17 18

3
Weiler-Atherton Algorithm:
Intersection Example: Intersection
Boolean Special Cases
• Start at intersection point
– If connected to an “inside” vertex, go there
If polygons don’t intersect
– Else step to an intersection point
– If neither, stop – Union
• Traverse linked list • If one inside the other, return polygon that
• At each intersection point surrounds the other
switch to other polygon and • Else, return both polygons
remove intersection point from – Intersection
list • If one inside the other, return polygon inside
• Do until return to starting intersection point the other
• If intersection list not empty, pick another one • Else, return no polygons
• All visited vertices and nodes define and’ed polygon {P1, V7, P0}, {P3, V5, P2}
19 20 21

19 20 21

Point P Inside a Polygon? Point P Inside a Rectangle?


Edge clipping
• Connect P with another point P` that you know is
outside polygon
• Intersect segment PP` with polygon edges • Just re-use code from • Re-use line clipping from HW1
Cohen-Sutherland – Similar triangles method
• Watch out for vertices! algorithm
• If # intersections is even (or 0) → Outside – Cyrus-Beck line clipping
• If a vertex’s code
• If odd → Inside equals zero, it’s inside • Yet another technique
• Else, it’s outside

22 23 24

22 23 24

4
Filling Primitives: Rectangles,
Intersecting Two Edges (1) Intersecting Two Edges (2)
Polygons & Circles
• Edge 0 : (P0,P1) • Solve for t’s • Two part process
• Edge 2 : (P2,P3) • t0 = ((x0 - x2) * dy2+ (y2 - y0) * dx2) / – Which pixels to fill?
• E0 = P0 + t0*(P1-P0) D0 º (P1-P0) (dy0 * dx2- dx0 * dy2) – What values to fill them with?
• t2 = ((x2 - x0) * dy0+ (y0 - y2) * dx0) / • Idea: Coherence
• E2 = P2 + t2*(P3-P2) D2 º (P3-P2)
(dy2 * dx0- dx2 * dy0) – Spatial: pixels are the
• P0 + t0*D0 = P2 + t2*D2 • See http://www.vb-helper.com/howto_intersect_lines.html same from pixel-to-pixel
• x0 +dx0 * t0 = x2 +dx2 * t2 for derivation and scan-line to scan line;
– Span: all pixels on a span get the same value
• y0 +dy0 * t0 = y2 +dy2 * t2 • Edges intersect if 0 £ t0,t2 £ 1
– Scan-line: consecutive scan lines are the same
• Edges are parallel if denominator = 0
– Edge: pixels are the same along edges
28 29 30

28 29 30

Scan Filling Primitives: Scan Filling Primitives:


Scan Filling Polygons
Rectangles Polygons
• Easy algorithm • Observe: • Idea #1: use midpoint
– Fill from xm in to xm ax – FA, DC intersections algo on each edge, fill
Fill from ym in to ym ax are integer in between extrema
• Issues – FE, ED intersections points
– What if two adjacent are not integer • Note: many extrema
rectangles share an edge? • For each scan line, pixels lie outside the
– Color the boundary pixels twice? how to figure out polygon
– Rules: which pixels are • Why: midpoint algo
• Color only interior pixels inside the polygon? has no sense of in/out
• Color left and bottom edges
31 32 33
1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG

31 32 33

5
How to handle vertices?
Scan Filling Polygons Scan Filling Polygons
• Problem:
• Idea #2: draw pixels only strictly inside • Issues with Idea #2: – vertices are counted twice
– Find intersections of
scan line with edges – If at a fractional x value, how to pick which • Solution:
– Sort intersections by pixels are in interior? – If both neighboring vertices are
increasing x coordinate – Intersections at integer vertex coordinates? on the same side of the scan
– Fill pixels on inside line, don’t count it
based on a parity bit – Shared vertices? – If both neighboring vertices are
• Bp initially even (off) – Vertices that define a horizontal edge? on different sides of a scan
• Invert at each intersect line, count it once
• Draw when odd,
do not draw when even – Compare current y value with y
value of neighboring vertices
34 35 36
1994 Foley/VanDam/Finer/Huges/Phillips ICG

34 35 36

How to handle horizontal edges? How to handle horizontal edges? Polygon Filling Algorithm
• Start drawing at IJ • For each polygon
• Idea: don’t count their vertices (Bp becomes odd). – For each edge, mark each scan-line that the edge
• Apply open and closed status • C is ymax (open) for BC. crosses by examining its ymin and ymax
to vertices to other edges
Bp doesn’t change. • If edge is horizontal, ignore it
– ymin vertex closed • If ymax on scan-line, ignore it
– ymax vertex is open • Ignore CD. D is ymin (closed)
• If ymin <= y < ymax add edge to scan-line y‘s edge list
for DE. Bp becomes even.
• On AB, A is at ymin for JA; AB – For each scan-line between polygon’s ymin and ymax
Stop drawing.
does not contribute, Bp is odd • Calculate intersections with edges on list
and draw AB • I is ymax (open) for IJ.
• Sort intersections in x
• Edge BC has ymin at B, but AB No drawing.
• Perform parity-bit scan-line filling
does not contribute, Bp • Ignore IH. H is ymin (closed) • Check for double intersection special case
becomes even and drawing for GH. Bp becomes odd.
– Clear scan-lines’ edge list
stops Draw to FE.
37 • Ignore GF. No drawing 38 40
1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG

37 38 40

6
Scan-Filling a Polygon
How to handle slivers? Scan Filling Curved Objects
• When the scan area does • Hard in general case
not have an “interior” • Easier for circles and
• Solution: use anti-aliasing ellipses.
• But, to do so will require • Use midpoint Alg to
softening the rules about generate boundary points.
drawing only interior • Fill in horizontal pixel spans
pixels • Use symmetry

41 42 43
1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG
Anim ated by M ax Peysakhov @ Drexel University

41 42 43

4 Connected Boundary-Fill Alg Boundary-Fill Algorithm


Boundary-Fill Algorithm Void BoundaryFill4( int x, int y, int fill,
int bnd)
• Issues with recursive boundary-fill algorithm:
• Start with some – May make mistakes if parts of the space already filled
{ with the Fill color
internal point (x,y) If Color(x,y) != fill and Color(x,y) != bnd
– Requires very big stack size
• Color it {
• Check neighbors for SetColor(x,y) = fill;
• More efficient algorithms
filled or border color BoundaryFill4(x+1, y, fill, bnd);
– First color contiguous span along one scan line
• Color neighbors if OK BoundaryFill4(x, y +1, fill, bnd);
– Only stack beginning positions of neighboring scan
BoundaryFill4(x-1, y, fill, bnd);
• Continue recursively lines
BoundaryFill4(x, y -1, fill, bnd);
}
44 } 45 46
1994 Foley/VanDam/Finer/Huges/Phillips ICG

44 45 46

7
Homework #2
Course Status
So far everything straight lines! • Modify homework #1
• How to model 2D curved objects? • Add “moveto” and “lineto” commands
– Representation • They define closed polygons
• Circles
• Types of 2D Curves • Transform polygon vertices
• Parametric Cubic Curves
• Bézier Curves, (non)uniform, (non)rational • Clip polygons against window with
• NURBS
Sutherland-Hodgman algorithm
– Drawing of 2D Curves
• Line drawing algorithms for complex curves • Display edges with HW1 line-drawing
• DeCasteljeau, Subdivision, De Boor code
48 49

48 49

You might also like