0% found this document useful (0 votes)
715 views33 pages

Understanding Polygons in Graphics

The document discusses different types of polygons including convex and concave polygons. It defines key polygon concepts like sides, edges, and vertices. It also covers polygon filling algorithms like the odd-even test and non-zero winding number rule. Scan line polygon filling algorithms are described which use sorted edge intersections along scan lines to determine interior pixels.

Uploaded by

Arko Kundu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
715 views33 pages

Understanding Polygons in Graphics

The document discusses different types of polygons including convex and concave polygons. It defines key polygon concepts like sides, edges, and vertices. It also covers polygon filling algorithms like the odd-even test and non-zero winding number rule. Scan line polygon filling algorithms are described which use sorted edge intersections along scan lines to determine interior pixels.

Uploaded by

Arko Kundu
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Introduction to Polygons

• A polygon may be represented as a number of line segments connected end


to end to form a closed figure.
• The line segments which make up the polygon boundary are called sides or
edges.
• The endpoints of the sides are called the polygon vertices.
• Two types of polygons
– Convex
– Concave
• A convex polygon is a polygon such that for any two points inside the
polygon, all points on the line segment connecting them are also inside the
polygon.
• A concave polygon is one which is not convex.
Introduction to Polygons
• Different types of Polygons
• Simple Convex
• Simple Concave

Convex Concave
Polygons

convex concave
all interior angles are at least one angle is
<1800 >1800
Polygon Filling: Inside-Outside Test
Polygon Filling: Inside-Outside Test

Odd-Even Test
Draw a line from any point P to
a point outside the closed
polyline
If the number of line-segment
crossings is:
odd => P is an interior point
even => P is an exterior point
Nonzero Winding-Number Rule
• Give each boundary line crossed a direction number
and sum these direction numbers.
• The direction numbers indicates the direction the
polygon edge was drawn relative to the line segment,
constructed for test.
• For example, to test a point (xa , ya), let us consider a
horizontal line segment y=ya which runs from outside
the polygon to (xa , ya).
• There are two ways for a side to cross.
• The side could be drawn starting below the line, cross
1 it, and end above the line (first y value less than the
second y value). Give a direction number of -1 to the
side.
-1 1
• The edge could start above the line and finish it (first y
value greater than the second y value). Give a direction
Calculating winding number as the sum of of 1.
the direction numbers for the sides crossed • The sum of the direction numbers for the sides that
cross the constructed horizental line segment yields the
winding number for the point in question.
Flood Fill
• Want to fill in an area that is not defined within a single color boundary.
• Suppose we want to color the entire area whose original color is interiorColor, and
replace it with fillColor.
• Then, we start with a point in this area, and then color all surrounding points until
we see a pixel that is not interiorColor.
• If the area we want to paint has more than one interior color, we can first reassign
pixel values so that all interior points have the same color.
• Using either a 4-connected or 8-connected approach, we than step through pixel
positions until all interior points have been repainted.

void floodFill(int x, int y, int fillColor, int interiorColor) {


int color;
color=getPixel(x,y)
if (color==interiorColor) {
setPixel(x,y,fillColor);
floodFill(x+1,y,fillColor,interiorColor);
floodFill(x-1,y,fillColor,interiorColor);
floodFill(x,y+1,fillColor,interiorColor);
floodFill(x,y-1,fillColor,interiorColor);
}
}
Scan Line Polygon Fill
Algorithms
• A standard output primitive in general graphics package is a solid
color or patterned polygon area:
• There are two basic approaches to filling on raster systems.
• Determine overlap Intervals for scan lines that cross that area.
• Start from a given interior point and paint outward from this point
until we encounter the boundary
• The first approach is mostly used in general graphics packages,
however second approach is used in applications having complex
boundaries and interactive painting systems
Xk+1,yk+1 Scan Line yk +1

Scan Line yk

Xk , yk
Scan Line Polygon Fill Algorithm

