Tuesday, October 27, 2020

Cohen Line clipping algorithm

 



// C++ program to implement Cohen Sutherland algorithm

// for line clipping.

#include <iostream>

#include <GL/glut.h>

using namespace std;



// Defining region codes

const int INSIDE = 0; // 0000

const int LEFT = 1; // 0001

const int RIGHT = 2; // 0010

const int BOTTOM = 4; // 0100

const int TOP = 8; // 1000


// Defining x_max, y_max and x_min, y_min for

// clipping rectangle. Since diagonal points are

// enough to define a rectangle

const int x_max = 100;

const int y_max = 100;

const int x_min = 40;

const int y_min = 40;

int x1,x2,y1,y2;

// Function to compute region code for a point(x, y)

int computeCode(double x, double y)

{

// initialized as being inside

int code = INSIDE;


if (x < x_min) // to the left of rectangle

code |= LEFT;

else if (x > x_max) // to the right of rectangle

code |= RIGHT;

if (y < y_min) // below the rectangle

code |= BOTTOM;

else if (y > y_max) // above the rectangle

code |= TOP;


return code;

}


// Implementing Cohen-Sutherland algorithm

// Clipping a line from P1 = (x2, y2) to P2 = (x2, y2)

void cohenSutherlandClip(double x1, double y1,double x2, double y2)

{

// Compute region codes for P1, P2

int code1 = computeCode(x1, y1);

int code2 = computeCode(x2, y2);


// Initialize line as outside the rectangular window

bool accept = false;


while (true)

{

if ((code1 == 0) && (code2 == 0))

{

// If both endpoints lie within rectangle

accept = true;

break;

}

else if (code1 & code2)

{

// If both endpoints are outside rectangle,

// in same region

break;

}


}

if (accept)

{

cout <<"Line accepted from (" << x1 << ", "

<< y1 << " to "<< x2 << ", " << y2 << ") because its completely VISIBLE" << endl;

// Here the user can add code to display the rectangle

// along with the accepted (portion of) lines

}

else

        cout <<"Line rejected from (" << x1 << ", "

<< y1 << " to "<< x2 << ", " << y2 << ") because its completely INVISIBLE" << endl;

}


 void display()

 {

        glClear(GL_COLOR_BUFFER_BIT);

        glColor3f(0.0,1.0,0.0);

        glBegin(GL_LINES);

        glVertex2i(40,100);

        glVertex2i(40,10);

        glVertex2i(100,100);

        glVertex2i(100,10);


        glVertex2i(40,100);

        glVertex2i(100,100);

        glVertex2i(40,10);

        glVertex2i(100,10);


        glColor3f(1.0,0.0,0.0);

        //Visible line

        glVertex2i(50,60);

        glVertex2i(70,40);

        //Invisible line

        glVertex2i(150,100);

        glVertex2i(180,50);

        glEnd();

        glFlush();


 }

 void myInit(void)

        {


            glClearColor(0.0,0.0,0.0,1.0);


            glPointSize(2.0);

            glMatrixMode(GL_PROJECTION);


            glLoadIdentity();

            gluOrtho2D(0,200,0,200);


        }



// Driver code

int main(int argc, char**argv)

{

    cout<<" The coordinates of the clipping window are (40,100), (100,100), (100,10) and (40,40)"<<endl;

    // First Line segment

// P11 = (5, 5), P12 = (7, 7) completely VISIBLE(inside)

cohenSutherlandClip(50, 60, 70, 40);


// P11 = (5, 5), P12 = (7, 7) completely INVISIBLE(outside)

cohenSutherlandClip(150, 100, 180, 150);




    glutInit(&argc, argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);


// giving window size in X- and Y- direction

glutInitWindowSize(400,400);

glutInitWindowPosition(0,0);


// Giving name to window

    glutCreateWindow("Cohen-Sutherland line clipping algorithm");

    myInit();


glutDisplayFunc(display);


glutMainLoop();


return 0;

}


output:



No comments:

Post a Comment

Derivatives stock list at NSE

Complete FNO stock list at NSE. ABB India Ltd ACC Ltd APL Apollo Tubes Ltd AU Small Finance Bank Ltd Aarti Industries Ltd Abbott India Ltd A...