0% found this document useful (0 votes)
28 views9 pages

Computer Graphics

This 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 using four clipping cases. The algorithm traverses the polygon vertices and adds them to the output one by one based on their inside/outside status with respect to the clipping edge. The Sutherland-Hodgman algorithm can clip both convex and concave polygons, but has issues with concave polygons. The document also mentions the Weiler-Atherton algorithm, which can handle concave polygons with holes by producing multiple output polygons.
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)
28 views9 pages

Computer Graphics

This 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 using four clipping cases. The algorithm traverses the polygon vertices and adds them to the output one by one based on their inside/outside status with respect to the clipping edge. The Sutherland-Hodgman algorithm can clip both convex and concave polygons, but has issues with concave polygons. The document also mentions the Weiler-Atherton algorithm, which can handle concave polygons with holes by producing multiple output polygons.
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/ 9

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
• Given 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 • Clip polygon P’ to clipping edge
• Check vi,vi+1 wrt the clip edge
–foreach edge in polygon P’
• Need vi,vi+1‘s inside/outside status
» Check clipping cases (there are 4)
• Add vertex one at a time. There are 4 cases:
» Case 1 : Output vi+1 to P’’
» Case 2 : Output intersection point to P’’
» Case 3 : No output
» Case 4 : Output intersection point & vi+1 to P’’
• P’ = P’’
7
1994 Foley/VanDam/Finer/Huges/Phillips ICG
} 8 9
Animated by Max Peysakhov @ Drexel University

7 8 9

Final Result Issues with Sutherland- Weiler-Atherton Algorithm


Hodgman Algorithm • General clipping algorithm for concave
polygons with holes
• Clipping a concave polygon • Produces multiple polygons (with holes)
• Can produce two CONNECTED areas • Make linked list data structure
Note: Edges
XY and ZW! • Traverse to make new polygon(s)

11 12 13
1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG

11 12 13

2
Intersection Special Cases
Weiler-Atherton Algorithm
Linked
• Given polygons A and B as linked list of List Data • If “intersecting” edges are parallel, ignore
vertices (counter-clockwise order) • Intersection point is a vertex
Structure
• Find all edge intersections & place in list – Vertex of A lies on a vertex or edge of B
• Insert as “intersection” nodes – Edge of A runs through a vertex of B
Intersection – Replace vertex with an intersection node
• Nodes point to A & B
Nodes
• Determine in/out
status of vertices
14 15 16

14 15 16

Weiler-Atherton Algorithm: Example: Union Example


Union
B
• Find a vertex of A outside of B
• Traverse linked list
• At each intersection point switch to
other polygon
• Do until return to
starting vertex
• All visited vertices and
{V1, V2, V3, P0, V8, V4, P3, V0}, {V6, P1, P2} A
nodes define union’ed polygon
17 18 19

17 18 19

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

20 21 22

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


Boolean Special Cases
• Connect P with another point P` that you know is
outside polygon
If polygons don’t intersect • Just re-use code from
• Intersect segment PP` with polygon edges
– Union Cohen-Sutherland
• Watch out for vertices! algorithm
• If one inside the other, return polygon that
surrounds the other
• If # intersections is even (or 0) ! Outside • If a vertex’s bit code
• Else, return both polygons
• If odd ! Inside equals zero, it’s inside
– Intersection • Else, it’s outside
• If one inside the other, return polygon inside
the other
• Else, return no polygons
23 24 25

23 24 25

4
Edge clipping Intersecting Two Edges (1) Intersecting Two Edges (2)
• Re-use line clipping from HW1 • Edge 0 : (P0,P1) • Solve for t’s
– Similar triangles method • Edge 2 : (P2,P3) • t0 = ((x0 - x2) * dy2+ (y2 - y0) * dx2) /
– Cyrus-Beck line clipping • E0 = P0 + t0*(P1-P0) D0 º (P1-P0) (dy0 * dx2- dx0 * dy2)
• t2 = ((x2 - x0) * dy0+ (y0 - y2) * dx0) /
• Yet another technique • E2 = P2 + t2*(P3-P2) D2 º (P3-P2)
(dy2 * dx0- dx2 * dy0)
• P0 + t0*D0 = P2 + t2*D2 • See http://www.vb-helper.com/howto_intersect_lines.html
• x0 +dx0 * t0 = x2 +dx2 * t2 for derivation
• y0 +dy0 * t0 = y2 +dy2 * t2 • Edges intersect if 0 £ t0,t2 £ 1
• Edges are parallel if denominator = 0
26 30 31