10 14 18 24
Interior pixels along a scan line passing through a
polygon area
• For each scan line crossing a polygon are then sorted from left to right, and
the corresponding frame buffer positions between each intersection
pair are set to the specified color.
• These intersection points are then sorted from left to right , and the
corresponding frame buffer positions between each intersection pair are set to
specified color
Scan Line Polygon Fill Algorithm
• In the given example ( previous slide) , four pixel intersections define stretches
from x=10 to x=14 and x=18 to x=24
• Some scan-Line intersections at polygon vertices require special handling:
– A scan Line passing through a vertex intersects two polygon edges at that
position, adding two points to the list of intersections for the scan Line
– In the given example , scan Line y intersects five polygon edges and the
scan Line y’ intersects 4 edges although it also passes through a vertex
y' correctly identifies internal pixel spans ,but need some extra processing
• The topological difference between scan line y and scan line y’ is identified by
noting the position of the intersecting edges relative to the scan line.
• For Scan line y, the two intersecting edges sharing a vertex are on opposite
sides of the scan line. But for scan line y’, the two intersecting edges are both
above the scan line
• Thus, the vertices that require additional processing are those that have
connecting edges on opposite sides of scan line.

• We can identify these vertices by tracing around the polygon boundary
either in clock-wise or anti-clockwise order and observing the relative
changes in vertex y coordinates as we move from one edge to the next.
• If the endpoint y values of two consecutive edges monotonically increase
or decrease, we need to count the middle vertex as a single intersection
point for any scan line passing through that vertex.
• Otherwise, the shared vertex represents a local extremum (min. or max.)
on the polygon boundary, and the two edge intersections with the scan line
passing through that vertex can be added to the intersection list .
• One way to resolve this is also to shorten some polygon edges to split
those vertices that should be counted as one intersection.
• Nonhorizontal edges around the polygon boundary in the order specified,
either clockwise or anti-clockwise.
• When the end point y coordinates of the two edges are increasing , the y
value of the upper endpoint for the current edge is decreased by 1.
• When the endpoint y values are monotonically decreasing, we decrease the
y coordinate of the upper endpoint of the edge following the current edge.
Scan Line Fill:
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 monotonically increasing or decreasing
– Case B: edges reverse direction at endpoint
• 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
Scan Line Polygon Fill Algorithm

(a) (b)

Adjusting endpoint values for a polygon, as we process edges in order


around the polygon perimeter. The edge currently being processed is
indicated as a solid like. In (a), the y coordinate of the upper endpoint of
the current edge id decreased by 1. In (b), the y coordinate of the upper
end point of the next edge is decreased by 1
• Coherence properties are used to perform the calculations in scan-conversion and
other graphics algorithms.
• Coherence is a property that one part of a scene are related in some way to other
parts of the scene so that the relationship can be used to reduce processing.
• Coherence methods often involve incremental calculations applied along a single
scan lines.
• In determining edge intersections, we can set up incremental coordinate
calculations along any edge is constant from one scan line to the next.
• Slope of the polygon boundary line in terms of
xk+1, yk+1 Scan line yk+1 the scan-line intersection coordinates
m=(yk+1 – yk) / (xk+1 – xk)
xk , y k Scan line yk
• The change in y coordinates between the two
scan lines is
(yk+1 – yk) = 1

• The x-intersection value xk+1 on the upper scan line can be determined from the x-
intersection value xk on the preceding scan line as xk+1 = xk + (1/m)
• Each successive x intercept can thus be calculated by adding the inverse of the
slope and rounding to the nearest integer.
The scan conversion algorithm works as follows
i. Intersect each scanline with all edges
ii. Sort intersections in x
iii. Calculate parity of intersections to determine in/out
iv. Fill the “in” pixels
Special cases to be handled:
i. Horizontal edges should be excluded
ii. For vertices lying on scanlines,
i. count twice for a change in slope.
ii. Shorten edge by one scanline for no change in slope
• Coherence between scanlines tells us that
- Edges that intersect scanline y are likely to intersect y + 1
- X changes predictably from scanline y to y + 1
(Xk + 1, Yk + 1) Scan Line yk + 1

