You are on page 1of 47

Clipping:

LINES
and
d
POLYGONS

OUTPUT

INPUT

Solving
g Simultaneous equations
q
using parametric form of a line:

P(t ) = (1 t ) P0 + tP1
where
h , P(0) = P0 ; P(1) = P1
S l
Solve
with
ith respective
ti
pairs:
i

Kx X0
t lx =
X1 X0

t ly =

K y Y0
Y1 Y 0

Vertical Line:
X = Kx;
Horizontal Line:
Y = Ky.

In general
general, solve for two sets of
simultaneous equations for the
parameters:
t
tedge and tline
Check if they fall within range
[0 - 1].
i.e. Rewrite

P(t ) = P0 + t ( P1 P0 )

and Solve:

t 1 ( P1 P0 ) t 2 ( P1 P0 ) = P0 P0
'

'

'

Cyrus Beck
Cyrus-Beck
Line Clipping

CYRUS-BECK formulation

P ( t ) = P0 + t ( P1 P0 )

Define,

where , P (0) = P0 ; P (1) = P1

F(N, PE) =
N [P(t) PE]
N.[P(t)

Solve for t using:

P(t) - PE

PE

P1

N.[P(t) PE] = 0;

F(N,PE)<0
F(N,PE)>0
P0

F(N,PE)=0
N

PE

P( ) - PE
P(t)
P1
F(N,PE)<0

F(N,PE)>0
P0

F(N,PE)=0
N

Solve for t using:


N.[P(t) PE] = 0;

N .[[ P0 + ( P1 P0 )t PE ] = 0;
Substitute, D = P1 P0 ;
N .[ P0 PE ]
To Obtain: t =
N .D

To ensure valid value of t, denominator must


be non-zero.
Assuming,
A
i
th
thatt D,
D N <> 0,
0 check
h k if
if:
N.D <> 0. i.e. edge and line are not parallel.
If they are parallel ?
Use the
U
th above
b
expression
i
off t to
t obtain
bt i all
ll
the four intersections:
Select a point on each of the four edges of
the clip rectangle.
Obtain four values of t.
Find
Fi d valid
lid iintersections
t
ti
How to implement the last step ?

Consider this example


p
P1

PE

PE
P0

P1

L2

PL
P0
P0

P1

L1

PL
PL

PE
L3
PE

PL

Steps:
If any value of t is outside the range [0 1]
reject
j t it.
it
Else, sort with increasing values of t.
This solves L1, but not lines L2 and L3.
Criteria to choose intersection points,
PE or PL:
Move from p
point P0 to P1;
If you are entering edges inside half-plane,
th
then
th
thatt intersection
i t
ti
point
i t is
i marked
k d PE;
PE
else,
l
if you are leaving
l
i
it is
i marked
k d as PL
PL.

Check the angle


g of D and N vectors,, for
each edge separately.
If angle between D and N is:
>90 deg., N.D < 0, mark the point as PE,
store tE (i) = t
<90 deg., N.D > 0, mark the point as PL,
store
t
tL(i) = t
Find the maximum
ma im m value
al e of tE, and
minimum value of tL for a line.
If tE < tL choose pair of parameters as
valid intersections on the line
line. Else NULL
NULL.

P1

PE
P0

L3

PL

P0

L1

P0

P1

PL

PL
PL

PE
L2
PE

PE

P1

Calculations for parametric line Clipping


Clip
Edge

Left:
X = Xmin
Ri ht
Right:
X = Xmax
Bottom:
Y = Ymin
Top:
Y = Ymax

Normal
N
( 1 0)
(-1,
(1, 0)
(0, -1)
1)
(0, 1)

PE

(Xmin,Y)
Y)
(Xmax,Y)
(X,Ymin)
(X,Ymax)

P0 - PE
((X0 Xmin,
Y0 Y)
(X0 Xmax,
Y0 Y))
(X0 X,
Y0 Ymin)
(X0 X,
Y0 Ymax)

N.[P0 PE ]
t=
N.
ND
((X0 X min )
(X1 X 0 )

(X 0 X max )
(X 1 X 0 )
(Y 0 Y min )
(Y 1 Y 0 )

( 0 Y max )
(Y
(Y 1 Y 0 )

- Exact coordinates for PE is irrelevant.

Cohen Sutherland
Cohen-Sutherland
Line Clipping

Region Outcodes:

1001

1000

1010

0001

0000

0010

0101

0100

xmin

0110

xmax

ymax
ma
ymin

Bit
Number
FIRST
(MSB)
SECOND

THIRD

FOURTH
(LSB)

1
Above
Top edge
Y > Ymax
Below
Bottom edge
Y < Ymin
Right of
Right edge
X > Xmax
L ft off
Left
Left edge
X < Xmin

0
Below
B
l
Top
T
edge
d
Y < Ymax
Above Bottom edge
Y > Ymin
Left of Right edge
X < Xmax
Right of Left edge
X > Xmin

First Step:
p Determine the bit values of the two
end-points of the line to be clipped.
To determine the bit value of any point, use:
b1 = sgn(Ymax - Y);
b2 = sgn(Y - Ymin);
b3 = sgn(Xmax - X);
b4 = sgn(X - Xmin);
Use these end-point codes to locate the line.
Various possibilities:
If b
both
th endpoint
d i t codes
d
are [0000]
[0000], th
the li
line li
lies
completely inside the box, no need to clip. This is
th simplest
the
i
l t case (e.g.
(
L1).
)
Any line has 1 in the same bit positions of both
the endpoints, it is guaranteed to lie outside the
box completely (e.g.
(e g L2 and L3 ).
)

1001

1000

1010

0001

0000

0010

0101

0100

xmin

0110

ymax
ymin

xmax

L4

Neither completely
L2
reject nor inside the box:
Lines: L4 and L5, - needs
more processing.
What about Line L6 ?

L6

L1

L5
L3

Processing of lines, neither Completely


IN or OUT; e.g. Lines:
i
L4, L5 and
d L6.
B i idea:
Basic
id
Clip parts of the line in any order (consider
from top or bottom).
bottom)
Algorithm Steps:
Compute outcodes of both endpoints to
check for trivial acceptance or rejection
(AND logic).
If not so, obtain an endpoint that lies outside
the box (at least one will ?).
Using the outcode, obtain the edge that is
crossed first.

1001

1000

1010

0001

0000

0010

0101

0100

xmin

0110

ymax
ymin
D

xmax

L4
B

A
L5

N
M
L6

F
K
J

Coordinates for intersection, for clipping


w.r.t edge:
Inputs: Endpoint coordinates:
(X0, Y0) and (X1, Y1)
OUTPUT
OUTPUT:
Edge for clipping
(obtained using
outcode
t d off currentt endpoint).
d i t)

Obtain corresponding
intersection points
CLIP (replace the endpoint by the
intersection point) w.r.t. the edge.
Compute the outcode for the updated
endpoint and repeat the iteration
iteration, till
it is 0000.
Repeat the above steps, if the other
endpoint is also outside the area
area.

e.g. Take Line L5 (endpoints - E and I):


E has outcode 0100 (to be clipped w.r.t. bottom
edge);
So EI is clipped to FI;
D
Outcode of F is 0000;
C
I
L4
But outcode of I is 1010;
Cli (w.r.t.
Clip
(
t top
t
edge)
d )
B
H
to get FH.
A
G
O t d off H is
Outcode
i 0010;
0010
Clip (w.r.t. right edge)
L5
N
to get FG;
M
F
Since outcode of G
K
L6
is 0000, display the
E
final result as FG.
FG
J

Formulas for clipping


pp g w.r.t. edge,
g , in cases of:
T
Top
Edge
Ed
:
Bottom Edge:

(Ymax Y0 )
X = X0 + (X1 X0 )*
(Y1 Y0 )
(Ymin Y0 )
X = X 0 + (X 1 X 0 )*
(Y1 Y0 )

Right Edge:

(X max X 0 )
Y = Y0 + (Y1 Y0 ) *
(X1 X 0 )

Left edge:

(X min X 0 )
Y = Y0 + (Y1 Y0 ) *
(X1 X 0 )

Lets compare with Cyrus-Beck formulation

Calculations for parametric line Clipping


Clip
Edge

Left:
X = Xmin
Ri ht
Right:
X = Xmax
Bottom:
Y = Ymin
Top:
Y = Ymax

Normal
N
( 1 0)
(-1,
(1, 0)
(0, -1)
1)
(0, 1)

PE

(Xmin,Y)
Y)
(Xmax,Y)
(X,Ymin)
(X,Ymax)