26 30 31

Examples Filling Primitives: Rectangles, Scan Filling Primitives:


Polygons & Circles Rectangles
0 £ t0 ,t2 £ 1
• Two part process • Easy algorithm
– Which pixels to fill? – Fill from xmin to xmax
– What values to fill them with? Fill from ymin to ymax
• Idea: Coherence • Issues
– Spatial: pixels are the – What if two adjacent
same from pixel-to-pixel rectangles share an edge?
t0 ,t2 £ 0 t2 £ 0 and scan-line to scan line; – Color the boundary pixels twice?
0 £ t0 £ 1 – Span: all pixels on a span get the same value – Rules:
– Scan-line: consecutive scan lines are the same • Color only interior pixels
• Color left and bottom edges
– Edge: pixels are the same along edges
32 33 34

32 33 34

5
Scan Filling Primitives:
Scan Filling Polygons Scan Filling Polygons
Polygons
• Observe: • Idea #1: use midpoint • Idea #2: draw pixels only strictly inside
– FA, DC intersections algo on each edge, fill – Find intersections of
are integer in between extrema scan line with edges
– FE, ED intersections points – Sort intersections by
increasing x coordinate
are not integer • Note: many extrema
– Fill pixels on inside
• For each scan line, pixels lie outside the based on a parity bit
how to figure out polygon • Bp initially even (off)
which pixels are • Invert at each intersect
• Why: midpoint algo • Draw when odd,
inside the polygon? has no sense of in/out do not draw when even

35 36 37
1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG

35 36 37

How to handle vertices? Scan-Filling a Polygon


Scan Filling Polygons
• Problem:
• Issues with Idea #2: – vertices are counted twice
– If at a fractional x value, how to pick which • Solution:
pixels are in interior? – If both neighboring vertices are
– Intersections at integer vertex coordinates? on the same side of the scan
line, don’t count it
– Shared vertices? – If both neighboring vertices are
– Vertices that define a horizontal edge? on different sides of a scan
line, count it once
– Compare current y value with y
value of neighboring vertices
38 39 40
Animated by Max Peysakhov @ Drexel University

38 39 40

6
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 y that the
• Apply open and closed status • C is ymax (open) for BC. edge crosses by examining its ymin and ymax
to vertices to other edges
– ymin vertex closed
Bp doesn’t change. • If edge is horizontal, ignore it
• Ignore CD. D is ymin (closed) • If ymax on scan-line, ignore it
– ymax vertex is open • 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) • Apply floor on first xsect and ceiling on second xsect
becomes even and drawing for GH. Bp becomes odd. • Check for double intersection special case
stops Draw to FE.
– Clear scan-lines’ edge list
41
1994 Foley/VanDam/Finer/Huges/Phillips ICG
• Ignore GF. No drawing 42
1994 Foley/VanDam/Finer/Huges/Phillips ICG
44

41 42 44

Example
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

45 46 47
1994 Foley/VanDam/Finer/Huges/Phillips ICG 1994 Foley/VanDam/Finer/Huges/Phillips ICG

45 46 47

7
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);
}
48 } 49 50
1994 Foley/VanDam/Finer/Huges/Phillips ICG

48 49 50

Plain PBM Image File


Plain PBM Image files Plain PBM Image Example
• There is exactly one image in a file
• File begins with ”magic number” “P1”
• Next line specifies pixel resolution
• Each pixel is represented by a byte
containing ASCII ‘1’ (black) or ‘0’ (white)
• All fields/values separated by
whitespace characters
• No line longer than 70 characters?
51 52 53

51 52 53

8
Homework #2
Course Status Programming assignment 3
So far everything straight lines! • Modify homework #1
• Input PostScript-like file.
• How to model 2D curved objects? • Add reading “moveto” and “lineto” • Output B/W PBM.
– Representation commands • Implement viewports.
• Circles
• Types of 2D Curves • They define closed polygons • Use HW2 for polygon clipping.
• Parametric Cubic Curves
• Transform polygon vertices • Implement scanline polygon filling. (You can
• Bézier Curves, (non)uniform, (non)rational not use flood filling algorithms)
• NURBS
• Clip polygons against window with
– Drawing of 2D Curves
• Line drawing algorithms for complex curves Sutherland-Hodgman algorithm
• DeCasteljeau, Subdivision, De Boor
• Display edges with HW1 line-drawing
55
code 56 57

55 56 57

You might also like