Professional Documents
Culture Documents
Clipping PDF
Clipping PDF
LINES
and
d
POLYGONS
OUTPUT
INPUT
Solving
g Simultaneous equations
q
using parametric form of a line:
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,
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.
Move from p
point P0 to P1;
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.
P0 PL PL PL
L1
PL
PE
P0 L2
PE
PE
P0
Calculations for parametric line Clipping
Line Clipping
Region Outcodes:
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);
• 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).
)
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).
OUTPUT
OUTPUT:
Edge for clipping (obtained using
outcode
t d off currentt endpoint).
d i t)
Obtain corresponding
intersection points
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
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
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:
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.
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
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