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 3.

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 3.

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

5) Divide partially visible line segments to equal parts and repeat steps 3 through 5 for both sub divided line segments until you get completely visible and completely invisible line segments.

6) Stop.

Admin says

I’m glad you find it useful. Thank you for stopping by.

Srijita Majumdar says

A slight rectification of the algorithm- 4. a)If region codes for both end points P1 and P2 are 0 = > Line is completely visible. Draw Line and Go to Step 6 (not step 3).

Srijita Majumdar says

And 4. b) 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 6 (not step 3 again).