50
Polygon Clipping A polygon may be outside the view area (e.g. zoom). Determine what to draw

Polygon Clipping - cs.gettysburg.educs.gettysburg.edu/~ilinkin/courses/Fall-2014/cs373/slides/clipping.pdfPolygon Clipping Examine the edges of given poly relative to current clipper

Embed Size (px)

Citation preview

Polygon ClippingA polygon may be outside the view area (e.g. zoom). Determine what to draw

Polygon ClippingA polygon may be outside the view area (e.g. zoom). Determine what to draw

Polygon ClippingThe Sutherland-Hodgman algorithm uses 4 clippers (for each side of view area)

Polygon ClippingBottom Clipper

Polygon ClippingBottom Clipper

Polygon ClippingRight Clipper

Polygon ClippingRight Clipper

Polygon ClippingTop Clipper

Polygon ClippingTop Clipper

Polygon ClippingLeft Clipper

Polygon ClippingLeft Clipper

Polygon ClippingNow we can use the polygon filling algorithm to draw the polygon

Polygon ClippingExamine the edges of given poly relative to current clipper and add point to new poly

s

p

outside viewnothing added

Polygon ClippingExamine the edges of given poly relative to current clipper and add point to new poly

s

p

entering viewoutput i and p

i

Polygon ClippingExamine the edges of given poly relative to current clipper and add point to new poly

s

p

inside viewoutput p only

Polygon ClippingExamine the edges of given poly relative to current clipper and add point to new poly

s

p

exiting viewoutput i only

i

Polygon ClippingExamine the edges of given poly relative to current clipper and add point to new poly

s

p

outside viewnothing added

s

p

entering viewoutput i and p

i

s

p

inside viewoutput p only

s

p

exiting viewoutput i only

i

Polygon ClippingBottom Clipper

Polygon ClippingBottom Clipper

outside clippernothing added

Polygon ClippingBottom Clipper

entering clipperadd i and p

Polygon ClippingBottom Clipper

inside clipperadd p only

Polygon ClippingBottom Clipper

inside clipperadd p only

Polygon ClippingBottom Clipper

inside clipperadd p only

Polygon ClippingBottom Clipper

and so on ...

Polygon ClippingBottom Clipper

last edge exiting clipperadd i only

Polygon ClippingBottom Clipper

Polygon ClippingRight Clipper

Polygon ClippingRight Clipper

exiting clipperadd i only

Polygon ClippingRight Clipper

outside clipperadd i only

Polygon ClippingRight Clipper

entering clipperadd i and p

Polygon ClippingRight Clipper

exiting clipperadd i only

Polygon ClippingRight Clipper

entering clipperadd i and p

Polygon ClippingRight Clipper

inside clipperadd p only

Polygon ClippingRight Clipper

inside clipperadd p only

Polygon ClippingRight Clipper

inside clipperadd p only

Polygon ClippingRight Clipper

and so on ...

Polygon ClippingRight Clipper

last edge inside clipperadd p only

Polygon ClippingRight Clipper

Polygon ClippingThe algorithm works for view area that is any convex polygon (e.g. cockpit view)

VectorsDescribe a direction in space

VectorsDescribe a direction in space

all vectors below represent the same direction

represented by the (x, y) coords of the vector anchored at the origin

VectorsVector (direction) defined by two points

vx = P

1(x) – P

0(x) different from v

x = P

0(x) - P

1(x)

vy = P

1(y) – P

0(y) v

y = P

0(y) - P

1(y)

P0

P1v

v

VectorsVector (direction) defined by two points

vx = P

1(x) – P

0(x)

vy = P

1(y) – P

0(y)

In vector form

v = P0P1 = P

1 - P

0P0

P1v

vP0

P1

Vector OperationsLength

|v| = sqrt(vx·v

x + v

y·v

y)

Addition/Subtraction

w = u ± v wx = v

x ± u

x, w

y = v

y ± u

y

Scalar multiplication

u = c·v ux = c·v

x, u

y = c·v

does not change the direction, just the magnitude

Dot product (vector multiplication)

c = u·v = vx·u

x + v

y·u

y = |v|·|u|·cos a

dot product is a scalar (number), not a vector

Vector OperationsDot product is useful for determining angle between vectors

u·v > 0 if angle < 90

u·v = 0 if angle == 90

u·v < 0 if angle > 90

Line / Edge IntersectionParametric line equation given two points P

0 and P

1 on the line

P = P0 + P

0P1·t , t in (-∞ , +∞)

x = P0(x) + P

0P1(x)·t

y = P0(y) + P

0P1(x)·t

t = 0

P0

P1

t = 1

t < 0

for points

behind P0

t > 0

for points

after P1

Line / Edge Intersection

P0

P1

Pi

Given a clipper and an edge (P0,P

1)

decide if P1 is inside the visible area

find the intersection Pi

Line / Edge Intersection

P0

P1

Pe

Pi

Given a clipper and an edge (P0,P

1)

decide if P1 is inside the visible area – N·P

eP1 < 0 means P

1 is inside

pick a point Pe on the clipper

pick a vector perpendicular to clipper

use dot product

N·PeP1 < 0 means P

1 is inside N

Line / Edge Intersection

P0

P1

Pe

Pi

Given a clipper and an edge (P0,P

1)

decide if P1 is inside the visible area – N·P

eP1 < 0 means P

1 is inside

find the intersection Pi

N·PePi = 0 since N

perp. to clipper

N·(Pi-P

e) = 0 rewritten

P = P0 + P

0P1·t line equation of edge

Pi = P

0 + P

0P1·t

isome t

i will give P

i

N·(P0 + P

0P1·t

i - P

e) = 0 replace P

i

N·(P0-P

e + P

0P1·t

i) = 0 rearrange

N·(PeP0 + P

0P1·t

i) = 0

N·PeP0 + N·P

0P1·t

i) = 0

ti = –N·P

eP0 / N·P

0P1

N

PeP1

PePi

Line / Edge IntersectionGiven a clipper and an edge (P

0,P

1)

decide if P1 is inside the visible area

N·PeP1 < 0 means P

1 is inside

find the intersection Pi

Pi = P

0 + P

0P1·t

iwhere t

i = –N·P

eP0 / N·P

0P1

but since we are interested in P1

Pi = P

1 + P

1P0·t

iwhere t

i = –N·P

eP1 / N·P

1P0