Cohen-Sutherland Line Clipping Algorithm

1. Assign 4-bit REGION Code [TBRL] for end-points of line.
  • Bit 1 is set to 0 if XWmin≤x, else set to 1. (L)
  • Bit 2 is set to 0 if x≤XWmax, else set to 1. (R)
  • Bit 3 is set to 0 if YWmin≤y, else set to 1. (B)
  • Bit 4 is set to 0 if y≤YWmax, else set to 1. (T)
(Condition: XWmin≤x≤XWmax, YWmin≤y≤YWmax)

2. Determine whether the line is completely inside or outside of the window using tests.
a) If end-point has region code 0000, the point is completely inside.
b) If logical AND of end-points is not 0000, the line is completely outside.
c) If logical AND of end-points is 0000, the line is not completely outside.

3. If 2(c) exists, we need to find the intersection with window boundary.
Here, 

i) If bit 1 is 1, line intersects with left boundary.
So,
where x=XWmin.

ii) If bit 2 is 1, line intersects with right boundary.
So,
where x=XWmax.

iii) If bit 3 is 1, line intersects with bottom boundary.
So,
where y=YWmin.

iv) If bit 4 is 1, line intersects with top boundary.
So,
where y=YWmax.
Here, xi & yi are x & y intercepts for that line. Update that.

4. Repeat (1) to (3) for every points till completely accepted.

No comments:

Post a Comment