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 Vertical Line:


X = Kx;
h , P(0) = P0 ; P(1) = P1
where
Horizontal Line:
S l
Solve with
ith respective
ti pairs:
i Y = Ky.
Kx − X0
t lx =
X1 − X0
K y − Y0
t ly =
Y1 − Y 0
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 F(N,PE)=0

P0
N
P( ) - PE
P(t)
PE
P1

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

P0
N

N .[[ P0 + ( P1 − P0 )t − PE ] = 0;
Solve for t using:
Substitute, D = P1 − P0 ;
N.[P(t) – PE] = 0; 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 ifif:
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
P1
PE P1
L2

PL PL PL
P0
L1 PL
PE
P0
L3
PE PE

P0
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 edge’s 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
P1 P1
PE
L3

P0 PL PL PL
L1
PL
PE
P0 L2

PE
PE
P0
Calculations for parametric line Clipping

Clip Normal N.[P0 −PE ]


PE §
P0 - PE t=
Edge N −N.ND
Left: ((X0 – Xmin, − ((X0 − X min )
( 1 0)
(-1, (Xmin,Y)
Y)
X = Xmin Y0 – Y) (X1 − X 0 )
Ri ht
Right: (X0 – Xmax, (X 0 − X max )
(1, 0) (Xmax,Y)
X = Xmax Y0 – Y)) − (X 1 − X 0 )
Bottom: (X0 – X, − (Y 0 − Y min )
(0, -1)
1) (X,Ymin) (Y 1 − Y 0 )
Y = Ymin Y0 – Ymin)
Top: (X0 – X, ( 0 − Y max )
(Y
(0, 1) (X,Ymax) − (Y 1 − Y 0 )
Y = Ymax Y0 – Ymax)
§ - Exact coordinates for PE is irrelevant.
Cohen Sutherland
Cohen-Sutherland

Line Clipping
Region Outcodes:

1001 1000 1010


ymax
ma
0001 0000 0010
ymin
0101 0100 0110

xmin xmax
Bit
1 0
Number
Above
FIRST Below
B l Top
T edge
d
Top edge
(MSB) Y < Ymax
Y > Ymax
Below
Above Bottom edge
SECOND Bottom edge
Y > Ymin
Y < Ymin
Right of
Left of Right edge
THIRD Right edge
X < Xmax
X > Xmax
L ft off
Left
FOURTH Right of Left edge
Left edge
(LSB) X > Xmin
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
ymax
0001 0000 0010
ymin
0101 0100 0110

xmin xmax
L4

• Neither completely L2
reject nor inside the box:
L1 L5
Lines: L4 and L5, - needs
more processing.
L6 L3
• What about Line L6 ?
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
ymax
0001 0000 0010
ymin
0101 0100 0110
D C
xmin L4 I
xmax
B A H
G
L5
N
M F
K
L6 E
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;
Outcode of F is 0000; D C
But outcode of I is 1010; L4 I
Cli (w.r.t.
Clip ( t top
t edge)
d )
to get FH. B A H
O t d off H is
Outcode i 0010;
0010 G
Clip (w.r.t. right edge) L5
to get FG; N
M F
Since outcode of G K
is 0000, display the L6 E
final result as FG.
FG J
Formulas for clipping
pp g w.r.t. edge,
g , in cases of:

(Ymax −Y0 )
T
Top Edge
Ed : X = X0 + (X1 − X0 )*
(Y1 −Y0 )
Bottom Edge: (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 )
Let’s compare with Cyrus-Beck formulation 
Calculations for parametric line Clipping

Clip Normal N.[P0 −PE ]


PE §
P0 - PE t=
Edge N −N.ND
Left: ((X0 – Xmin, − ((X0 − X min )
( 1 0)
(-1, (Xmin,Y)
Y)
X = Xmin Y0 – Y) (X1 − X 0 )
Ri ht
Right: (X0 – Xmax, (X 0 − X max )
(1, 0) (Xmax,Y)
X = Xmax Y0 – Y)) − (X 1 − X 0 )
Bottom: (X0 – X, − (Y 0 − Y min )
(0, -1)
1) (X,Ymin) (Y 1 − Y 0 )
Y = Ymin Y0 – Ymin)
Top: (X0 – X, ( 0 − Y max )
(Y
(0, 1) (X,Ymax) − (Y 1 − Y 0 )
Y = Ymax Y0 – Ymax)
§ - Exact coordinates for PE is irrelevant.
Liang Barsky
Liang-Barsky

Line Clipping
pp g
Consider parametric equation of a line segment:

X = X 1 + uΔX ; Y = Y1 + uΔY , 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 + uΔX ≤ 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  Left


to the corresponding clipping K = 2  Right
boundary: K=3  Bottom
K=4  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;

• Obta
Obtain pi, qi; for
o i = 1,, 2,, 3, 4.

• Usingg 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  Topp L1 L5
p1 = −ΔX , q1 = X 1 − XWmin
p2 = ΔX , q2 = XWmax − X 1 L6 L3

p3 = −ΔY , q3 = Y1 − YWmin
p4 = ΔY , q4 = YWmax − Y1
L1: (0, 1); /*Analyze the line in both directions.
Do for L2: [max(0,
[ (0 -dd2, -d
d3) min(1,
i (1 -dd1, d4(<1))]
= (0, -d1) – hence reject.
L3, L5 & L6.
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 (No output)
Polygon Clip
being y
boundary
clipped
li d
Processing of Polygon
vertices against boundary

IN OUT S

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

i: first
output
Processing of Polygon
vertices against boundary

IN OUT

S S and
dPb
both
th IN

Output: P
P.
Polygon
b i
being
clipped

Clip
P: Output 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