D7A_DM_chapter 4....pdf

33
1 Chapter 04:Filling Algorithms

Transcript of D7A_DM_chapter 4....pdf

Page 1: D7A_DM_chapter 4....pdf

1

Chapter 04:Filling Algorithms

Page 2: D7A_DM_chapter 4....pdf

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

Page 3: D7A_DM_chapter 4....pdf

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

Page 4: D7A_DM_chapter 4....pdf

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

Page 5: D7A_DM_chapter 4....pdf

5

Polygons

convex

all interior angles are

<1800

concave

at least one angle is

>1800

Page 6: D7A_DM_chapter 4....pdf

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

Page 7: D7A_DM_chapter 4....pdf

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

Page 8: D7A_DM_chapter 4....pdf

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

Page 9: D7A_DM_chapter 4....pdf

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

Page 10: D7A_DM_chapter 4....pdf

10

Inside-Outside Tests

• P1 is an interior point.

• P2 is an exterior point.

p1 p2

Page 11: D7A_DM_chapter 4....pdf

11

Fill Area Algorithms

Page 12: D7A_DM_chapter 4....pdf

Area-Filling Algorithms:

Introduction

Assign or fill color to the created

polygon (inside region).

Basic idea:

Filling Mode

Assign red color

12

Page 13: D7A_DM_chapter 4....pdf

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

Page 14: D7A_DM_chapter 4....pdf

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

Page 15: D7A_DM_chapter 4....pdf

Area-Filling Algorithms

Scan-Line

Algorithm To fill simple convex and

concave polygons.

Seed Fill Algorithm To fill arbitrary complex,

irregular boundaries.

15

Page 16: D7A_DM_chapter 4....pdf

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

Page 17: D7A_DM_chapter 4....pdf

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.

Page 18: D7A_DM_chapter 4....pdf

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

Page 19: D7A_DM_chapter 4....pdf

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

Page 20: D7A_DM_chapter 4....pdf

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

Page 21: D7A_DM_chapter 4....pdf

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

Page 22: D7A_DM_chapter 4....pdf

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

Page 23: D7A_DM_chapter 4....pdf

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)

Page 24: D7A_DM_chapter 4....pdf

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.

Page 25: D7A_DM_chapter 4....pdf

25

Boundary-Fill Algorithm

The fill method

can be applied

to a 4-connected

area or to an 8-

connected area

Page 26: D7A_DM_chapter 4....pdf

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

Page 27: D7A_DM_chapter 4....pdf

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

}

}

Page 28: D7A_DM_chapter 4....pdf

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

}

}

Page 29: D7A_DM_chapter 4....pdf

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

Page 30: D7A_DM_chapter 4....pdf

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

Page 31: D7A_DM_chapter 4....pdf

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

}

}

Page 32: D7A_DM_chapter 4....pdf

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

}

}

Page 33: D7A_DM_chapter 4....pdf

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)