Polygon Filling -...

50
Polygon Filling Goal intensify the pixels that belong to the polygon Issues which pixels belong to the polygon Approach use a (horizontal) scan line that traverses the polygon intensify the pixels along the spans (the segments of intersection) exploit spatial coherence – little change along a span (span coherence) or between scan lines (scan-line coherence)

Transcript of Polygon Filling -...

Polygon FillingGoal

intensify the pixels that belong to the polygon

Issues

which pixels belong to the polygon

Approach

use a (horizontal) scan line that traverses the polygon

intensify the pixels along the spans (the segments of intersection)

exploit spatial coherence – little change along a span (span coherence)

or between scan lines (scan-line coherence)

Rectangle FillingSpecial case

filling rectangles specified by xmin, xmax, ymin, ymax

def drawRectangle(xmin, xmax, ymin, ymax):

for y in [ymin : ymax]: # for each vertical scan line:

for x in [xmin : xmax]: # for each horizontal span:

setPixel(x, y, color)

Rectangle FillingIssues in rectangle (and polygon) filling

draw R1(8, 5, 16, 10) and R2(16, 5, 24, 10)

0 4 128 16 2016 240

5

10

Rectangle FillingIssues in rectangle (and polygon) filling

draw R1(8, 5, 16, 10) and R2(16, 5, 24, 10)

00

5

10

4 128 16 2016 24

Rectangle FillingIssues in rectangle (and polygon) filling

draw R1(8, 5, 16, 10) and R2(16, 5, 24, 10)

shared edge at x = 16 – order of drawing gives different visual results

00

5

10

4 128 16 2016 24

Rectangle FillingIssues in rectangle (and polygon) filling

adjacent rectangles (shared vertices, edges)

Solution

do not draw the right-most and top-most edges of the rectangle

(arbitrary decision – could have chosen to omit left-most, bottom-most)

00

5

10

4 128 16 2016 24

Rectangle FillingIssues in rectangle (and polygon) filling

adjacent rectangles (shared vertices, edges)

Solution

do not draw the right-most and top-most edges of the rectangle

00

5

10

4 128 16 2016 24

Polygon FillingSame solution for general polygons

do not draw the right-most and top-most edges of the polygon

draw only pixels interior to the polygon

Example

polygon outline if we used the line drawing algorithm for each edge

Polygon FillingSame solution for general polygons

do not draw the right-most and top-most edges of the polygon

draw only pixels interior to the polygon

Example

polygon outline if we used the line drawing algorithm for each edge

line drawing algorithm may pick pixels that are outside the ideal boundary

Polygon FillingSame solution for general polygons

do not draw the right-most and top-most edges of the polygon

draw only pixels interior to the polygon

Example

polygon outline if we used the line drawing algorithm for each edge

line drawing algorithm may pick pixels that are outside the ideal boundary

Polygon FillingSame solution for general polygons

do not draw the right-most and top-most edges of the polygon

draw only pixels interior to the polygon

Example

polygon outline if we used the line drawing algorithm for each edge

line drawing algorithm may pick pixels that are outside the ideal boundary

Polygon FillingSame solution for general polygons

do not draw the right-most and top-most edges of the polygon

draw only pixels interior to the polygon

Example

polygon outline if we used the line drawing algorithm for each edge

line drawing algorithm may pick pixels that are outside the ideal boundary

Polygon FillingCheck the x-coordinate of intersections between scan line and edges

if entering the polygon (i.e. left edge ), round x up (11.4 12)→

if leaving the polygon (i.e. right edge), round x down (16.7 16)→

If the intersection occurs at integer x-coordinate

left edge – draw pixel at x (11 11)→

right edge – draw pixel at x-1 (11 10)→

11.4 12→ 16.7 16→ 11 11→ 16 15→

Polygon FillingCheck the x-coordinate of intersections between scan line and edges

if entering the polygon (i.e. left edge ), round x up (11.4 12)→

if leaving the polygon (i.e. right edge), round x down (16.7 16)→

If the intersection occurs at integer x-coordinate:

left edge – draw pixel at x, right edge – draw pixel at x-1

4 128 16 2016 240

4

Polygon FillingHorizontal edges are ignored:

either they don't need to be drawn, since they are top edges

or they will be drawn as part of a longer span

4 128 16 2016 240

4

should be ignored

will be filled

Polygon FillingScan line at level y = 4 intersects edges at x = 11.4, 16.7, 21.6, 26

Every pair of intersections represents a span that needs to be filleds1 = (11.4, 16.7) s2 = (21.6, 26)

4 128 16 2016 240

4

Polygon FillingScan line at level y = 4 intersects edges at x = 11.4, 16.7, 21.6, 26

Every pair of intersections represents a span that needs to be filleds1 = (11.4, 16.7) s2 = (21.6, 26)

For each span round up left coordinate, round down the right coordinate

s1 = (12, 16) s2 = (22, 25)

Intensify pixels at scan line y = 4 and x = [12..17] and x = [23..25]

4 128 16 2016 240

4

Polygon FillingMove the scan line by one unit (to draw the next row of pixels)

Scan line now intersects edges at x = 11, 26ignore horizontal edges and edges whose top vertex is on the scan line

Every pair of intersections represents a span that needs to be filleds1 = (11, 26) round up/down s1 = (11, 25)

Intensify pixels at scan line y = 5 and x = [11..25]

4 128 16 2016 240

4

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

filled as desired

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

ignored as desired

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

Polygon FillingAlgorithm

discard all horizontal edgesset up a scan line at the bottom of the polygonrepeat

