GLUT Shapes Dem-WPS Office
GLUT Shapes Dem-WPS Office
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include <stdio.h>
if(x>xmax)code+=2;
else if(x<xmin)code+=1;
return code;
}
void drawLine(int x0, int y0, int x1, int y1, int zone)
{
int dX = x1-x0;
int dY = y1-y0;
int x=x0,y=y0;
int d= 2*dY-dX;
int dE=2*dY,dNE=2*(dY-dX);
// glVertex2i(x,y);
while(x<x1)
{
if(d<0)
{
x++;
d+=dE;
}
else
{
x++;
y++;
d+=dNE;
}
if(zone==0)
glVertex2i(x,y);
else if(zone==1)
glVertex2i(y,x);
else if(zone==2)
glVertex2i(-y,x);
else if(zone==3)
glVertex2i(-x,y);
else if(zone==4)
glVertex2i(-x,-y);
else if(zone==5)
glVertex2i(-y,-x);
else if(zone==6)
glVertex2i(y,-x);
else if(zone==7)
glVertex2i(x,-y);
if(click==0){
x0Click=x0=x;
y0Click=y0=y;
click=1;
nxyget=0;
}
else if(click ==1 ){
x1Click=x1=x;
y1Click=y1=y;
click=2;
writen=false;
}
else{
x0Click=x0=x;
y0Click=y0=y;
click =1;
nxyget=0;
}
printf("%d %d %d %d\n",x0,y0,x1,y1);
}
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN){
glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT);
check=false;
}
glutPostRedisplay();
}
void CohenSutherLand(){
int code0, code1,code;
int x, y;
int top=8,bottom=4,left=1,right=2;
code0=makeCode(x0,y0);
code1=makeCode(x1,y1);
while(1){
if(!(code0|code1)){
nxyget=1;
lineDraw(x0,y0,x1,y1);
if(!writen)
printf("Completely Accepted\n"),writen=true;
break;
}
else if(code0 & code1){
if(!writen)
printf("Completely Rejected\n"),writen=true;
break;
}
else{
if(!writen)
printf("Partially Accepted\n"),writen=true;
if(code0)code=code0;
else code=code1;
}
else if(code & bottom){
y=ymin;
x=x0+((y-y0)*(x1-x0)) / (y1-y0);
}
else if(code & left){
x=xmin;
y=y0+((x-x0)*(y1-y0)) / (x1-x0);
}
else{
x=xmax;
y=y0+((x-x0)*(y1-y0)) / (x1-x0);
}
if(code==code0){
x0=x;y0=y;
code0=makeCode(x0,y0);
}
else if(code==code1){
x1=x;y1=y;
code1=makeCode(x1,y1);
}
}
}
}
glVertex2i(xl+gp,yl+gp);
glVertex2i(xl+gp,yr-gp);
glVertex2i(xl+gp,yr-gp);
glVertex2i(xr-gp,yr-gp);
glVertex2i(xr-gp,yr-gp);
glVertex2i(xr-gp,yl+gp);
glVertex2i(xr-gp,yl+gp);
glVertex2i(xl+gp,yl+gp);
glEnd();
glPointSize(5.0);
glBegin(GL_POINTS);
if(click==1){
glVertex2i(x0Click,y0Click);
}
else if(nxyget==0 and click==2){
glVertex2i(x0Click,y0Click);
glVertex2i(x1Click,y1Click);
}
else if(nxyget == 1 and click==2){
glVertex2i(x0Click,y0Click);
glVertex2i(x1Click,y1Click);
glVertex2i(x0,y0);
glVertex2i(x1,y1);
}
glEnd();
glPointSize(1.0);
glBegin(GL_POINTS);
if(click==2) {
glColor3f(1,0,0);
lineDraw(x0Click,y0Click,x1Click,y1Click);
glColor3f(1,1,1);
CohenSutherLand();
}
glEnd();
glutSwapBuffers();
}
case '+':
slices++;
stacks++;
break;
case '-':
if (slices>3 && stacks>3)
{
slices--;
stacks--;
}
break;
}
glutPostRedisplay();
}
glutCreateWindow("Experiment 01");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutMouseFunc(mouse);
glutMainLoop();
return EXIT_SUCCESS;
}