2. Cohen-Sutherland line clipping Algorithm:-
Step 1: Read two end points of the line say p1(x1,y1) and p2(x2,y2).
P1(x1,y1) P2(x2,y2)
Step 2: Read two corner (left-bottom and right-top) of the window, say
(Xwmin,YWmin) and (Xwmax,YWmax).
Step 3: Assign the region code for two end points p1 and p2 are follows:
Region code calculation for point (x,y):-
Sign bit for Bit1 is x-xwmin, sign bit for Bit2 xwmax-x, sign bit for Bit3 for y-ywmin,
sign bit for Bit4 ywmax-y.
(Xwmax,Ywmax)
(Xwmin,Ywmin)
Bit4 Bit3 Bit2 Bit1
3. Then if sign bit is negative then the corresponding bit is assigned to be 1. If it is
positive then the corresponding bit is assigned to be 0.
Step 4: Check for visibility of P1P2:-
a)If the both region code for p1 and p2 is 0000. Then the line completely visible, then
exit.
b)Else, ANDing the region code for p1 and p2, if the result is not 0000. Then the line is
completely invisible. Then exit.
c)If the region code for two end points do not satisfy the above two condition, then the
line is partially visible.
Step 5: Determine the intersection point:-
a)If the line partially visible and region code for both p1 and p2 are not 0000.
Then find p1’ and p2’. The intersection point with the boundaries edge of the clipping
window.
4. p1 P1’ P2’ p2
b) If any one of the point p1 or p2 is 0000(region code) and other root 0000. Then
find the intersection point p1’ or p2’ with the boundary edge of the clipping
window.
p1 P2’ p2
Step 6: Reject the line outside of the clipping window and draw the remaining line
segment.
Step 7: Stop.
5. consider the clipping window and the lines shown in following figure and
find the region code for each end points and identify whither the line is
completely visible, partially visible or completely.
Line End points Region code ANDing Result
P1p2 0000 0000 0000 Completely
visible
P3p4 0001 0001 0001 Completely
invisible
P5p6 0001 0000 0000 Partially Visible
P7p8 0100 0010 0000 Partially visible
P9p10 1000 0010 0000 Partially visible
6. Problem :
Giving a clipping window p(25,25), Q(65,25), R(65,45) and S(25,45). Using
Cohen-Sutherland algorithm find the visible position of line segment joining
the points x(45,85), x’(125,35).
Xwmin=25
Xwmax=65
Ywmin=25
Ywmax=45
According to Cohen-Sutherland algorithm the region code for the point x(45,85). Here
x=45 and y=85.
Bit1=x-xwmin=45-25=20(+ve)=0
Bit2=xwmax-x=65-45=20(+ve)=0
Bit3=y-ywmin=85-25=60(+ve)=0
Bit4=ywmax-y=45-85=-60(-Ve)=1
7. Thus the region code of the point x=1000.
The region code for the point x’(125,35). Here x=125,y=35.
Bit1=x-xwmin=125-25=100(+ve)=0
Bit2=xwmax-x=65-125=-60(-ve)=1
Bit3=y-ywmin=35-25=10(+ve)=0
Bit4=ywmax-y=45-35=-10(+Ve)=0
Thus the region code of the point x’=0010.
AND operation between two region code:
For x=1000
For x’=0010
0000
Thus the line is partially visible.
8. Liang-Barsky Line Clipping Algorithm:-
The Liang–Barsky algorithm uses the parametric equation of a line and inequalities
describing the range of the clipping window to determine the intersections between
the line and the clipping window. With these intersections it knows which portion of
the line should be drawn. This algorithm is significantly more efficient than
Cohen–Sutherland. Consider first the usual parametric form of a straight line:
X=x0+u(x1-x0)=x0+uΔx,
Y=y0+u(y1-y0)=y0+uΔy, where 0<=u<=1.
A point is in the clip window, if Xwmin<=x0+uΔx<=xwmax and
Ywmin<=y0+uΔy<=Ywmax, which can be expressed as the 4 inequalities u.pk<qk,
k=1,2,3,4 where
P1=- Δx,
p2= Δx,
p3=- Δy,
p4= Δy,
q1=x0-xmin(left),
q2=xmax-x0(right),q3=y0-ymin(bottom),q4=ymax-y0(top).
9. • When pk < 0, as u increases
- line goes from outside to inside - entering
• When pk > 0,
- line goes from inside to outside - exiting
• When pk = 0,
- line is parallel to an edge
• If there is a segment of the line inside the clip region, a sequence of infinite line
intersections must go: entering, entering, exiting, exiting.
Algorithm:-
1. Set umin = 0 and umax = 1.
2. Calculate the u values:
3. If u < umin or u > umax ignore it.
Otherwise classify the u values as entering or exiting.
4. If umin < umax then draw a line from: ( x0 + Δx · umin, y0 + Δy · umin ) to
( x0 + Δx · umax, y0 + Δy · umax ).
10.
11. We have umin = 1/4 and umax = 3/4
Pend - P0 = (15+5,9-3) = (20,6)
If umin < umax , there is a line segment
- compute endpoints by substituting u values
Draw a line from (-5+(20)·(1/4), 3+(6)·(1/4)) to (-5+(20)·(3/4), 3+(6)·(3/4))
12. • We have umin = 4/5 and umax = 2/3
Pend - P0 = (2+8, 14-2) = (10, 12)
• umin > umax , there is no line segment do draw.
To compute the final line segment:
1. A line parallel to a clipping window edge has pk=0 for that boundary.
2. If for that k, qk<0, the line is completely outside and can be eliminated.
3. When pk<0 the line proceeds outside to inside the clip window and when pk>0, the
line proceeds inside to outside.
4. For nonzero pk, u=qk/pk gives the intersection point.
5. For each line, calculate u1 and u2. For u1, look at boundaries for which pk<0 (i.e.
outside to inside). Take to be the largest among . For u2, look at boundaries for
which pk>0 (i.e. inside to outside). Take u2 to be the minimum of {1,qk/pk}.
If u1>u2, the line is outside and therefore rejected.
13. Liang-Barsky vs Cohen-Sutherland:-
LB generally more efficient:
- LB: updates of u1 and u2 use one division, window intersections computed
only once with final u1 and u2.
- CS: may repeatedly calculate intersections along a line, even if line is
totally exterior to clip window and each intersection computation uses division
and multiplication.