1) Read two end points of line P1 (x1, y1) and P2 (x2, y2)

2) Read two corner vertices, left top and right bottom of window: (XwMIN, YwMAX) and (XwMAX, YwMIN)

3) Calculate values of parameters pi and qi for I =1, 2, 3, 4 such that

p1 = -Δx

q1 = x1 – XwMIN

p2 = Δx

q2 = XwMAX – xi

q1 = – Δy

q3 = y1 – YwMIN

q2 = Δy

q4 = YwMAX – y1

4) If pi = 0 then,

{

Line is parallel to ith boundary.

If qi < 0 then,

{

Line is completely outside the boundary. Therefore, discard line segment and Go to Step 10.

}

else

Check line is horizontal or vertical and accordingly check line end points with corresponding boundaries. If line endpoints lie within the bounded area then use them to draw line. Otherwise use boundary coordinates to draw line. Go to Step 10.

}

}

5) Initialize t1 = 0 and t2 = t1

6) Calculate values for qi/pi for i= 1,2,3,4

7) Select values of qi/pi where pi<0 and assign maximum out of them as t1.

8) Select values of qi/pi where pi>0 and assign maximum out of them as t2.

9) If (t1 < t2)

{

Calculate endpoints of clipped line:

xx1 = x1 + t1 Δx

xx2 = x1 + t2 Δx

yy1 = y1 + t1 Δy

yy2 = y1 + t2 Δy

Draw line (xx1, yy1, xx2, yy2)

}

10) Stop.

Pros:

1) Less intersection calculations.

2) More efficient.

3) Only requires one division to update t1 and t2.

4) Window intersections of line are calculated just once.

## Leave a Reply