polygons - e-studythirdyearengineering.weebly.com/uploads/.../polygon... · scan line polygon...
TRANSCRIPT
![Page 1: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/1.jpg)
Polygons UNIT - III
![Page 2: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/2.jpg)
Agenda
Polygon Terminology
Types of polygons
Inside Test
Polygon Filling Algorithms
Scan-Line Polygon Fill Algorithm
Flood-Fill Algorithm
![Page 3: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/3.jpg)
A Polygon
Vertex = point in space (2D or 3D)
Polygon = ordered list of vertices
Each vertex connected with the next in the list
Last is connected with the first
May contain self-intersections
Simple polygon – no self-intersections
These are of most interest in CG
![Page 4: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/4.jpg)
Polygons in graphics
The main geometric object used for interactive
graphics.
Different types of Polygons
Simple Convex
Simple Concave
Non-simple : self-intersecting
Convex Concave Self-intersecting
![Page 5: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/5.jpg)
5
Polygon Representation and Entering
![Page 6: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/6.jpg)
Polygon Representation
Some graphics packages store polygon as a whole unit.
Some graphics packages provide trapezoid primitive:
means polygons are drawn in the form of trapezoids. And
polygon is considered as a collection of trapezoids.
Sometimes we need to get information of vertices and
polygon is drawn by using series of lines. Here polygon
information is stored in the Display File. The information
is in the form of commands.
Opcode 1 move
2 line
above 3 for no. of vertices of polygon
![Page 7: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/7.jpg)
Inside Test
Why ?
Want to fill in (color) only pixels inside a polygon
What is “inside” of a polygon ?
Methods
1. Even –odd method
2. Winding no. method
![Page 8: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/8.jpg)
Even –odd method
Case :1
Count number of intersections with polygon edges
If N is odd, point is inside
If N is even, point is outside
Case:2
P
P
Q
Q
P Q
P Q
Odd
even
![Page 9: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/9.jpg)
Winding no. Method
Every side has given a no. called winding no.
Total of this winding no. is called net winding.
If net winding is zero then point is outside otherwise it
is inside.
+1 -1
![Page 10: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/10.jpg)
Polygon Filling
10
Seed Fill Approaches
2 algorithms: Boundary Fill and Flood Fill
works at the pixel level
suitable for interactive painting apllications
Scanline Fill Approaches
works at the polygon level
better performance
![Page 11: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/11.jpg)
11
Seed Fill Algorithms: Connectedness
4-connected region: From a given pixel, the region that you can get to by a series of 4 way moves (N, S, E and W)
8-connected region: From a given pixel, the region that you can get to by a series of 8 way moves (N, S, E, W, NE, NW, SE, and SW)
4-connected 8-connected
![Page 12: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/12.jpg)
12
Boundary Fill Algorithm
Boundary-defined region
Start at a point inside a region
Paint the interior outward to the edge
The edge must be specified in a single color
Fill the 4-connected or 8-connected region
4-connected fill is faster, but can have problems:
![Page 13: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/13.jpg)
13
Boundary Fill Algorithm (cont.)
void BoundaryFill(int x, int y, newcolor, edgecolor) { int current; current = ReadPixel(x, y); if(current != edgecolor && current != newcolor) { putpixel(x,y, newcolor); BoundaryFill(x+1, y, newcolor, edgecolor); BoundaryFill(x-1, y, newcolor, edgecolor); BoundaryFill(x, y+1, newcolor, edgecolor); BoundaryFill(x, y-1, newcolor, edgecolor); } }
![Page 14: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/14.jpg)
14
Flood Fill Algorithm
Interior-defined region
Used when an area defined with multiple color
boundaries
Start at a point inside a region
Replace a specified interior color (old color) with fill
color
Fill the 4-connected or 8-connected region until all
interior points being replaced
![Page 15: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/15.jpg)
15
Flood Fill Algorithm (cont.)
void FloodFill(int x, int y, newcolor, oldColor) { if(ReadPixel(x, y) == oldColor) { putpixel(x,y, newcolor); FloodFill(x+1, y, newcolor, oldColor); FloodFill(x-1, y, newcolor, oldColor); FloodFill(x, y+1, newcolor, oldColor); FloodFill(x, y-1, newcolor, oldColor); } }
![Page 16: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/16.jpg)
Problems with Fill Algorithm
Recursive seed-fill algorithm may not fill regions
correctly if some interior pixels are already displayed in the fill color.
This occurs because the algorithm checks next pixels
both for boundary color and for fill color.
Encountering a pixel with the fill color can cause a recursive branch to terminate, leaving other interior pixels unfilled.
This procedure requires considerable stacking of
neighboring points, more efficient methods are generally employed.
![Page 17: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/17.jpg)
Scan line polygon Filling
Interior Pixel Convention
Pixels that lie in the interior of a polygon belong to
that polygon, and can be filled.
Pixels that have centers that fall outside the polygon,
are said to be exterior and should not be drawn.
Exploit coherence: pixels that are nearby each other tend to share common attributes (color, illumination, normal vectors, texture, etc.).
Span coherence: Pixels in the same scan line tend to be similar.
Scan-line coherence: Pixels in adjacent scan line tend to be similar.
![Page 18: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/18.jpg)
Example
The boundary of a
polygon: (In
practice, a polygon
is defined as a list of
vertices.)
![Page 19: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/19.jpg)
Basic Scan-Fill Algorithm
For each scan line:
1.Find the intersections of the scan line with all edges of the polygon.
2. Sort the intersections by increasing
x-coordinate.
3. Fill in all pixels between pairs of
intersections.
![Page 20: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/20.jpg)
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
![Page 21: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/21.jpg)
What happens at edge end-point?
Edge endpoint is duplicated. In other words, when a scan line intersects an edge endpoint, it
intersects two edges. Two cases:
Case A: edges are on opposite side of the scan line Case B: edges are on the same side of current scan line
In Case A, we should consider this as only ONE edge intersection In Case B, we should consider this as TWO edge intersections
Scan-line Scan-line
Case A Case B
![Page 22: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/22.jpg)
22
Spacial Handling (cont.)
Intersection points: (p0, p1, p2) ???
->(p0,p1,p1,p2) so we can still fill pairwise ->In fact, if we compute the intersection of the scanline with edge e1 and e2 separately, we will get the intersection point p1 twice. Keep both of the p1.
Case 1
![Page 23: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/23.jpg)
Spacial Handling (cont.)
However, in this case we count p1 once (p0,p1,p2,p3),
Case 2
![Page 24: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/24.jpg)
To increase the efficiency of the algorithm special data structures are maintained in Scan line Algorithm.
Active Edge list and Active Edge Table.
Each non-horizontal edge occupies one row/record .
The rows are sorted according to ymin.
![Page 25: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/25.jpg)
Active Edges
Polygon edges are sorted according to their minimum Y.
When the current scan line y value matches the ymin of an edge that edge becomes active.
When the current scan line moves above the upper endpoint, the edge becomes inactive.
Only Active edges are involved in intersection computation.
![Page 26: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/26.jpg)
Active Edge Table
Active edges are sorted according to increasing X.
![Page 27: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/27.jpg)
v1
v2
v3
v4
v5 v6
v7 E1
E2
E3
E4
E5
E6
E7
Edge Ymin Ymax X-coor of vertex with y=ymin
E1 Y1 Y2-1 X1
E7 Y1 Y7 X1
E4 Y5 Y4-1 X5
E6 Y6 Y7 X6
E2 Y2 Y3 X2
E3 Y4 Y3 X4
An Edge List
![Page 28: Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon Filling Interior Pixel Convention Pixels that lie in the interior of a polygon belong](https://reader035.vdocuments.us/reader035/viewer/2022062415/5fbc8ec744f9321b09637511/html5/thumbnails/28.jpg)