Solving Simultaneous equations
gq
using parametric form of a line:
1
0
)
1
(
)
0
(
)
1
(
)
(
P
P
P
P
h
tP
P
t
t
P +
−
= Vertical Line:
X = Kx;
1
0
)
1
(
;
)
0
(
, P
P
P
P
where =
=
x;
Horizontal Line:
S l ith ti i Y = Ky.
Solve with respective pairs:
X
K −
0
1
0
X
X
X
K
t x
lx
−
=
0
1
0
Y
K y
−
0
1
0
Y
Y
t y
ly
−
=
4.
In general solvefor two sets of
In general, solve for two sets of
simultaneous equations for the
t
parameters:
t and t
Check if they fall within range
tedge and tline
Check if they fall within range
[0 - 1].
i.e. Rewrite
)
(
)
( P
P
t
P
t
P +
=
and Solve:
)
(
)
( 0
1
0 P
P
t
P
t
P −
+
=
'
'
'
)
(
)
( P
P
P
P
t
P
P
t −
=
−
−
− 0
0
0
1
2
0
1
1
)
(
)
( P
P
P
P
t
P
P
t
CYRUS-BECK formulation
0
1
0
)
(
)
( P
P
t
P
t
P−
+
= Define,
1
0
0
1
0
)
1
(
;
)
0
(
,
)
(
)
(
P
P
P
P
where =
= F(N, PE) =
N [P(t) P ]
N.[P(t) – PE]
Solve for t using:
P
PE
P(t) - PE
N.[P(t) – PE] = 0;
P1
F(N,PE)<0
F(N,PE)>0 F(N,PE)=0
F(N,PE)<0
P0
P0
N
7.
P( ) P
P1
PE
P(t)- PE
P1
F(N,PE)<0
F(N,PE)>0 F(N,PE)=0
P0
N
P
t
P
P
P
N ;
0
]
)
(
[ +
N
P
P
D
P
t
P
P
P
N E
;
;
0
]
)
(
.[
0
1
0
1
0
−
=
=
−
−
+
,
Substitute
Solve for t using:
D
N
P
P
N
t E
]
.[ 0
0
1
−
=
:
Obtain
To
N.[P(t) – PE] = 0;
D
N.
−
8.
To ensure validvalue of t, denominator must
To ensure valid value of t, denominator must
be non-zero.
A i th t D N <> 0 h k if
Assuming, that D, N <> 0, check if:
N.D <> 0. i.e. edge and line are not parallel.
If they are parallel ?
U th b i f t t bt i ll
Use the above expression of t to obtain all
the four intersections:
• Select a point on each of the four edges of
the clip rectangle.
Fi d lid i t ti
• Obtain four values of t.
• Find valid intersections
How to implement the last step ?
How to implement the last step ?
Steps:
Steps:
• If anyvalue of t is outside the range [0 – 1]
j t it
• Else, sort with increasing values of t.
reject it.
This solves L1, but not lines L2 and L3.
Criteria to choose intersection points,
PE or PL:
PE or PL:
Move from point P0 to P1;
If you are entering edge’s inside half-plane,
th th t i t ti i t i k d PE
p 0 1;
then that intersection point is marked PE;
l if l i it i k d PL
else, if you are leaving it is marked as PL.
11.
Check the angleof D and N vectors, for
g ,
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,
t t (i) t
store tL(i) = t
Find the ma im m al e of t and
Find the maximum value of tE, and
minimum value of tL for a line.
If tE < tL choose pair of parameters as
valid intersections on the line Else NULL
valid intersections on the line. Else NULL.
Calculations for parametricline Clipping
Clip
Edge
Normal
N
PE
§
P0 - PE
D
N
P
P
N
t E
]
.[ 0
−
=
Edge N
E 0 E
Left:
( 1 0) (X Y)
(X0 – Xmin,
D
N.
−
)
X
(X min
0
−
−
X = Xmin
(-1, 0) (Xmin,Y)
( 0 min,
Y0 – Y)
Ri ht (X X
)
X
(X
)
(
0
1
min
0
−
)
X
(X
Right:
X = Xmax
(1, 0) (Xmax,Y)
(X0 – Xmax,
Y0 – Y) )
X
(X
)
X
(X
0
1
max
0
−
−
−
max 0 )
Bottom:
(0, -1) (X,Ymin)
(X0 – X,
0
1
)
Y
(Y
)
Y
(Y min
0
−
−
Y = Ymin
(0, 1) (X,Ymin)
Y0 – Ymin)
Top: (X0 – X,
)
Y
(Y 0
1
−
)
Y
(Y max
0
−
Top:
Y = Ymax
(0, 1) (X,Ymax)
(X0 X,
Y0 – Ymax) )
Y
(Y
)
(
0
1
max
0
−
−
§ - Exact coordinates for PE is irrelevant.
Bit
1 0
Number
1 0
FIRST
Above
Bl T d
FIRST
(MSB)
Above
Top edge
Y > Y
Below Top edge
Y < Ymax
Y > Ymax
Below
Above Bottom edge
SECOND Bottom edge
Y < Ymin
Above Bottom edge
Y > Ymin
THIRD
Right of
Right edge
Left of Right edge
THIRD Right edge
X > Xmax
X < Xmax
L ft f
FOURTH
(LSB)
Left of
Left edge
Right of Left edge
X > X
(LSB)
X < Xmin
X > Xmin
17.
First Step: Determinethe bit values of the two
p
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 th d i t d [0000] th li li
• If both endpoint codes are [0000], the line lies
completely inside the box, no need to clip. This is
th i l t ( L )
the simplest case (e.g. L1).
• Any line has 1 in the same bit positions of both
• Any line has 1 in the same bit positions of both
the endpoints, it is guaranteed to lie outside the
box completely (e g L and L )
box completely (e.g. L2 and L3 ).
18.
1001 1000 1010
10011000 1010
0001 0010
0000
ymax
0001 0010
0101 0100 0110
0000
ymin
0101 0100 0110
x
x xmax
xmin
L4
L2
• Neither completely
L1 L5
reject nor inside the box:
Lines: L4 and L5, - needs
L
L
Lines: L4 and L5, needs
more processing.
L3
L6
• What about Line L6 ?
19.
Processing of lines,neither Completely
i d
IN or OUT; e.g. Lines: L4, L5 and L6.
B i id
Basic idea:
Clip parts of the line in any order (consider
from top or bottom)
from top or bottom).
Algorithm Steps:
Algorithm Steps:
• Compute outcodes of both endpoints to
check for trivial acceptance or rejection
check for trivial acceptance or rejection
(AND logic).
• If not so, obtain an endpoint that lies outside
the box (at least one will ?).
the box (at least one will ?).
• Using the outcode, obtain the edge that is
Using the outcode, obtain the edge that is
crossed first.
20.
1001 1000 1010
10011000 1010
0001 0010
0000
ymax
0001 0010
0101 0100 0110
0000
ymin
0101 0100 0110
x
x L4
C
D
I
xmax
xmin
L4
A
B H
I
L5
A
B
G
H
L5
F
M
N
L6 E
K
E
J
21.
Coordinates for intersection,for clipping
w.r.t edge:
Inputs: Endpoint coordinates:
(X0, Y0) and (X1, Y1)
OUTPUT
OUTPUT:
Edge for clipping (obtained using
t d f t d i t)
outcode of current endpoint).
22.
Obtain corresponding
Obtain corresponding
intersectionpoints
intersection points
CLIP (replace the endpoint by the
• CLIP (replace the endpoint by the
intersection point) w.r.t. the edge.
• Compute the outcode for the updated
endpoint and repeat the iteration till
endpoint and repeat the iteration, till
it is 0000.
• Repeat the above steps, if the other
endpoint is also outside the area
endpoint is also outside the area.
23.
e.g. Take LineL5 (endpoints - E and I):
5
E has outcode 0100 (to be clipped w.r.t. bottom
edge);
So EI is clipped to FI;
Outcode of F is 0000;
But outcode of I is 1010;
Cli ( t t d )
L4
C
D
I
Clip (w.r.t. top edge)
to get FH.
O t d f H i 0010
A
B
G
H
Outcode of H is 0010;
Clip (w.r.t. right edge)
to get FG;
L5
G
N
to get FG;
Since outcode of G
F
K
M
N
Since outcode of G
is 0000, display the
final result as FG
L6 E
J
K
final result as FG. J
24.
Formulas for clippingw.r.t. edge, in cases of:
pp g g ,
)
Y
(Y
*
)
X
(X
X
X 0
max
−
−
+
=
T Ed
)
Y
(Y
)
X
(X
X
X
0
1
0
1
0
−
−
+
=
Top Edge :
)
Y
(Y
)
Y
(Y
*
)
X
(X
X
X 0
min
0
1
0
−
−
+
=
Bottom Edge:
)
X
(X
)
Y
(Y 0
1
−
Right Edge:
)
X
(X
)
X
(X
*
)
Y
(Y
Y
Y
0
1
0
max
0
1
0
−
−
−
+
=
Right Edge:
0
1
)
X
(X
*
)
Y
(Y
Y
Y 0
min −
−
+
=
Left edge:
)
X
(X
)
Y
(Y
Y
Y
0
1
0
1
0
−
−
+
=
Let’s compare with Cyrus-Beck formulation
25.
Calculations for parametricline Clipping
Clip
Edge
Normal
N
PE
§
P0 - PE
D
N
P
P
N
t E
]
.[ 0
−
=
Edge N
E 0 E
Left:
( 1 0) (X Y)
(X0 – Xmin,
D
N.
−
)
X
(X min
0
−
−
X = Xmin
(-1, 0) (Xmin,Y)
( 0 min,
Y0 – Y)
Ri ht (X X
)
X
(X
)
(
0
1
min
0
−
)
X
(X
Right:
X = Xmax
(1, 0) (Xmax,Y)
(X0 – Xmax,
Y0 – Y) )
X
(X
)
X
(X
0
1
max
0
−
−
−
max 0 )
Bottom:
(0, -1) (X,Ymin)
(X0 – X,
0
1
)
Y
(Y
)
Y
(Y min
0
−
−
Y = Ymin
(0, 1) (X,Ymin)
Y0 – Ymin)
Top: (X0 – X,
)
Y
(Y 0
1
−
)
Y
(Y max
0
−
Top:
Y = Ymax
(0, 1) (X,Ymax)
(X0 X,
Y0 – Ymax) )
Y
(Y
)
(
0
1
max
0
−
−
§ - Exact coordinates for PE is irrelevant.
Consider parametric equationof a line segment:
Consider parametric equation of a line segment:
1
0
; ≤
≤
Δ
+
=
Δ
+
= u
Y
u
Y
Y
X
u
X
X .
1
0
,
; 1
1 ≤
≤
Δ
+
=
Δ
+
= u
Y
u
Y
Y
X
u
X
X
where
where,
1
2
1
2 ; Y
Y
Y
X
X
X −
=
Δ
−
=
Δ
A point is considered to be within a
rectangle, iff
min 1 max ;
XW X u X XW
≤ + Δ ≤
min 1 max .
YW Y u Y YW
≤ + Δ ≤
28.
E h fth f i liti b
Each of these four inequalities, can be
expressed as:
4
3
2
1
; =
= k
q
p
u 4
,
3
,
2
,
1
;
. =
= k
q
p
u k
k
where, the parameters are defined as:
min
1
1
1 , XW
X
q
X
p −
=
Δ
−
=
1
max
2
2 ,
YW
Y
Y
X
XW
q
X
p
Δ
−
=
Δ
=
min
1
3
3 ,
Y
YW
Y
YW
Y
q
Y
p
Δ
−
=
Δ
−
=
1
max
4
4 , Y
YW
q
Y
p −
=
Δ
=
29.
Based on thesefour inequalities we can
Based on these four inequalities, we can
find the following conditions of line clipping:
• If pk = 0, the line is parallel
to the corresponding clipping
K = 1 Left
K = 2 Right
to the corresponding clipping
boundary:
K = 2 Right
K = 3 Bottom
K = 4 Top
K = 4 Top
• If for any k, for which pk = 0:
k
- qk < 0, the line is completely outside
the boundary
- qk > 0, the line is inside the parallel
clipping boundary.
30.
If 0 thli d f th t id
• If pk < 0, the line proceeds from the outside
to the inside of the particular clipping boundary
( i li i fi it t i i b th)
(visualize infinite extensions in both).
• If pk > 0, the line proceeds from the inside to
the outside of the particular clipping boundary
the outside of the particular clipping boundary
(visualize infinite extensions in both).
In both these cases, the intersection
i l l d
parameter is calculated as:
/ k
k p
q
u /
=
31.
The Algorithm:
• Initializeline intersection parameters to:
u1 = 0; u2 = 1;
u1 0; u2 1;
• Obtain pi, qi; for i = 1, 2, 3, 4.
Obta pi, qi; o , , 3,
• Using pi, qi - find if the line can be rejected or
g pi, qi j
the intersection parameters must be adjusted.
• If pk < 0, update u1 as:
4
1
)],
/
(
,
0
max[ −
=
k
p
q k
k
• If pk > 0, update u2 as:
)],
(
,
[ p
q k
k
Aft d t if j t th li
4
1
)],
/
(
,
1
min[ −
=
k
p
q k
k
• After update, if u1 > u2 : reject the line.
32.
L4
4
1
)],
/
(
,
0
max[
:
0 1 −
=
=
<k
p
q
u
p k
k
k
L
4
1
)],
/
(
,
1
min[
:
0 2 −
=
=
> k
p
q
u
p k
k
k
L1
L2
L5
K = 1 Left; K = 2 Right
K = 3 Bottom; K = 4 Top L1 L5
; p
min
1
1
1 , XW
X
q
X
p −
=
Δ
−
=
L3
L6
1
max
2
2 ,
YW
Y
Y
X
XW
q
X
p
Δ
−
=
Δ
=
min
1
3
3 ,
Y
YW
q
Y
p
YW
Y
q
Y
p
−
=
Δ
=
−
=
Δ
−
=
1
max
4
4 , Y
YW
q
Y
p −
=
Δ
=
L1: (0, 1); /*Analyze the line in both directions.
L [ (0 d d ) i (1 d d (<1))]
L2: [max(0, -d2, -d3) min(1, -d1, d4(<1))]
= (0, -d1) – hence reject.
L : [max(0 -d -d ) min(1 d d )]
Do for
L3, L5 & L6.
L4: [max(0, -d2, -d3) min(1, d1, d4)]
= (0, d4) (why ?) – so accept and clip
33.
What about Circle/Ellipseclipping
/ p pp g
or for curves ??
INPUT OUTPUT
Can you do a inside-outside test,
f h bj l
for the object vs. rectangle ?
Methodology: CHANGE positionof
ti f h d b li li i
vertices for each edge by line clipping
M h t dd ti t th li t
May have to add new vertices to the list.
39.
Processing of Polygon
INOUT
vertices against boundary
IN OUT
P
S and P
both OUT
Output: Null.
Output: Null.
S (No output)
Polygon Clip
boundary
Polygon
being
li d
y
clipped
40.
Processing of Polygon
verticesagainst boundary
IN OUT S
S OUT;
P IN;
Output: i and P
P: second
P: second
output
i: first
output
output
41.
Processing of Polygon
verticesagainst boundary
S d P b th IN
IN OUT
S and P both IN
Output: P
S
Output: P.
Polygon
b i
being
clipped
P: Output
Clip
boundary
P: Output boundary
Now output isas above Desired Output
Now output is as above Desired Output
Any Idea ??
Any Idea ??
– the modified
Weiler-Atherton algorithm
Weiler Atherton algorithm
46.
Solution for multiple
p
components
Forsay, clockwise processing of polygons,
follow:
• For OUT -> IN pair, follow the polygon
b d
boundary
• For IN -> OUT pair, follow Window boundary
i l k i di ti
in clockwise direction
47.
For say, clockwiseprocessing of polygons,
follow:
• For OUT -> IN pair, follow the polygon
boundary
• For IN -> OUT pair, follow Window boundary
in clockwise direction