Professional Documents
Culture Documents
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,
F(N, PE) =
N [P(t) PE]
N.[P(t)
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
N .[[ P0 + ( P1 P0 )t PE ] = 0;
Substitute, D = P1 P0 ;
N .[ P0 PE ]
To Obtain: t =
N .D
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.
P1
PE
P0
L3
PL
P0
L1
P0
P1
PL
PL
PL
PE
L2
PE
PE
P1
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 )
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
1001
1000
1010
0001
0000
0010
0101
0100
xmin
0110
ymax
ymin
D
xmax
L4
B
A
L5
N
M
L6
F
K
J
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.
(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 )
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 )
Liang Barsky
Liang-Barsky
Line Clipping
pp g
X = X 1 + uX ; Y = Y1 + uY , 0 u 1.
where,
where
X = X 2 X 1 ; Y = Y2 Y1
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
p1 = X ,
q1 = X 1 XWmin
p2 = X ,
q2 = XWmax X 1
p3 = Y ,
q3 = Y1 YWmin
p4 = Y ,
q4 = YWmax Y1
Left
Right
Bottom
Top
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:
After
Aft
update,
d t if u1 > u2 : reject
j t the
th line.
li
L4
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.
INPUT
OUTPUT
POLYGON
CLIPPING
CONVEX SHAPE
MULTIPLE
COMPONENTS
CONCAVE
SHAPE
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
INPUT
OUTPUT
Desired Output
Any Idea ??
the modified
Weiler-Atherton
Weiler
Atherton algorithm