Computer Graphics Program5
Computer Graphics Program5
cpp 1
1 #include<stdio.h>
2 #include<GL/glut.h>
3 #define outcode int
4 double xmin=50,ymin=50,xmax=100,ymax=100;
5 double xvmin=200,yvmin=200,xvmax=300,yvmax=300;
6 double x0,y0,x1,y1;
7 const int RIGHT=8;
8 const int LEFT=2;
9 const int TOP=4;
10 const int BOTTOM=1;
11 outcode ComputeOutCode(double x, double y);
12 void CohenSutherland(double x0, double y0, double x1, double y1)
13 {
14 outcode outcode0, outcode1, outcodeOut;
15 bool accept=false, done=false;
16 outcode0=ComputeOutCode(x0,y0);
17 outcode1=ComputeOutCode(x1,y1);
18 do
19 {
20 if(!(outcode0|outcode1))
21 {
22 accept=true;
23 done=true;
24 }
25 else if(outcode0&outcode1)
26 done=true;
27 else
28 {
29 double x, y;
30 outcodeOut=outcode0?outcode0:outcode1;
31 if(outcodeOut&TOP)
32 {
33 x=x0+(x1-x0)*(ymax-y0)/(y1-y0);
34 y=ymax;
35 }
36 else if(outcodeOut&BOTTOM)
37 {
38 x=x0+(x1-x0)*(ymin-y0)/(y1-y0);
39 y=ymin;
40 }
41 else if(outcodeOut&RIGHT)
42 {
43 y=y0+(y1-y0)*(xmax-x0)/(x1-x0);
44 x=xmax;
45 }
46 else
47 {
48 y=y0+(y1-y0)*(xmin-x0)/(x1-x0);
49 x=xmin;
50 }
51 if(outcodeOut==outcode0)
52 {
53 x0=x;
54 y0=y;
55 outcode0=ComputeOutCode(x0,y0);
56 }
57 else
58 {
59 x1=x;
60 y1=y;
61 outcode1=ComputeOutCode(x1,y1);
62 }
63 }
64 }while(!done);
65 if(accept)
66 {
67 double sx=(xvmax-xvmin)/(xmax-xmin);
68 double sy=(yvmax-yvmin)/(ymax-ymin);
69 double vx0=xvmin+(x0-xmin)*sx;
70 double vy0=yvmin+(y0-ymin)*sy;
71 double vx1=xvmin+(x1-xmin)*sx;
72 double vy1=yvmin+(y1-ymin)*sy;
73 glColor3f(1.0,0.0,0.0);
74 glBegin(GL_LINE_LOOP);
C:\Users\madar\Documents\Visual Studio 2010\Projects\prgm5\prgm5\prg5.cpp 2
75 glVertex2f(xvmin, yvmin);
76 glVertex2f(xvmax, yvmin);
77 glVertex2f(xvmax, yvmax);
78 glVertex2f(xvmin, yvmax);
79 glEnd();
80 glColor3f(0.0,0.0,1.0);
81 glBegin(GL_LINES);
82 glVertex2d(vx0,vy0);
83 glVertex2d(vx1,vy1);
84 glEnd();
85 }
86 }
87 outcode ComputeOutCode(double x, double y)
88 {
89 outcode code=0;
90 if(y>ymax)
91 code=TOP;
92 else if(y<ymin)
93 code=BOTTOM;
94 if(x>xmax)
95 code=RIGHT;
96 else if(x<xmin)
97 code=LEFT;
98 return code;
99 }
100 void display()
101 {
102 glClear(GL_COLOR_BUFFER_BIT);
103 glColor3f(1.0,0.0,0.0);
104 glBegin(GL_LINES);
105 glVertex2d(x0,y0);
106 glVertex2d(x1,y1);
107 glEnd();
108 glColor3f(0.0,0.0,1.0);
109 glBegin(GL_LINE_LOOP);
110 glVertex2f(xmin, ymin);
111 glVertex2f(xmax, ymin);
112 glVertex2f(xmax, ymax);
113 glVertex2f(xmin, ymax);
114 glEnd();
115 CohenSutherland(x0,y0,x1,y1);
116 glFlush();
117 }
118 void myinit()
119 {
120 glClearColor(1.0,1.0,1.0,1.0);
121 glMatrixMode(GL_PROJECTION);
122 glLoadIdentity();
123 gluOrtho2D(0.0,499.0,0.0,499.0);
124 }
125
126 void main(int argc, char** argv)
127 {
128 printf("Enter the end points of the line: ");
129 scanf("%lf%lf%lf%lf", &x0,&y0,&x1,&y1);
130 glutInit(&argc, argv);
131 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
132 glutInitWindowSize(500,500);
133 glutInitWindowPosition(0,0);
134 glutCreateWindow("Cohen-Sutherland Line Clipping");
135 glutDisplayFunc(display);
136 myinit();
137 glutMainLoop();
138 }