P0 - PE
((X0 Xmin,
Y0 Y)
(X0 Xmax,
Y0 Y))
(X0 X,
Y0 Ymin)
(X0 X,
Y0 Ymax)

N.[P0 PE ]
t=
N.
ND
((X0 X min )
(X1 X 0 )

(X 0 X max )
(X 1 X 0 )
(Y 0 Y min )
(Y 1 Y 0 )

( 0 Y max )
(Y
(Y 1 Y 0 )

- Exact coordinates for PE is irrelevant.

Liang Barsky
Liang-Barsky
Line Clipping
pp g

Consider parametric equation of a line segment:

X = X 1 + uX ; Y = Y1 + uY , 0 u 1.
where,
where

X = X 2 X 1 ; Y = Y2 Y1

A point is considered to be within a


rectangle, iff

XWmin X 1 + uX

XWmax ;

YWmin Y1 + u Y

YWmax .

Each
E
h off th
these ffour iinequalities,
liti
can be
b
expressed as:

u. pk = qk ; k = 1,2,3,4

where, the parameters are defined as:

p1 = X ,

q1 = X 1 XWmin

p2 = X ,

q2 = XWmax X 1

p3 = Y ,

q3 = Y1 YWmin

p4 = Y ,

q4 = YWmax Y1

Based on these four inequalities


inequalities, we can
find the following conditions of line clipping:
If pk = 0, the line is parallel K = 1
to the corresponding clipping K = 2
boundary:
K=3
K=4

Left
Right
Bottom
Top

If for any k, for which pk = 0:


- qk < 0, the line is completely outside
the boundary
- qk > 0, the line is inside the parallel
clipping boundary.

If pk < 0,
0 the
th line
li
proceeds
d from
f
th
the outside
t id
to the inside of the particular clipping boundary
( i
(visualize
li
infinite
i fi it extensions
t
i
in
i both).
b th)
If pk > 0, the line proceeds from the inside to
the outside of the particular clipping boundary
(visualize infinite extensions in both).
In both these cases, the intersection
parameter is
i calculated
l l
d as:

u = q k / pk

The Algorithm:
Initialize line intersection parameters to:
u1 = 0; u2 = 1;
Obtain pi, qi; for
o i = 1,, 2,, 3, 4.
Obta
Using
g pi, qi - find if the line can be rejected
j
or
the intersection parameters must be adjusted.
If pk < 0, update u1 as:

max[[ 0, (qk / pk )], k = 1 4

If pk > 0, update u2 as:

min[ 1, (qk / pk )], k = 1 4

After
Aft
update,
d t if u1 > u2 : reject
j t the
th line.
li

pk < 0 : u1 = max[ 0, (qk / pk )], k = 1 4

L4

p k > 0 : u2 = min[ 1, (qk / pk )], k = 1 4

L2

K = 1 Left; K = 2 Right
K = 3 Bottom;; K = 4 Top
p

p1 = X ,

L1

q1 = X 1 XWmin

p2 = X ,

q2 = XWmax X 1

p3 = Y ,

q3 = Y1 YWmin

L6

L5
L3

p4 = Y ,

q4 = YWmax Y1

Do for
L3, L5 & L6.

L1: (0, 1); /*Analyze the line in both directions.


L2: [max(0,
[
(0 -d
d2, -d
d3)
min(1,
i (1 -d
d1, d4(<1))]
= (0, -d1) hence reject.
L4: [max(0
[max(0, -d2, -d3) min(1
min(1, d1, d4)]
= (0, d4) (why ?) so accept and clip

What about Circle/Ellipse


/
p
clipping
pp g
or for curves ??

INPUT

OUTPUT

Can you do a inside-outside test,


f
for
the
h object
bj
vs. rectangle
l ?

POLYGON
CLIPPING

Examples of Polygon Clipping

CONVEX SHAPE

MULTIPLE
COMPONENTS

CONCAVE
SHAPE

Methodology: CHANGE position of


vertices
ti
for
f
each
h edge
d
b
by li
line clipping
li i
M
May
have
h
tto add
dd new vertices
ti
to
t the
th list.
li t

Processing of Polygon
vertices against boundary

IN

OUT

P
S and P
both OUT
Output: Null.
S

Polygon
being
clipped
li
d

Clip
y
boundary

(No output)

Processing of Polygon
vertices against boundary

IN

P: second
output

OUT

S
S OUT;
P IN;
Output: i and P

i: first
output

Processing of Polygon
vertices against boundary

IN

OUT
S and
dPb
both
th IN

Output: P
P.

Polygon
b i
being
clipped

P: Output

Clip
boundary

Processing of Polygon
vertices against boundary

IN

OUT
S IN; P OUT
Output: i

P
S
i output

Problems with multiple components

INPUT
OUTPUT

Problems with multiple components

Now output is as above

Desired Output

Any Idea ??
the modified
Weiler-Atherton
Weiler
Atherton algorithm

Solution for multiple


p
components

For say, clockwise processing of polygons,


follow:
For OUT -> IN pair, follow the polygon
b
boundary
d
For IN -> OUT pair, follow Window boundary
i clockwise
in
l k i
direction
di
ti

For say, clockwise processing of polygons,


follow:
For OUT -> IN pair, follow the polygon
boundary
For IN -> OUT pair, follow Window boundary
in clockwise direction

You might also like