find the x intersections of scan line with the edges (active edges)every pair of intersections represents a span to fill/intensifymove the scan line up, discard edges with top vertex on scan line

until scan line reaches top of polygon

ignored as desired

Polygon FillingData structures

Edge – representation of a polygon edge

ET (edge table) – all edges sorted by ymin, of their lower vertex

AEL (active edge list) – the edges intersected by current scan line

Algorithmstore all edges in ET buckets based on ymin of their lower vertex

yscan = lowest y coordinate of the polygon

AEL = empty list

do

move from ET to AEL all edges whose ymin = yscan

sort edges in AEL based on their x-intersection with yscan

fill pixels between successive pairs in AEL

yscan = yscan + 1

remove from AEL all edges whose ymax = yscan

for each edge in AEL update x-intersection with yscan

while AEL not empty

Polygon FillingData structures

Edge – representation of a polygon edge

ymin, ymax – y coordinate of lower and higher vertex

x – intersection with current scan line

m – slope of the edge (to update the x intersection)

ET (edge table) –all edges sorted by ymin, of their lowest vertex

a Hash Map with integer key (ymin) and list values (edges)

AEL (active edge list) – a Linked List of edges

edges with ymin = 10

edges with ymin = 5

edges with ymin = 4

edges with ymin = 1

ymin | ymax | x | m

Polygon FillingData structures

Edge

updateX – adds 1/m to current x value (m is slope of the edge)

leftX – rounds up the x value (if left side of a span)

rightX – rounds down the x value (if right side of a span)

ET (edge table)

STL map sorted on the key (ymin)

AEL (active edge list) – from STL list

sort

remove

merge

Polygon FillingIncremental computation of x-coordinate of the scan line and edge intersection

Use scan line coherence – relatively small change between scan lines

scan line equation y = mx + k → x = y/m + n , n = -k/m

to find x” x” = y”/m + n

= (y’ + 1)/m + n since y” = y'+1

= y’/m + 1/m + n

= y’m + n + 1/m

= x’ + 1/m

To get the next x intersection just add 1/m to the previous one

x’x”

y’

y”

Integer ArithmeticInteger arithmetic for computing x-coordinate of the scan line and edge intersection

x” = x’ + 1/m m = dy/dx 1/m = dx/dy

From earlier slide we keep adding 1/m = dx/dy , so the sequence of intersections is

x, x + dx , x + 2dx , x + 3dx , x + 4dx , etc. dy dy dy dy

Example: edge = (x0, y0, x1, y1) = (3, 2, 5, 7) dx = 5-3 = 2, dy = 7-5 = 5

3, 3 + 2 , 3 + 4 , 3 + 6 = 4 + 1 , 4 + 3 , 4 + 5 = 5 + 0

5 5 5 5 5 5 5

Integer ArithmeticInteger arithmetic for computing x-coordinate of the scan line and edge intersection

x” = x’ + 1/m m = dy/dx 1/m = dx/dy

From earlier slide we keep adding 1/m = dx/dy , so the sequence of intersections is

x, x + dx , x + 2dx , x + 3dx , x + 4dx , etc. dy dy dy dy

To handle overflow in numerator, keep track of its value (i.e. k*dx) and current x

if numer >= dy:

numer = numer - dy x = x + 1

Example: edge = (x0, y0, x1, y1) = (3, 2, 5, 7) dx = 5-3 = 2, dy = 7-5 = 5

3, 3 + 2 , 3 + 4 , 3 + 6 = 4 + 1 , 4 + 3 , 4 + 5 = 5 + 0

5 5 5 5 5 5 5

only for slope m > 1

Integer ArithmeticCalculating the x intersection given the previous sequence

3, 3 + 2 , 3 + 4 , 3 + 6 = 4 + 1 , 4 + 3 , 4 + 5 = 5 + 0 5 5 5 5 5 5 5

if a left edge (i.e. entering the polygon) – round up when numer > 0

3, 4, 4, 5, 5, 5

if a right edge (i.e. leaving the polygon) – round down, including integer values

2, 3, 3, 4, 4, 4

left edge right edge

Integer ArtihmeticInteger arithmetic for slope 0 < m < 1

x” = x’ + 1/m m = dy/dx 1/m = dx/dy

Same idea, but dx/dy has a whole part, w, and fractional part, f

Example: edge = (x0, y0, x1, y1) = (3, 2, 8, 5) dx = 8-3 = 5, dy = 5-2 = 3

w = dx/dy = 5/3 = 1

f = dx%dy = 5%3 = 2

3

3 + 5/3 =

Integer ArtihmeticInteger arithmetic for slope 0 < m < 1

x” = x’ + 1/m m = dy/dx 1/m = dx/dy

Same idea, but dx/dy has a whole part, w, and fractional part, f

Example: edge = (x0, y0, x1, y1) = (3, 2, 8, 5) dx = 8-3 = 5, dy = 5-2 = 3

w = dx/dy = 5/3 = 1

f = dx%dy = 5%3 = 2

3

3 + 5/3 = 3 + 3/3 + 2/3 = 3 + w + f/3 = 4 + f/3 = 4 + 2/3

4 + 2/3 + 5/3 = 4 + 2/3 + w + f/3 = 5 + (2+f)/3 = 5 + 4/3 = 5 + 1 + 1/3 = 6 + 1/3

6 + 1/3 + 5/3 = 6 + 1/3 + w + f/3 = 7 + (1+f)/3 = 7 + 3/3 = 7 + 1 + 0/3 = 8 + 0/3

Procedure

always add to x the whole part, w (for slopes m > 1, w = 0)

always add to numer the fract. part, f, and adjust if numer >= dy