Algoritmo de Recorte de Cohen - Sutherland
The Cohen-Sutherland Line-Clipping Algorithm
The more efficient Cohen-Sutherland Algorithm performs initial tests on a line to determine whether intersection calculations can be avoided.
Steps for Cohen-Sutherland algorithm
- End-points pairs are check for trivial acceptance or trivial rejected using the outcode.
- If not trivial-accepance or trivial-rejected, divided into two segments at a clip edge.
- Iteratively clipped by testing trivial-acceptance or trivial-rejected, and divided into two segments until completely inside or trivial-rejected.
Pseudo-code of Cohen-Sutherland Algorithm.
Trivial acceptance/reject test
To perform trivial accept and reject tests, we extend the edges of the clip rectangle to divide the plane of the clip rectangle into nine regions. Each region is assigned a 4-bit code deteermined by where the region lies with respect to the outside halfplanes of the clip-rectangle edges. Each bit in the outcode is set to either 1 (true) or 0 (false); the 4 bits in the code correspond to the following conditions:
- Bit 1 : outside halfplane of top edge, above top edge
Y > Ymax - Bit 2 : outside halfplane of bottom edge, below bottom edge
Y <> - Bit 3 : outside halfplane of right edge, to the right of right edge
X > Xmax - Bit 4 : outside halfplane of left edge, to the left of left edge
X <>
In summary, the C-S algorithm is efficient when outcode testing can be done cheaply (for example, by doing bitwise operations in assembly language) and trivial acceptance or rejection is applicable to the majority of line segments .(For example, large windows - everything is inside , or small windows - everything is outside).
http://www.cc.gatech.edu/grads/h/Hao-wei.Hsieh/Haowei.Hsieh/code1.html


#include
