function [  ] = CohenSutherland( x0,y0,x1,y1,xmin, ymin, xmax, ymax )

    INSIDE = uint8(0);  
    LEFT = uint8(1);    
    RIGHT = uint8(2);   
    BOTTOM = uint8(4);  
    TOP = uint8(8);    
    
function code = ComputeOutCode(x,y)

        code = INSIDE;  

        if (x < xmin)      
            code = bitor(code, LEFT);
        elseif (x > xmax)  
            code = bitor(code, RIGHT);
        end
        if (y < ymin)      
            code = bitor(code, BOTTOM);
        elseif (y > ymax)  
            code = bitor(code, TOP);
        end
end

        outcode0 = ComputeOutCode(x0, y0);
        outcode1 = ComputeOutCode(x1, y1);
        accept = false;

        while true
            if ~bitor(outcode0, outcode1)  
                accept = true;
                break;
            elseif bitand(outcode0, outcode1) 
                break;
            else
                if outcode0
                    outcodeOut = outcode0;
                else
                    outcodeOut = outcode1;
                end

                if bitand(outcodeOut, TOP)  
                    x = x0 + (x1 - x0) * (ymax - y0) / (y1 - y0);
                    y = ymax;
                elseif bitand(outcodeOut, BOTTOM)  
                    x = x0 + (x1 - x0) * (ymin - y0) / (y1 - y0);
                    y = ymin;
                elseif bitand(outcodeOut, RIGHT)  
                    y = y0 + (y1 - y0) * (xmax - x0) / (x1 - x0);
                    x = xmax;
                elseif bitand(outcodeOut, LEFT)  
                    y = y0 + (y1 - y0) * (xmin - x0) / (x1 - x0);
                    x = xmin;
                end
                
                if outcodeOut == outcode0
                    x0 = x;
                    y0 = y;
                    outcode0 = ComputeOutCode(x0, y0);
                else
                    x1 = x;
                    y1 = y;
                    outcode1 = ComputeOutCode(x1, y1);
                end
            end
        end

        DrawLine(round(x0),round(y0),round(x1),round(y1),34);
end
