This line clipping algorithm was developed by Dan Cohen and Ivan Sutherland:

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

2) Read corners of window (Wx1, Wy1) and (Wx2, Wy2).

3) Assign region codes for P1 and P2. A region code is a 4 digit bit code which indicates one of nine regions having the end point of line.

Initialize code with bits 0000

Set Bit 1 if (x<Wx1)

Set Bit 2 if (x>Wx2)

Set Bit 3 if (y<Wy2)

Set Bit 4 if (y>Wy1)

4) Check visibility of line P1P2

a) If region codes for both end points P1 and P2 are 0 = > Line is completely visible. Draw Line and Go to Step 9.

b) If region codes for both end points P1 and P2 are not 0 and logical ANDing of them is also not zero = > Line is completely invisible. Reject line and Go to Step 9.

c) If a) and b) both are not satisfied, line is partially visible.

5) Find intersecting edge of clipping window by inspecting region codes of two endpoints.

a) If region codes for both end points are non zero find intersection points P11 and P21 with boundary edges of clipping window with respect to point P1 and P2 respectively.

b) If region coed for any one end point is non zero then find intersection point P11 or P21 with boundary edge of clipping window with respect to it.

6) Divide line segments considering intersection points.

7) Reject line segment is any one end point of it appears outside clipping window.

8) Draw remaining line segments.

9) Stop.

## Leave a Reply