Cohen-Sutherland clipping algorithm

Cohen-Sutherland clipping algorithm

    The first part of this algorithm serves simply for excluding abscissas at which it isn't necessary to calculate the conjunction with the display box. Firstly, we split a plane into 9 sections, while the middle section represents a display box. For every section is allocated a 4-bit code, and such code is allocated also to the point of abscissa which belongs into this section. Individual bits for a point [x, y] are set as follows:

1.    bit - a point lying left of the display box ( xmin > x )
2.    bit - a point lying right of the display box ( xmax < x )
3.    bit - a point lying lower than the display box ( ymin > y )
4.    bit - a point lying higher than the display box ( ymax < y )

$$$APPLET

Applet Codes of areas determined by the display box

    A point located inside the display box has the code 0000. The abscissa is lying wholly inside in the display box. We can exclude this abscissa if both its points are left, right, above or down from the display box. We can easily identify this just by the codes of the end points. We take both codes of the end points of an abscissa, and we make their logical conjunction ((1 and 1) = 1, otherwise 0). If it is different from 0000, then the abscissa doesn't intersect the display box, and we can exclude it. In the opposite case, we cannot exclude this abscissa, and we must test it for a conjunction in respect of the display box limits, 4 times in a worst case. 

$$$APPLET

Applet Clipping the given abscissa in respect of the display box, and the determination of conjunction points.