Read Me:
For run this code you must install GLUT in your system
Installation Guide : GLUT Install
Algorithm:Cohen Sutherland Line Clipping
Source code :
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include<windows.h>
#include<iostream>
#include <chrono>
#include <thread>
#include <bitset>
using namespace std;
int wx1,wx2,wy1,wy2,line_no;
struct points
{
int x_start;
int y_start;
int x_end;
int y_end;
int visible;
};
points line_arr[100];
void bit_generate(int a)
{
bitset<4>binary(a);
cout << binary << endl;
}
int visible_test(int x,int y)
{
int res=0;
if(x < wx1)
{
res+=1;
}
if(x > wx2)
{
res+=2;
}
if(y < wy1)
{
res+=4;
}
if(y > wy2)
{
res+=8;
}
return res;
}
/***
Tarequzzaman Khan
CSE'12
2012331514
*/
int Round(double x)
{
return (floor(x+0.5));
}
void dda(double x_start, double y_start, double x_end,double y_end)
{
double dx=(x_end-x_start);
double dy=(y_end-y_start);
double steps;
float xInc,yInc,x_=x_start,y_=y_start;
/* Find out whether to increment x or y */
steps= max(abs(dx),abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;
/* Plot the first point */
glBegin(GL_POINTS);
glVertex2d(x_,y_);
glEnd();
glFlush();
int k;
/* For every step, find an intermediate vertex */
for(k=0; k<steps; k++)
{
x_+=xInc;
y_+=yInc;
/* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
glBegin(GL_POINTS);
glVertex2d(Round(x_), Round(y_));
glEnd();
glFlush();
}
}
/***
Tarequzzaman Khan
CSE'12
2012331514
*/
void dda1(double x_start, double y_start, double x_end,double y_end)
{
double dx=(x_end-x_start);
double dy=(y_end-y_start);
double steps;
float xInc,yInc,x_=x_start,y_=y_start;
/* Find out whether to increment x or y */
steps= max(abs(dx),abs(dy));
xInc=dx/(float)steps;
yInc=dy/(float)steps;
/* Plot the first point */
int k;
/* For every step, find an intermediate vertex */
for(k=0; k<steps; k++)
{
/* printf("%0.6lf %0.6lf\n",floor(x), floor(y)); */
int xx = round(x_);
int yy = round(y_);
int a = visible_test(xx,yy);
if(a != 0)
{
glBegin(GL_POINTS);
glVertex2d(xx, yy);
glEnd();
glFlush();
}
x_+=xInc;
y_+=yInc;
}
}
/***
Tarequzzaman Khan
CSE'12
2012331514
*/
void init()
{
glClearColor(1.0, 1.0, 1.0,0);
glColor3f(0.0,0.0,0.0);
gluOrtho2D(-520,640,-440,480);
}
void input()
{
printf("Enter the window co-ordinate x_min and y_min\n");
scanf("%d %d",&wx1,&wy1);
printf("Enter the window co-ordinate x_max and y_max\n");
while(true)
{
scanf("%d %d",&wx2,&wy2);
if(wx1>=wx2 || wy1>=wy2)
{
printf("Enter valid end points\n");
}
else
{
break;
}
}
/***
Tarequzzaman Khan
CSE'12
2012331514
*/
printf("How many line\n");
scanf("%d",&line_no);
printf("x1 y1 x2 y2\n");
for(int i=0; i<line_no; i++)
{
scanf("%d %d",&line_arr[i].x_start, &line_arr[i].y_start);
int a = visible_test(line_arr[i].x_start,line_arr[i].y_start);
bit_generate(a);
scanf("%d %d",&line_arr[i].x_end, &line_arr[i].y_end);
int b = visible_test(line_arr[i].x_end,line_arr[i].y_end);
bit_generate(b);
int p = a&b;
if(a==0 && b==0)
{
line_arr[i].visible = 2;///completely inside
}
else if(p==0)
{
line_arr[i].visible = 1;///clipping candidate
}
else
{
line_arr[i].visible = 0;///completely outside
}
}
}
void line_clip()
{
glClear(GL_COLOR_BUFFER_BIT);
dda(wx1,wy1,wx1,wy2);
dda(wx1,wy2,wx2,wy2);
dda(wx2,wy2,wx2,wy1);
dda(wx2,wy1,wx1,wy1);
for(int i=0; i<line_no; i++)
{
if(line_arr[i].visible==0)
{
glColor3f(1.0,0.0,0.0);///set red color which is completely outside
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end); /// line drawing Algorithm
}
else if(line_arr[i].visible==1)
{
glColor3f(0.0,0.0,1.0);/// set blue color for clipping candidate
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);
}
else
{
glColor3f(0.0,1.0,0.0);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);
}
}
/***
Tarequzzaman Khan
CSE'12
2012331514
*/
for(int i=0; i<line_no; i++)
{
if(line_arr[i].visible==0)///completely outside line
{
glColor3f(1.0,1.0,1.0);///set white color
std::this_thread::sleep_for(std::chrono::milliseconds(1000));///sleep for 1 sec
dda(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);
}
}
for(int i=0; i<line_no; i++)
{
if(line_arr[i].visible==1)/// clipping candidate
{
glColor3f(1.0,1.0,1.0);///set white color
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
dda1(line_arr[i].x_start,line_arr[i].y_start,line_arr[i].x_end,line_arr[i].y_end);
}
}
glColor3f(0.0,0.0,0.0);///set default black color
}
int main(int argc,char ** argv)
{
/***
Tarequzzaman Khan
CSE'12
2012331514
*/
glutInit(&argc,argv);
input();
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0,0);
glutInitWindowSize(640,480);
glutCreateWindow("Cohen-Sutherland Line Clipping");
init();
glutDisplayFunc(line_clip);
glutMainLoop();
/***
Tarequzzaman Khan
CSE'12
2012331514
*/
}