/*
Program : Clipping
*/
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<sstream>
#include<stack>
#include<list>
#include<iterator>
#include <windows.h>
#include <gl/glut.h>
using namespace std;
#define REP(i,n) for(i=0; i<(n); i++)
#define FOR(i,a,b) for(i=(a); i<=(b); i++)
#define CLEAR(t) memset((t), 0, sizeof(t))
#define sz size()
#define pb push_back
#define pf push_front
#define VI vector<int>
#define VS vector<string>
#define LL long long
#define WIDTH 640
#define HEIGHT 480
#define pi 2*acos(0.0)
#define Wxmin 0
#define Wxmax 200
#define Wymin 0
#define Wymax 200
struct wind
{
double x[4],y[4];
}W;
double XX = .2,YY=.3;
struct Point
{
double x;
double y;
double z;
double w;
}P[4],D,PP[4],QQ[4];
int cho,angle;
double M[4][4],R[4][4],Q,zp;
typedef unsigned int outcode;
enum{TOP = 0x1,BOTTOM=0x2,RIGHT=0x4,LEFT=0x8};
void reshape(int width, int height)
{
glViewport(0,0,width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-WIDTH/2, WIDTH/2+1, -HEIGHT/2, HEIGHT/2+1,-1000,1000);
}
void drawPixel(int x, int y, int op)
{
if(op == 0)glVertex2i(x,y);
else if(op == 3)glVertex2i(-x,y);
else if(op == 4)glVertex2i(-x,-y);
else if(op == 7)glVertex2i(x,-y);
else if(op == 1)glVertex2i(y,x);
else if(op == 2)glVertex2i(-y,x);
else if(op == 5)glVertex2i(-y, -x);
else if(op == 6)glVertex2i(y, -x);
}
void drawLine(int x0,int y0,int x1,int y1,int value)
{
int dx = x1 – x0;
int dy = y1 – y0;
int dinitial = 2*dy – dx;
int dE = 2*dy;
int dNE = 2*(dy – dx);
int x = x0;
int y = y0;
drawPixel(x,y,value);
while ( x < x1 )
{
if( dinitial < 0 )
{
x ++;
dinitial += dE;
}
else
{
x++;
y++;
dinitial += dNE;
}
drawPixel(x,y,value);
}
}
void drawSlop(int x0, int y0, int x1, int y1)
{
int dx = x1 – x0;
int dy = y1 – y0;
if(abs(dx)>=abs(dy))//groupe 0
{
if((x1>=x0)&&(y1>=y0))
drawLine(x0,y0,x1,y1,0);//op=0
else if((x1<x0)&&(y1>=y0))
drawLine(-x0,y0,-x1,y1,3);//op3
else if((x1<x0) && (y1<y0))
drawLine(-x0,-y0,-x1,-y1,4);//op = 4
else
drawLine(x0,-y0,x1,-y1,7);//op = 7
}
else//groupe = 1…
{
if((x1>=x0)&&(y1>=y0))
drawLine(y0,x0,y1,x1,1);//op=1
else if((x1<x0)&&(y1>=y0))
drawLine(y0,-x0,y1,-x1,2);//op=2
else if((x1<x0)&&(y1<y0))
drawLine(-y0,-x0,-y1,-x1,5);//op=5
else if((x1>=x0)&&(y1<=y0))
drawLine(-y0,x0,-y1,x1,6);//op=6
}
}
void getPoint()
{
FILE *fp;
int i;
fp = fopen(“input.txt”,”r”);
REP(i,4)
{
fscanf(fp,”%lf %lf %lf”,&P[i].x,&P[i].y,&P[i].z);
}
fclose(fp);
}
outcode CompOutCode(double x,double y,double xmin,double xmax,double ymin,double ymax)
{
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 CohenSutherlandLineClipAndDraw(double x0,double y0,double x1,double y1,double xmin,double xmax,double ymin,double ymax)
{
outcode outcode0,outcode1,outcodeOut;
bool accept = false,done = false;
outcode0 = CompOutCode(x0,y0,xmin,xmax,ymin,ymax);
outcode1 = CompOutCode(x1,y1,xmin,xmax,ymin,ymax);
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 = CompOutCode(x0,y0,xmin,xmax,ymin,ymax);
}
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1,xmin,xmax,ymin,ymax);
}
}
}while(done==false);
if(accept)
{
drawSlop(x0,y0,x1,y1);
}
}
void drawTetra()
{
int i,j;
REP(i,4)
{
j = (i + 1)%4;
drawSlop((int)P[i].x,(int)P[i].y,(int)P[j].x,(int)P[j].y);
}
REP(i,2)
{
j = (i + 2)%4;
drawSlop(P[i].x,P[i].y,P[j].x,P[j].y);
}
}
void drawTetraC()
{
int i,j;
REP(i,4)
{
j = (i + 1)%4;
CohenSutherlandLineClipAndDraw(P[i].x,P[i].y,P[j].x,P[j].y,W.x[0],W.x[1],W.y[0],W.y[2]);
}
REP(i,2)
{
j = (i + 2)%4;
CohenSutherlandLineClipAndDraw(P[i].x,P[i].y,P[j].x,P[j].y,W.x[0],W.x[1],W.y[0],W.y[2]);
}
}
void findQ()
{
Q = sqrt((D.x*D.x) + (D.y*D.y) + (D.z)*(D.z));
}
void getPos()
{
FILE *fp;
fp = fopen(“P.txt”,”r”);
fscanf(fp,”%lf %lf %lf”,&D.x,&D.y,&D.z);
fscanf(fp,”%lf”,&zp);
D.z=D.z-zp;
findQ();
D.x/=Q;
D.y/=Q;
D.z/=Q;
fclose(fp);
}
void setW()
{
W.x[0] = Wxmin;
W.y[0] = Wymin;
W.x[1] = Wxmax;
W.y[1] = Wymin;
W.x[2] = Wxmax;
W.y[2] = Wymax;
W.x[3] = Wxmin;
W.y[3] = Wymax;
}
void drawW()
{
int i,j;
REP(i,4)
{
j = (i + 1)%4;
drawSlop((int)W.x[i],(int)W.y[i],(int)W.x[j],(int)W.y[j]);
}
}
void resetW()
{
int i;
if(W.x[0]+XX<=-320||W.x[1]+XX>=320)
XX = -XX;
if(W.y[0]+YY<=-240||W.y[2]+YY>=240)
YY = -YY;
REP(i,4)
W.x[i]+=XX,W.y[i]+=YY;
}
void display(void)
{
glColor4f(1.0,0.0,0.0,1.0);
angle = 0;
getPoint();
getPos();
setW();
while(true)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POINTS);
resetW();
glColor4f(3.0,5.0,1.0,1.0);
drawW();
glColor4f(0.0,0.0,0.0,0.0);
drawTetra();
glColor4f(1.0,0.0,0.0,1.0);
drawTetraC();
glEnd();
glutSwapBuffers();
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowPosition(-1,-1);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow(“My Window”);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Recent Comments