D7A_DM_chapter 4....pdf

Post on 23-Dec-2015

39 views 3 download

Tags:

transcript

1

Chapter 04:Filling Algorithms

Polygon

2

A polyline is a chain of connected line segments. When starting point and

terminal point of any polyline is same, i.e. when polyline is closed then it

is called polygon.

2

Polygon Classifications

3

interior angle: is an angle inside the polygon boundary

that is formed by two adjacent edges.

convex polygon: interior angles of a polygon are less

than or equal to 180°

Or Convex : is a polygon in which the line segment joining

any two points within the polygon lies completely inside

the polygon

3

Polygon Classifications

4

Concave polygon: the polygon that is not convex.

or Concave: is a polygon in which the line segment

joining any two points within the polygon may not lie

completely inside the polygon.

4

5

Polygons

convex

all interior angles are

<1800

concave

at least one angle is

>1800

6

Identifying Concave Polygons

Method

if some vertices are on one

side and some on the

other side of an

extension line, then

concave

v1 v2

v3

v4 v5

E1

E4

E3

E2 E5

7

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

8

Polygon Tables

E1 v5

v4

v3

v2

v1

E2

E3

E4

E5

E6

S1 S2

Vertex

Table

v1: x1, y1, z1

v2: x2, y2, z2

v3: x3, y3, z3

v4: x4, y4, z4

v5: x5, y5, z5

Edge Table

E1: v1, v2

E2: v2, v3

E3: x3, v1

E4: v3, v4

E5: v4, v5

E6: v5, v1

Surface Table

S1: E1, E2, E3

S2: E3, E4, E5, E6

9

Inside-Outside Tests

o Odd-even rule (or odd parity rule) o Conceptually drawing a line from any position P to

a distant point outside the object.

o Counting the number of edge crossings along the line. • P is interior: odd

• P is exterior: even

10

Inside-Outside Tests

• P1 is an interior point.

• P2 is an exterior point.

p1 p2

11

Fill Area Algorithms

Area-Filling Algorithms:

Introduction

Assign or fill color to the created

polygon (inside region).

Basic idea:

Filling Mode

Assign red color

12

13

Fill Algorithms

Given the edges defining a polygon,

and a color for the polygon, we need

to fill all the pixels inside the

polygon.

Three different algorithms:

1. Scan-line fill

2. Boundary fill

3. Flood fill

14

Region Filling

• 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

Area-Filling Algorithms

Scan-Line

Algorithm To fill simple convex and

concave polygons.

Seed Fill Algorithm To fill arbitrary complex,

irregular boundaries.

15

16

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

17

Fill Area Algorithms

Fill-Area algorithms are used to

fill the interior of a polygonal

shape.

Many algorithms perform fill

operations by first identifying

the interior points, given the

polygon boundary.

18

The basic filling algorithm is commonly used

in interactive graphics packages, where the

user specifies an interior point of the region to

be filled.

Basic Filling Algorithm

4-connected pixels

19

[1] Set the user specified point.

[2] Store the four neighboring pixels in

a stack.

[3] Remove a pixel from the stack.

[4] If the pixel is not set,

Set the pixel

Push its four neighboring pixels

into the stack

[5] Go to step 3

[6] Repeat till the stack is empty.

Basic Filling Algorithm

20

Requires an interior point.

Involves considerable amount of

stack operations.

The boundary has to be closed.

Not suitable for self-intersecting

polygons

Basic Filling Algorithm

21

Boundary Fill Algorithm

For filling a region with a single

boundary color.

Condition for setting pixels: Color is not the same as border color

Color is not the same as fill color

Flood Fill Algorithm

For filling a region with multiple

boundary colors.

Condition for setting pixels: Color is same as the old interior color

Types of Basic Filling Algorithms

Filling Irregular Boundaries

boundary fill: expand and fill region until you reach

boundary color

flood fill: expand and fill region while you find interior color

Boundary Fill

Interior Fill

23

Boundary-Fill Algorithm

In many graphics packages the user can fill a region (defined by a boundary).

In the figure, the boundary is red and the filling color is blue

The user needs to click inside the region (seed)

24

Boundary Fill

Basic concept:

To start from a given interior position and paint outward

from this point until we encounter the specified boundary

conditions.

25

Boundary-Fill Algorithm

The fill method

can be applied

to a 4-connected

area or to an 8-

connected area

26

Start at point inside a region and paint the interior outward

toward the boundary.

Particularly useful in interactive painting program

Input

interior point (x, y)

color to be filled

boundary color

Starting from (x, y) the procedure tests neighboring

pixels to determine whether they are of the boundary

color.

If pixels = boundary color : stop.

boundary color : paint with filled color and

repeat procedure

Boundary-Fill Algorithm

27

Boundary Fill Suppose that the edges of the polygon has already been

colored.

Suppose that the interior of the polygon is to be colored a different color from the edge.

Suppose we start with a pixel inside the polygon, then we color that pixel and all surrounding pixels until we meet a pixel that is already colored.

void boundaryFill(int x, int y, int fillColor, int borderColor)

{

int interiorColor;

getPixel(x,y,interiorColor);

if ((interiorColor!=borderColor)&&(interiorColor!=fillColor))

{

setPixel(x,y,fillColor);

boundaryFill(x+1,y,fillColor,borderColor);

boundaryFill(x-1,y,fillColor,borderColor);

boundaryFill(x,y+1,fillColor,borderColor);

boundaryFill(x,y-1,fillColor,borderColor);

}

}

28

Boundary-Fill Algorithm

void boundaryFill4 (int x, int y, int fill, int boundary)

{ int current;

current = getPixel (x, y);

if ((current != boundary) && (current != fill))

{

setColor (fill);

setPixel (x, y);

boundaryFill4 (x+1, y, fill, boundary);

boundaryFill4 (x-1, y, fill, boundary);

boundaryFill4 (x, y+1, fill, boundary);

boundaryFill4 (x, y-1, fill, boundary);

}

}

29

Flood Fill

• For an area not defined within a single

color boundary.

• Paint such areas by replacing a

specified interior color instead of

searching for a boundary color.

• Start from a specified interior point (x, y)

• Reassign all pixel values that are currently

set to a given interior color(s).

30

Flood Fill Algorithm

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

31

Flood Fill 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.

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

int color;

getPixel(x,y,color)

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

}

}

32

Flood-Fill Algorithm

void floodFill4 (int x, int y, int fillColor, int oldColor)

{

if (getPixel (x, y) == oldColor)

{

setColor (fillColor);

setPixel (x, y);

floodFill4 (x+1, y, fillColor, oldColor);

floodFill4 (x-1, y, fillColor, oldColor);

floodFill4 (x, y+1, fillColor, oldColor);

floodFill4 (x, y-1, fillColor, oldColor);

}

}

33

Fill Methods

BOUNDARY

We need to

specify:

Interior point

Fill color

Border color

Condition:

check current

point color

Keep filling

while not

border color

and not fill color

FLOOD

We need to

specify:

Interior point

Fill color

Interior color

Condition:

check current point

color

Keep filling while

interior color

(and not fill color)