Cohen-Sutherland Line Clippings

, ,

Cohen-Sutherland Line Clippings algorithm uses the clipping window as shown in the following figure. The minimum coordinate for the clipping region is (XWmin,YWmin)(XWmin,YWmin) and the maximum coordinate for the clipping region is (XWmax,YWmax)(XWmax,YWmax).

Cohen-Sutherland Line Clippings akhtarbari.com


We will use 4-bits to divide the entire region. These 4 bits represent the Top, Bottom, Right, and Left of the region as shown in the following figure. Here, the TOP and LEFT bit is set to 1 because it is the TOP-LEFT corner.

Algorithm

Step 1 − Assign a region code for each endpoints.

Step 2 − If both endpoints have a region code 0000 then accept this line.

Step 3 − Else, perform the logical ANDoperation for both region codes.

Step 3.1 − If the result is not 0000, then reject the line.

Step 3.2 − Else you need clipping.

Step 3.2.1 − Choose an endpoint of the line that is outside the window.

Step 3.2.2 − Find the intersection point at the window boundary (base on region code).

Step 3.2.3 − Replace endpoint with the intersection point and update the region code.

Step 3.2.4 − Repeat step 2 until we find a clipped line either trivially accepted or trivially rejected.

Step 4 − Repeat step 1 for other lines.

 




DDA LINE ALGORITHM

, ,

A line connects two points. It is a basic element in graphics. To draw a line, you need two points between which you can draw a line. In the following DDA line algorithm, we refer the one point of line as X,YX,Y and the second point of line as X1,Y1X1,Y1.

Digital Differential Analyzer algorithm (DDA Algorithm)  is the simple line generation algorithm which is explained step by step here.

Step 1 − Get the input of two end points (X,Y)(X,Y) and (X1,Y1)(X1,Y1).

Step 2 − Calculate the difference between two end points.

dx = X1 – X

dy = Y1 – Y

Step 3 − Based on the calculated difference in step-2, you need to identify the number of steps to put pixel. If dx > dy, then you need more steps in x coordinate; otherwise in y coordinate.

if (absolute(dx) > absolute(dy))

Steps = absolute(dx);

else

Steps = absolute(dy);

Step 4 − Calculate the increment in x coordinate and y coordinate.

Xincrement = dx / (float) steps;

Yincrement = dy / (float) steps;

Step 5 − Put the pixel by successfully incrementing x and y coordinates accordingly and complete the drawing of the line.

for(int v=0; v < Steps; v++)

{

x = x + Xincrement;

y = y + Yincrement;

putpixel(Round(x), Round(y));

}