# 5. COMPUTER GRAPHICS LAB | READ NOW

VTU COMPUTER GRAPHICS LAB

Program 5:- Clip a lines using Cohen-Sutherland algorithm.

## STEPS TO RUN CG PROGRAM

1. Copy the below copy
2. Past it in any code compiler ex- Code Blocks, DEV C++, VS Code
3. Save the file with .cpp extension
4. Compile and Run the code
5. Program Execution Successful

Note:- if you use Dev C++, right click on project>project options>parameter>linker paste the below code in Linker – as shown in image.

```-lopengl32
-lfreeglut
-lglu32```

## Program Code [lab5.cpp]

```#include<stdio.h>
#include<GL/glut.h>
#define outcode int

double xmin=50,ymin=50,xmax=100,ymax=100;
double xvmin=200,yvmin=200,xvmax=300,yvmax=300;
double x0,y0,x1,y1;
const int RIGHT=8;
const int LEFT=2;
const int TOP=4;
const int BOTTOM=1;
outcode ComputeOutCode(double x, double y);

void CohenSutherland(double x0, double y0, double x1, double y1)
{
outcode outcode0, outcode1, outcodeOut;
bool accept=false, done=false;
outcode0=ComputeOutCode(x0,y0);
outcode1=ComputeOutCode(x1,y1);
do
{
if(!(outcode0 | outcode1))
{
accept=true;
done=true;
}
else if(outcode0 & outcode1)
done=true;
else
{
double x, y;
outcodeOut=outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
{
x=x0+(x1-x0)*(ymax-y0)/(y1-y0);
y=ymax;
}
else if(outcodeOut & BOTTOM)
{
x=x0+(x1-x0)*(ymin-y0)/(y1-y0);
y=ymin;
}
else if(outcodeOut & RIGHT)
{
y=y0+(y1-y0)*(xmax-x0)/(x1-x0);
x=xmax;
}
else
{
y=y0+(y1-y0)*(xmin-x0)/(x1-x0);
x=xmin;
}
if(outcodeOut==outcode0)
{
x0=x;
y0=y;
outcode0=ComputeOutCode(x0,y0);
}
else
{
x1=x;
y1=y;
outcode1=ComputeOutCode(x1,y1);
}
}
}while(!done);

if(accept)
{
double sx=(xvmax-xvmin)/(xmax-xmin);
double sy=(yvmax-yvmin)/(ymax-ymin);
double vx0=xvmin+(x0-xmin)*sx;
double vy0=yvmin+(y0-ymin)*sy;
double vx1=xvmin+(x1-xmin)*sx;
double vy1=yvmin+(y1-ymin)*sy;
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xvmin, yvmin);
glVertex2f(xvmax, yvmin);
glVertex2f(xvmax, yvmax);
glVertex2f(xvmin, yvmax);
glEnd();
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2d(vx0,vy0);
glVertex2d(vx1,vy1);
glEnd();
}
}

outcode ComputeOutCode(double x, double y)
{
outcode code=0;
if(y > ymax)
code = TOP;
else if(y < ymin)
code = BOTTOM;
if(x > xmax)
code = RIGHT;
else if(x < xmin)
code = LEFT;
return code;
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2d(x0,y0);
glVertex2d(x1,y1);
glEnd();
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(xmin, ymin);
glVertex2f(xmax, ymin);
glVertex2f(xmax, ymax);
glVertex2f(xmin, ymax);
glEnd();
CohenSutherland(x0,y0,x1,y1);
glFlush();
}

void myinit()
{
glClearColor(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,499.0,0.0,499.0);
}

void main(int argc, char** argv)
{
printf("Enter the end points of the line: ");
scanf("%lf%lf%lf%lf", &x0,&y0,&x1,&y1);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Cohen-Sutherland Line Clipping");
glutDisplayFunc(display);
myinit();
glutMainLoop();
}```

## Alternative Method [lab5.cpp]

```#include<stdio.h>
#include<GL/glut.h>

#define true 1;
#define false 0;
#define bool int;
double x,y;
int xmin=50,xmax=100,ymin=50,ymax=100;
const int RIGHT=8,LEFT=2,TOP=4,BOTTOM=1;
int outcode0,outcode1,outcodeout,done,accept;

int computeoutcode(double x,double y)
{
int code=0;
if(y>ymax)
code|=TOP;
else if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else if(x<xmin)
code|=LEFT;
return code;
}

void LineClip(double x0,double y0,double x1,double y1)
{
int accept=false;
int done=false;
outcode0=computeoutcode(x0,y0);
outcode1=computeoutcode(x1,y1);
do{
if(!(outcode0|outcode1))
{
accept=true;
done=true;
}
else if(outcode0&outcode1)
{
done=true;
}
else
{
outcodeout=outcode0?outcode0:outcode1;
if(outcodeout & TOP)
{
x=x0+(x1-x0)*(ymax-y0)/(y1-y0);
y=ymax;
}
else if(outcodeout & BOTTOM)
{
x=x0+(x1-x0)*(ymin-y0)/(y1-y0);
y=ymin;
}
else if(outcodeout & RIGHT)
{
y=y0+(y1-y0)*(xmax-x0)/(x1-x0);
x=xmax;
}
else
{
y=y0+(y1-y0)*(xmin-x0)/(x1-x0);
x=xmin;
}
if(outcodeout==outcode0)
{
x0=x;y0=y;outcode0=computeoutcode(x0,y0);
}
else
{
x1=x;y1=y;outcode1=computeoutcode(x1,y1);
}
}
}while(!done);
if(accept)
{
glPushMatrix();
glTranslatef(100,100,0);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2i(50,50);
glVertex2i(100,50);
glVertex2i(100,100);
glVertex2i(50,100);
glEnd();
glColor3f(1.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2i(x0,y0);
glVertex2i(x1,y1);
glEnd();
glPopMatrix();
glFlush();
}
}

void display()
{
glClearColor(1,1,1,1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
glVertex2i(50,50);
glVertex2i(100,50);
glVertex2i(100,100);
glVertex2i(50,100);
glEnd();
glColor3f(1.0,0.0,1.0);
glBegin(GL_LINES);
glVertex2i(60,20);
glVertex2i(80,120);
glVertex2i(80,20);
glVertex2i(60,120);
glEnd();
LineClip(60,20,80,120);
LineClip(80,20,60,120);
glFlush();
}

void init()
{
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0,300,0,300);
glMatrixMode(GL_MODELVIEW);
}

int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(500,500);
glutCreateWindow("Cohen Sutherland line and drawing algorithm");
init();
glutDisplayFunc(display);
glutMainLoop();
}```