Polygons - E-STUDYthirdyearengineering.weebly.com/uploads/.../polygon... · Scan line polygon...

Preview:

Citation preview

Polygons UNIT - III

Agenda

Polygon Terminology

Types of polygons

Inside Test

Polygon Filling Algorithms

Scan-Line Polygon Fill Algorithm

Flood-Fill Algorithm

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

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

5

Polygon Representation and Entering

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

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

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

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

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

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

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:

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); } }

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

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); } }

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.

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.

Example

The boundary of a

polygon: (In

practice, a polygon

is defined as a list of

vertices.)

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.

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

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

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

Spacial Handling (cont.)

However, in this case we count p1 once (p0,p1,p2,p3),

Case 2

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.

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.

Active Edge Table

Active edges are sorted according to increasing X.

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

Recommended