06-07-2012, 03:51 PM
COHEN-SUTHERLAND ALGORITHM
To clip a line using Cohen-Sutherland clipping algorithm.
Algorithm:
The method speeds up the processing of line segments by performing initial tests that reduce the number of intersections that must be calculated.
1. Every line endpoint is assigned a four digit binary code, called region code, that identifies the location of the point relative to the boundaries of the clipping rectangle.
2. Each bit position in the region code is used to indicate one of the four relative coordinate positions of the point with respect to the clip window.
Bit 1: left
Bit 2: right
Bit 3: below
Bit 4: above
3. Bit values in the region code are determined by comparing endpoint coordinates values (x, y) with respect to the clip boundaries. eg.Bit 1 is set to 1 if x<xwmin
4. Once we have established region codes for all line endpoints, we can quickly determine which lines are completely outside or inside the clip window.
5. Lines that cannot be identified as completely inside or outside a clip window are checked for intersection with boundaries.
6. Intersection points with a clipping boundary can be calculated using the slope-intercept form of the line equation.
7. The y coordinate of the intersection point at vertical line
8. The x coordinate of the intersection point at horizontal line