(Xk , Yk )
Scan Line yk
We have 2 data structures: Edge Table and Active Edge Table
• Traverse Edges to construct an Edge Table
1. Eliminate horizontal edges
2. Add edge to linked-list for the scan line corresponding to the lower
vertex.
Store the following:
- y_upper: last scanline to consider
- x_lower: starting x coordinate for edge
- 1/m: for incrementing x; compute
• Construct Active Edge Table during scan conversion. AEL is a linked list
of active edges on the current scanline, y. Each active edge line has the
following information
- y_upper: last scanline to consider
- x_lower: edge’s intersection with current y
- 1/m: x increment
The active edges are kept sorted by x
Algorithm
1. Set y to the smallest y coordinate that has an entry in the ET; i.e, y for the
first nonempty bucket.
2. Initialize the AET to be empty.
3. Repeat until the AET and ET are empty:
3.1 Move from ET bucket y to the AET those edges whose y_min = y
(entering edges).
3.2 Remove from the AET those entries for which y = y_max (edges not
involved in the next scanline), the sort the AET on x (made easier
because ET is presorted).
3.3 Fill in desired pixel values on scanline y by using pairs of x
coordinates from AET.
3.4 Increment y by 1 (to the coordinate of the next scanline).
3.5 For each nonvertical edge remaining in the AET, update x for the new
y.
Extensions:
1. Multiple overlapping polygons – priorities
2. Color, patterns Z for visibility
Boundary-Fill Algorithm

• Start at a point inside a region and paint the interior outward toward the boundary.
• If the boundary is specified in a single color, the fill algorithm proceeds outward
pixel by pixel until the boundary color is encountered.
• Accepts as input the coordinates of an interior point (x, y), a fill color and a
boundary color.
• Starting from point (x, y), the method tests neighboring positions to determine
whether they are of the boundary color.
• If not, they are painted with the fill color and their neighbors are tested.
• This process continues until all pixels up to the boundary color for the area have
been tested.
Boundary-Fill
Algorithm
Boundary-Fill Algorithm

void boundaryFill4 (int x, int y, int fillColor, int


borderColor)
{
int interiorColor;

/* Set current color to fillColor, then perform


following oprations. */

getPixel (x, y, interiorColor);


if ((interiorColor != borderColor) &&
(interiorColor != fillColor)) {
setPixel (x, y); // Set pixel color to fillColor
boundaryFill4 (x + 1, y , fillColor, borderColor);
boundaryFill4 (x - 1, y , fillColor, borderColor);
boundaryFill4 (x , y + 1, fillColor, borderColor);
boundaryFill4 (x , y - 1, fillColor, borderColor)
}
}
Boundary-Fill Algorithm

1 2
1
Polygon Filling:
Boundary-Fill Algorithm

1 3
1
Polygon Filling:
Boundary-Fill Algorithm

5 6
6
4 5
1 4
1
Polygon Filling:
Boundary-Fill Algorithm

4 5
1 4
1
Polygon Filling:
Boundary-Fill Algorithm

4
1 4
1
Polygon Filling:
Boundary-Fill Algorithm

7
1 7
1
Polygon Filling:
Boundary-Fill Algorithm

8 1 8
1
Polygon Filling:
Boundary-Fill Algorithm

1 9
9 1
Polygon Filling:
Boundary-Fill Algorithm

1
1
Seed Fill Algorithm
• These algorithms assume that at least one pixel interior
to a polygon or region is known
• Regions maybe interior or boundary defined

Interior-defined region Interior-defined region


A Simple Seed Fill Algorithm
Push the seed pixel onto the stack
While the stack is not empty
Pop a pixel from the stack
Set the pixel to the required value
For each of the 4 connected pixels
Adjacent to the current pixel, check if it is a boundary pixel
or if it has already been set to the required value.
In either case ignore it. Otherwise push it onto the stack
• The algorithm can be implemented using 8 connected
pixels
• It also works with holes in the polygons

You might also like