Professional Documents
Culture Documents
OUTPUT
INPUT
Kx X0 t lx = X1 X0
t ly =
K y Y0 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 and Solve:
P(t ) = P0 + t ( P1 P0 )
' ' '
t 1 ( P1 P0 ) t 2 ( P1 P0 ) = P0 P0
CYRUS-BECK formulation
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
To ensure valid value of t, denominator must be non-zero. Assuming, A i th that t 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 of f 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 i intersections t ti How to implement the last step ?
PE PL P0 P0 PE P0
P1
L2 L1 PE L3 PE PL PL PL
P1
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 that t 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 PL
L3 L1 PE L2 PE PL
P1 PL PL
P1
P0
P0
PE
PE
N.[P P ] 0 E t= N.D
( (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 )
Region Outcodes:
ymax ma ymin
xmin
xmax
1 Above Top edge Y > Ymax Below Bottom edge Y < Ymin Right of Right edge X > Xmax L ft of Left f 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
THIRD
FOURTH (LSB)
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 ). )
ymax ymin
L4
xmin
xmax
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.
ymax ymin
D C L4 B N M L6 K J E F L5 A G I H
xmin
xmax
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 of f current t endpoint). d i t)
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 of Outcode f H is 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:
Right Edge:
Left edge:
PE
N.[P P ] 0 E t= N.D
( (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 )
X = X 1 + uX ; Y = Y1 + uY , 0 u 1.
where, where
X = X 2 X 1 ; Y = Y2 Y1
XWmin X 1 + uX YWmin Y1 + u Y
XWmax ; YWmax .
u. pk = qk ; k = 1,2,3,4
p1 = X , p2 = X , p3 = Y , p4 = Y ,
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 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. 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: If pk > 0, update u2 as:
L4
p1 = X , p2 = X , p3 = Y , p4 = Y ,
Do for L3, L5 & L6.
L1
L5 L3
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
INPUT
OUTPUT
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 t to add dd new vertices ti to t the th list. li t
IN
OUT
Clip y boundary
IN
OUT
S
S OUT; P IN; Output: i and P
P: second output
i: first output
OUT
S and dPb both th IN Output: P P.
P: Output
Clip boundary
IN
OUT
S IN; P OUT Output: i
P S i output
INPUT OUTPUT
Desired Output
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