标签:idt mode inux iso struct ras main void single
作业。。。还是多做事,少想。。与其想一些遥不可及的事情,不如把脚下的事情做好。。。
应该分几个文件好好写的。。。习惯真差。。。
DDA,Bresenham,Scanline,按照中心平移,旋转,放大缩小,变化窗口大小。。。
ubuntu 下要装freeglut。。。
#ifdef WIN32 #include <windows.h> #endif #if defined (__APPLE__) || defined(MACOSX) #include <OpenGL/gl.h> //#include <OpenGL/glu.h> #include <GLUT/glut.h> #else //linux #include <GL/gl.h> #include <GL/glut.h> #endif //other includes #include <cmath> #include <stdio.h> #include <stdlib.h> #include <fstream> #include <vector> #include <iostream> #include <algorithm> using namespace std; struct point { double x, y; point() {}; point(double a, double b) { x = a; y = b; } }; struct polygon { vector<point> p; }; int xmin=0,ymin=0,xmax=500,ymax=500; int win_height; int win_width; int p_index; int parameter=10; float pixel_size; int grid_width; int grid_height; int rond(double x); void dda(point p1, point p2); void bresen(point p1, point p2); void rasterize(polygon p1); point getcenter(polygon p1); void translate(polygon &p1,int x,int y); void rotation(polygon &p1, double p); void scaling(polygon &p1, double p); void write(); void info(); void clipping(int xmin,int xmax,int ymin,int ymax, polygon &p1); // newly added void init(); void idle(); void display(); void draw_pix(int x, int y); void reshape(int width, int height); void key(unsigned char ch, int x, int y); void mouse(int button, int state, int x, int y); void motion(int x, int y); void check(); polygon arr[101]; int k; void init() { //set clear color (Default backgrond to white) glClearColor(1.0,1.0,1.0,1.0); //checks for OpenGL errors check(); } //called repeatedly when glut isn‘t doing anything else void idle() { //redraw the scene over and over again glutPostRedisplay(); } //this is where we render the screen void display() { //clears the screen glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); //clears the opengl Modelview transformation matrix glLoadIdentity(); //draws every other pixel on the screen /*for (int j = 0; j < grid_height; j+=2){ for (int i = 0; i < grid_width; i+=2){ //this is the only "rendering call you should make in project 1" draw_pix(i,j); } }*/ for (int i = 0; i < k; i++) { for (int j = 0; j < arr[i].p.size()-1; j++) { if (rond(arr[i].p[j].x)<xmax && rond(arr[i].p[j].x)>xmin &&rond(arr[i].p[j].y)>ymin &&rond(arr[i].p[j].y)<ymax) draw_pix(rond(arr[i].p[j].x),rond(arr[i].p[j].y)); bresen(arr[i].p[j],arr[i].p[j+1]); rasterize(arr[i]); } bresen(arr[i].p[arr[i].p.size()-1],arr[i].p[0]); } //blits the current opengl framebuffer on the screen glutSwapBuffers(); //checks for opengl errors check(); } void display2() { //clears the screen glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); //clears the opengl Modelview transformation matrix glLoadIdentity(); //draws every other pixel on the screen /*for (int j = 0; j < grid_height; j+=2){ for (int i = 0; i < grid_width; i+=2){ //this is the only "rendering call you should make in project 1" draw_pix(i,j); } }*/ for (int i = 0; i < k; i++) { if(arr[i].p.size()==0){ continue; } for (int j = 0; j < arr[i].p.size()-1; j++) { if (rond(arr[i].p[j].x)<xmax && rond(arr[i].p[j].x)>xmin &&rond(arr[i].p[j].y)>ymin &&rond(arr[i].p[j].y)<ymax) draw_pix(rond(arr[i].p[j].x),rond(arr[i].p[j].y)); dda(arr[i].p[j],arr[i].p[j+1]); } //cout<<arr[i].p[0].x<<‘ ‘<<arr[i].p[0].y<<endl; dda(arr[i].p[arr[i].p.size()-1],arr[i].p[0]); rasterize(arr[i]); } //blits the current opengl framebuffer on the screen glutSwapBuffers(); //checks for opengl errors check(); } //Draws a single "pixel" given the current grid size //don‘t change anything in this for project 1 void draw_pix(int x, int y){ glBegin(GL_POINTS); glColor3f(.2,.2,1.0); glVertex3f(x+.5,y+.5,0); glEnd(); } /*Gets called when display size changes, including initial craetion of the display*/ void reshape(int width, int height) { /*set up projection matrix to define the view port*/ //update the ne window width and height win_width = width; win_height = height; //creates a rendering area across the window glViewport(0,0,width,height); // up an orthogonal projection matrix so that // the pixel space is mapped to the grid space glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0,grid_width,0,grid_height,-10,10); //clear the modelview matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //set pixel size based on width, if the aspect ratio //changes this hack won‘t work as well pixel_size = width/(float)grid_width; //set pixel size relative to the grid cell size glPointSize(pixel_size); //check for opengl errors check(); } //gets called when a key is pressed on the keyboard void key(unsigned char ch, int x, int y) { switch(ch) { case ‘d‘: glutDisplayFunc(display2); break;//DDA alg case ‘b‘: glutDisplayFunc(display); break;//Bresenham alg default case ‘0‘: p_index = 0; break; case ‘1‘: p_index =1; break; case ‘2‘: p_index =2; break; case ‘3‘: p_index =3; break; case ‘4‘: p_index =4; break; case ‘5‘: p_index =5; break; case ‘6‘: p_index =6; break; case ‘7‘: p_index =7; break; case ‘8‘: p_index =8; break; case ‘9‘: p_index =9; break; case ‘c‘: cout<<"Enter clipping boundary(Xmin Xmax Ymin Ymax) seperate by a space"<<endl; cout<<"The default window size is 500*500"<<endl; cin>>xmin>>xmax>>ymin>>ymax; break; case ‘t‘: double x,y; cout<<"Enter translation vector in the terminal please (seperate by a space)"<<endl; cin>>x>>y; translate(arr[p_index],x,y);info();cout<<endl; cout<<"click on the demo window for more operation"<<endl; break; case ‘r‘: cout<<"Enter rotation parameter in the terminal please (in radius)"<<endl; cin>>x; rotation(arr[p_index],x);info();cout<<endl; cout<<"click on the demo window for more operation"<<endl; break; case ‘s‘: cout<<"Enter scaling parameter in the terminal please"<<endl; cin>>x; scaling(arr[p_index],x);info();cout<<endl; cout<<"click on the demo window for more operation"<<endl; break; case ‘p‘: xmin=0;xmax=500;ymin=0;ymax=500; case ‘w‘: write(); cout<<"new data is in 2.txt"<<endl; info(); break; default: //prints out which key the user hit printf("User hit the \"%c\" key\n",ch); break; } //redraw the scene after keyboard input glutPostRedisplay(); } //gets called when a mouse button is pressed void mouse(int button, int state, int x, int y) { //print the pixel location, and the grid location printf ("MOUSE AT PIXEL: %d %d, GRID: %d %d\n",x,y,(int)(x/pixel_size),(int)((win_height-y)/pixel_size)); switch(button) { case GLUT_LEFT_BUTTON: //left button //printf("LEFT "); break; case GLUT_RIGHT_BUTTON: //right button //printf("RIGHT "); default: //printf("UNKNOWN "); //any other mouse button break; } if(state !=GLUT_DOWN) x; //button released //printf("BUTTON UP\n"); else //printf("BUTTON DOWN\n"); //button clicked //redraw the scene after mouse click glutPostRedisplay(); } //gets called when the curser moves accross the scene void motion(int x, int y) { //redraw the scene after mouse movement glutPostRedisplay(); } //checks for any opengl errors in the previous calls and //outputs if they are present void check() { GLenum err = glGetError(); if(err != GL_NO_ERROR) { printf("GLERROR: There was an error %s\n",gluErrorString(err) ); exit(1); } } int rond(double x){ if(x-(int)x >=0.5){ return (int)x+1; } else{ return (int)x; } }//rond decimal void dda(point p1, point p2){ double dy=(p2.y-p1.y); double dx = (p2.x-p1.x); { int s = abs(dx) > abs(dy) ? abs(dx) : abs(dy); double xIncr, yIncr, x = p1.x, y = p1.y; xIncr = double(dx) / double(s); yIncr = double(dy) / double(s); for (int i = 0; i < s; i++) { x += xIncr; y += yIncr; if(rond(x)>=xmin && rond(x)<=xmax && rond(y)>=ymin && rond(y)<=ymax) draw_pix(rond(x), rond(y)); } } }//dda from textbook void bresen(point p1, point p2){ int x1 = rond(p1.x),x2=rond(p2.x),y1=rond(p1.y),y2=rond(p2.y); int dx = abs(x1-x2),dy = abs(y1-y2); if(dx>dy){ int p = 2*dy-dx; int tdy = 2*dy; int tdy_dx = 2*(dy-dx); if(x1>x2){ swap(x1,x2); swap(y1,y2); } while(x1<x2){ x1++; if(p<0){ p+=tdy; } else{ if(y2>y1){ y1++; } else{ y1--; } p+=tdy_dx; } if(x1>=xmin && x1<=xmax && y1>=ymin &&y1>=ymax) draw_pix(x1,y1); } } else{ int p = 2*dx-dy; int tdx = 2*dx; int tdx_dy = 2*(dx-dy); if(y1>y2){ swap(x1,x2); swap(y1,y2); } while(y1<y2){ y1++; if(p<0){ p+=tdx; } else{ if(x2>x1){ x1++; } else{ x1--; } p+=tdx_dy; } if(x1>=xmin && x1<=xmax && y1>=ymin &&y1>=ymax) draw_pix(x1,y1); } } }//bresen modified from textbook struct line{ double y1,y2,x1,x2; double max,min; }; void rasterize(polygon poly){ vector<line> ls; vector<line> ael; int exist[1001]; fill(exist,exist+1001,0); vector<int> inter[1001]; for(int i=0;i<poly.p.size()-1;i++){ point p1 = poly.p[i]; point p2 = poly.p[i+1]; line temp; temp.y1 = rond(p1.y); temp.y2 = rond(p2.y); temp.x1 = rond(p1.x); temp.x2 = rond(p2.x); temp.min = min(temp.y1,temp.y2); temp.max = max(temp.y1,temp.y2); if(temp.y1!=temp.y2) ls.push_back(temp); } if(poly.p.size()>2){ line temp; temp.y1 = rond(poly.p[poly.p.size()-1].y); temp.y2 = rond(poly.p[0].y); temp.x1 = rond(poly.p[poly.p.size()-1].x); temp.x2 = rond(poly.p[0].x); temp.min = min(temp.y1,temp.y2); temp.max = max(temp.y1,temp.y2); //temp.invslope = 1/((temp.y2-temp.y1)/(temp.x2-temp.x1)); if(temp.y1!=temp.y2) ls.push_back(temp); } for(int i=0;i<1000;i++){ for(int j=0;j<ls.size();j++){ if((i==ls[j].y1|| i==ls[j].y2)&& exist[j]==0){ exist[j]=1; ael.push_back(ls[j]); } } int g=0; for(int k=0;k<ael.size();k++){ if(i>=ael[k].min && i<ael[k].max){ double dy = ael[k].y2- ael[k].y1; double dx = ael[k].x2 - ael[k].x1; if(dx==0){ inter[i].push_back(ael[k].x1); } else{ double s; if(dx*dy>0){ s = abs(dy)/abs(dx); } else{ s = -abs(dy)/abs(dx); } double ddy = i-ael[k].y1; double ddx = ddy/s; int x = rond(ddx+ael[k].x1); inter[i].push_back(x); } } } } for(int i=0;i<1000;i++){ sort(inter[i].begin(),inter[i].end()); int g = inter[i].size(); int c=0; for(int p=0;p<g-1;p+=2){ //p++; for(int k = inter[i][p];k<=inter[i][p+1];k++){ if(k>=xmin && k<=xmax && i>=ymin && i<=ymax) draw_pix(k,i); } } } } void info(){ cout<<"hit 1~9 to choose the polygon"<<endl; cout<<"hit d for DDA algorithm"<<endl; cout<<"hit b for bresenham algorithm"<<endl; cout<<"hit t for translation"<<endl; cout<<"hit r for rotation"<<endl; cout<<"hit s for scaling"<<endl; cout<<"hit w to write data"<<endl; cout<<"hit p for default window size"<<endl<<endl; } bool operator==(const point& lhs, const point& rhs) { if(lhs.x==rhs.x && lhs.y==rhs.y){ return 1; } return 0;/* your comparison code goes here */ } void translate(polygon &p1 ,int x, int y){ for(int i=0;i<p1.p.size();i++){ p1.p[i].x+=x; p1.p[i].y+=y; } } void rotation(polygon &p1, double p){ point center = getcenter(p1); double r = p; for(int i=0;i<p1.p.size();i++){ point p = p1.p[i]; p1.p[i].x = (p.x - center.x) * cos(r) - (p.y-center.y) * sin (r) + center.x; p1.p[i].y = (p.x - center.x) * sin(r) + (p.y - center.y) * cos(r) +center.y; } } void scaling(polygon &p1, double r){ point center = getcenter(p1); for(int i=0;i<p1.p.size();i++){ point p = p1.p[i]; p1.p[i].x = (p.x - center.x) *r + center.x; p1.p[i].y = (p.y - center.y) * r +center.y; } } point getcenter(polygon p1){ double area=0; point centroid;int i=0; centroid.x = centroid.y=0; for(i=0;i<p1.p.size()-1;i++){ int x1 = rond(p1.p[i].x); int x2 = rond(p1.p[i+1].x); int y1 = rond(p1.p[i].y); int y2 = rond(p1.p[i+1].y); int a = x1*y2-x2*y1; area+=a; centroid.x +=(x1+x2)*a; centroid.y +=(y1+y2)*a; } int x1 = rond(p1.p[i].x); int x2 = rond(p1.p[0].x); int y1 = rond(p1.p[i].y); int y2 = rond(p1.p[0].y); int a = x1*y2-x2*y1; area+=a; centroid.x +=(x1+x2)*a; centroid.y +=(y1+y2)*a; area *= 0.5; centroid.x /= (6.0*area); centroid.y /= (6.0*area); centroid.x = rond(centroid.x); centroid.y = rond(centroid.y); return centroid; } void write(){ ofstream os("2.txt"); os<<k<<endl; for(int i=0;i<k;i++){ os<<arr[i].p.size()<<endl; for(int j=0;j<arr[i].p.size();j++){ os<<arr[i].p[j].x<<‘ ‘<<arr[i].p[j].y<<endl; } os<<endl; } } int main(int argc, char **argv) { ifstream myfile("1.txt"); myfile >> k; for (int i = 0; i < k; i++) { int n; myfile >> n; for (int j = 0; j < n; j++) { double d1, d2; myfile >> d1 >> d2; point temp(d1,d2); arr[i].p.push_back(temp); } } info(); grid_width = 500; grid_height = 500; pixel_size = 1; win_height = grid_height*pixel_size; win_width = grid_width*pixel_size; glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(win_width,win_height); glutCreateWindow("glut demo"); glutDisplayFunc(display2); //rendering calls here glutReshapeFunc(reshape); //update GL on window size change glutMouseFunc(mouse); //mouse button events glutMotionFunc(motion); //mouse movement events glutKeyboardFunc(key); //Keyboard events glutIdleFunc(idle); //Function called while program is sitting "idle" for(int i=0;i<k;i++){ //clipping(200,300,200,300,arr[i]); } //initialize opengl variables init(); //start glut event loop glutMainLoop(); return 0; }
#ifdef WIN32#include <windows.h>#endif
#if defined (__APPLE__) || defined(MACOSX)#include <OpenGL/gl.h>//#include <OpenGL/glu.h>#include <GLUT/glut.h>
#else //linux#include <GL/gl.h>#include <GL/glut.h>#endif
//other includes#include <cmath>#include <stdio.h>#include <stdlib.h>#include <fstream>#include <vector>#include <iostream>#include <algorithm>using namespace std;
struct point {double x, y;point() {};
point(double a, double b) {x = a;y = b;}
};
struct polygon {vector<point> p;};
int xmin=0,ymin=0,xmax=500,ymax=500;
int win_height;int win_width;
int p_index;int parameter=10;float pixel_size;
int grid_width;int grid_height;
int rond(double x);void dda(point p1, point p2);void bresen(point p1, point p2);void rasterize(polygon p1);point getcenter(polygon p1);void translate(polygon &p1,int x,int y);void rotation(polygon &p1, double p);void scaling(polygon &p1, double p);void write();void info();void clipping(int xmin,int xmax,int ymin,int ymax, polygon &p1);// newly added
void init();void idle();void display();void draw_pix(int x, int y);void reshape(int width, int height);void key(unsigned char ch, int x, int y);void mouse(int button, int state, int x, int y);void motion(int x, int y);void check();
polygon arr[101];int k;
void init(){ //set clear color (Default backgrond to white)glClearColor(1.0,1.0,1.0,1.0); //checks for OpenGL errorscheck();}
//called repeatedly when glut isn‘t doing anything elsevoid idle(){ //redraw the scene over and over againglutPostRedisplay();}
//this is where we render the screenvoid display(){ //clears the screenglClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); //clears the opengl Modelview transformation matrixglLoadIdentity();
//draws every other pixel on the screen /*for (int j = 0; j < grid_height; j+=2){ for (int i = 0; i < grid_width; i+=2){ //this is the only "rendering call you should make in project 1" draw_pix(i,j); } }*/
for (int i = 0; i < k; i++) {for (int j = 0; j < arr[i].p.size()-1; j++) { if (rond(arr[i].p[j].x)<xmax && rond(arr[i].p[j].x)>xmin &&rond(arr[i].p[j].y)>ymin &&rond(arr[i].p[j].y)<ymax) draw_pix(rond(arr[i].p[j].x),rond(arr[i].p[j].y));bresen(arr[i].p[j],arr[i].p[j+1]);rasterize(arr[i]);}
bresen(arr[i].p[arr[i].p.size()-1],arr[i].p[0]);}
//blits the current opengl framebuffer on the screen glutSwapBuffers(); //checks for opengl errorscheck();}
void display2(){ //clears the screenglClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); //clears the opengl Modelview transformation matrixglLoadIdentity();
//draws every other pixel on the screen /*for (int j = 0; j < grid_height; j+=2){ for (int i = 0; i < grid_width; i+=2){ //this is the only "rendering call you should make in project 1" draw_pix(i,j); } }*/
for (int i = 0; i < k; i++) { if(arr[i].p.size()==0){ continue; }for (int j = 0; j < arr[i].p.size()-1; j++) { if (rond(arr[i].p[j].x)<xmax && rond(arr[i].p[j].x)>xmin &&rond(arr[i].p[j].y)>ymin &&rond(arr[i].p[j].y)<ymax) draw_pix(rond(arr[i].p[j].x),rond(arr[i].p[j].y));dda(arr[i].p[j],arr[i].p[j+1]);}//cout<<arr[i].p[0].x<<‘ ‘<<arr[i].p[0].y<<endl;dda(arr[i].p[arr[i].p.size()-1],arr[i].p[0]);rasterize(arr[i]);}
//blits the current opengl framebuffer on the screen glutSwapBuffers(); //checks for opengl errorscheck();}
//Draws a single "pixel" given the current grid size//don‘t change anything in this for project 1void draw_pix(int x, int y){ glBegin(GL_POINTS); glColor3f(.2,.2,1.0); glVertex3f(x+.5,y+.5,0); glEnd();}
/*Gets called when display size changes, including initial craetion of the display*/void reshape(int width, int height){/*set up projection matrix to define the view port*/ //update the ne window width and heightwin_width = width;win_height = height;
//creates a rendering area across the windowglViewport(0,0,width,height); // up an orthogonal projection matrix so that // the pixel space is mapped to the grid space glMatrixMode(GL_PROJECTION); glLoadIdentity();glOrtho(0,grid_width,0,grid_height,-10,10);
//clear the modelview matrix glMatrixMode(GL_MODELVIEW); glLoadIdentity();
//set pixel size based on width, if the aspect ratio //changes this hack won‘t work as well pixel_size = width/(float)grid_width;
//set pixel size relative to the grid cell size glPointSize(pixel_size); //check for opengl errorscheck();}
//gets called when a key is pressed on the keyboardvoid key(unsigned char ch, int x, int y){switch(ch){ case ‘d‘: glutDisplayFunc(display2); break;//DDA alg case ‘b‘: glutDisplayFunc(display); break;//Bresenham alg default case ‘0‘: p_index = 0; break; case ‘1‘: p_index =1; break; case ‘2‘: p_index =2; break; case ‘3‘: p_index =3; break; case ‘4‘: p_index =4; break; case ‘5‘: p_index =5; break; case ‘6‘: p_index =6; break; case ‘7‘: p_index =7; break; case ‘8‘: p_index =8; break; case ‘9‘: p_index =9; break; case ‘c‘: cout<<"Enter clipping boundary(Xmin Xmax Ymin Ymax) seperate by a space"<<endl; cout<<"The default window size is 500*500"<<endl; cin>>xmin>>xmax>>ymin>>ymax; break;
case ‘t‘: double x,y; cout<<"Enter translation vector in the terminal please (seperate by a space)"<<endl; cin>>x>>y; translate(arr[p_index],x,y);info();cout<<endl; cout<<"click on the demo window for more operation"<<endl;
break; case ‘r‘:
cout<<"Enter rotation parameter in the terminal please (in radius)"<<endl; cin>>x; rotation(arr[p_index],x);info();cout<<endl; cout<<"click on the demo window for more operation"<<endl;
break;
case ‘s‘:
cout<<"Enter scaling parameter in the terminal please"<<endl; cin>>x; scaling(arr[p_index],x);info();cout<<endl; cout<<"click on the demo window for more operation"<<endl;
break; case ‘p‘: xmin=0;xmax=500;ymin=0;ymax=500; case ‘w‘: write(); cout<<"new data is in 2.txt"<<endl; info(); break;default: //prints out which key the user hit printf("User hit the \"%c\" key\n",ch);break;} //redraw the scene after keyboard inputglutPostRedisplay();}
//gets called when a mouse button is pressedvoid mouse(int button, int state, int x, int y){ //print the pixel location, and the grid location printf ("MOUSE AT PIXEL: %d %d, GRID: %d %d\n",x,y,(int)(x/pixel_size),(int)((win_height-y)/pixel_size));switch(button){case GLUT_LEFT_BUTTON: //left button //printf("LEFT ");
break;case GLUT_RIGHT_BUTTON: //right button //printf("RIGHT ");
default: //printf("UNKNOWN "); //any other mouse buttonbreak;} if(state !=GLUT_DOWN) x; //button released //printf("BUTTON UP\n"); else //printf("BUTTON DOWN\n"); //button clicked
//redraw the scene after mouse click glutPostRedisplay();}
//gets called when the curser moves accross the scenevoid motion(int x, int y){ //redraw the scene after mouse movementglutPostRedisplay();}
//checks for any opengl errors in the previous calls and//outputs if they are presentvoid check(){GLenum err = glGetError();if(err != GL_NO_ERROR){printf("GLERROR: There was an error %s\n",gluErrorString(err) );exit(1);}}
int rond(double x){if(x-(int)x >=0.5){return (int)x+1;}else{return (int)x;}}//rond decimal
void dda(point p1, point p2){
double dy=(p2.y-p1.y);double dx = (p2.x-p1.x);
{int s = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
double xIncr, yIncr, x = p1.x, y = p1.y; xIncr = double(dx) / double(s); yIncr = double(dy) / double(s);
for (int i = 0; i < s; i++) { x += xIncr; y += yIncr; if(rond(x)>=xmin && rond(x)<=xmax && rond(y)>=ymin && rond(y)<=ymax) draw_pix(rond(x), rond(y)); }}
}//dda from textbook
void bresen(point p1, point p2){ int x1 = rond(p1.x),x2=rond(p2.x),y1=rond(p1.y),y2=rond(p2.y); int dx = abs(x1-x2),dy = abs(y1-y2);
if(dx>dy){ int p = 2*dy-dx; int tdy = 2*dy; int tdy_dx = 2*(dy-dx);
if(x1>x2){ swap(x1,x2); swap(y1,y2); } while(x1<x2){ x1++; if(p<0){ p+=tdy; }
else{ if(y2>y1){ y1++; } else{ y1--; }
p+=tdy_dx; } if(x1>=xmin && x1<=xmax && y1>=ymin &&y1>=ymax) draw_pix(x1,y1); } }
else{ int p = 2*dx-dy; int tdx = 2*dx; int tdx_dy = 2*(dx-dy); if(y1>y2){ swap(x1,x2); swap(y1,y2); } while(y1<y2){ y1++; if(p<0){ p+=tdx; }
else{ if(x2>x1){ x1++; } else{ x1--; }
p+=tdx_dy; } if(x1>=xmin && x1<=xmax && y1>=ymin &&y1>=ymax) draw_pix(x1,y1); } }}//bresen modified from textbook
struct line{ double y1,y2,x1,x2;
double max,min;};
void rasterize(polygon poly){
vector<line> ls; vector<line> ael; int exist[1001]; fill(exist,exist+1001,0); vector<int> inter[1001]; for(int i=0;i<poly.p.size()-1;i++){ point p1 = poly.p[i]; point p2 = poly.p[i+1]; line temp; temp.y1 = rond(p1.y); temp.y2 = rond(p2.y); temp.x1 = rond(p1.x); temp.x2 = rond(p2.x); temp.min = min(temp.y1,temp.y2); temp.max = max(temp.y1,temp.y2);
if(temp.y1!=temp.y2) ls.push_back(temp); } if(poly.p.size()>2){ line temp; temp.y1 = rond(poly.p[poly.p.size()-1].y); temp.y2 = rond(poly.p[0].y); temp.x1 = rond(poly.p[poly.p.size()-1].x); temp.x2 = rond(poly.p[0].x); temp.min = min(temp.y1,temp.y2); temp.max = max(temp.y1,temp.y2); //temp.invslope = 1/((temp.y2-temp.y1)/(temp.x2-temp.x1)); if(temp.y1!=temp.y2) ls.push_back(temp); }
for(int i=0;i<1000;i++){ for(int j=0;j<ls.size();j++){ if((i==ls[j].y1|| i==ls[j].y2)&& exist[j]==0){ exist[j]=1; ael.push_back(ls[j]);
} } int g=0; for(int k=0;k<ael.size();k++){ if(i>=ael[k].min && i<ael[k].max){ double dy = ael[k].y2- ael[k].y1; double dx = ael[k].x2 - ael[k].x1; if(dx==0){ inter[i].push_back(ael[k].x1); }
else{ double s; if(dx*dy>0){ s = abs(dy)/abs(dx);
} else{ s = -abs(dy)/abs(dx);
}
double ddy = i-ael[k].y1; double ddx = ddy/s; int x = rond(ddx+ael[k].x1);
inter[i].push_back(x); }
} }
}
for(int i=0;i<1000;i++){
sort(inter[i].begin(),inter[i].end()); int g = inter[i].size(); int c=0; for(int p=0;p<g-1;p+=2){ //p++;
for(int k = inter[i][p];k<=inter[i][p+1];k++){
if(k>=xmin && k<=xmax && i>=ymin && i<=ymax) draw_pix(k,i); } }
}
}
void info(){ cout<<"hit 1~9 to choose the polygon"<<endl; cout<<"hit d for DDA algorithm"<<endl; cout<<"hit b for bresenham algorithm"<<endl; cout<<"hit t for translation"<<endl; cout<<"hit r for rotation"<<endl; cout<<"hit s for scaling"<<endl; cout<<"hit w to write data"<<endl; cout<<"hit p for default window size"<<endl<<endl;
}
bool operator==(const point& lhs, const point& rhs){ if(lhs.x==rhs.x && lhs.y==rhs.y){ return 1; } return 0;/* your comparison code goes here */}
void translate(polygon &p1 ,int x, int y){
for(int i=0;i<p1.p.size();i++){ p1.p[i].x+=x; p1.p[i].y+=y; }
}
void rotation(polygon &p1, double p){ point center = getcenter(p1); double r = p;
for(int i=0;i<p1.p.size();i++){ point p = p1.p[i]; p1.p[i].x = (p.x - center.x) * cos(r) - (p.y-center.y) * sin (r) + center.x; p1.p[i].y = (p.x - center.x) * sin(r) + (p.y - center.y) * cos(r) +center.y; }}
void scaling(polygon &p1, double r){point center = getcenter(p1);
for(int i=0;i<p1.p.size();i++){point p = p1.p[i];p1.p[i].x = (p.x - center.x) *r + center.x; p1.p[i].y = (p.y - center.y) * r +center.y;}}
point getcenter(polygon p1){
double area=0; point centroid;int i=0; centroid.x = centroid.y=0; for(i=0;i<p1.p.size()-1;i++){ int x1 = rond(p1.p[i].x); int x2 = rond(p1.p[i+1].x); int y1 = rond(p1.p[i].y); int y2 = rond(p1.p[i+1].y); int a = x1*y2-x2*y1; area+=a;
centroid.x +=(x1+x2)*a; centroid.y +=(y1+y2)*a; } int x1 = rond(p1.p[i].x); int x2 = rond(p1.p[0].x); int y1 = rond(p1.p[i].y); int y2 = rond(p1.p[0].y); int a = x1*y2-x2*y1; area+=a; centroid.x +=(x1+x2)*a; centroid.y +=(y1+y2)*a; area *= 0.5; centroid.x /= (6.0*area); centroid.y /= (6.0*area); centroid.x = rond(centroid.x); centroid.y = rond(centroid.y);
return centroid;}
void write(){ofstream os("2.txt");os<<k<<endl;for(int i=0;i<k;i++){os<<arr[i].p.size()<<endl;for(int j=0;j<arr[i].p.size();j++){os<<arr[i].p[j].x<<‘ ‘<<arr[i].p[j].y<<endl;}os<<endl;}}
int main(int argc, char **argv) {ifstream myfile("1.txt");
myfile >> k;
for (int i = 0; i < k; i++) {int n;myfile >> n;for (int j = 0; j < n; j++) {double d1, d2;myfile >> d1 >> d2;point temp(d1,d2);arr[i].p.push_back(temp);}
}
info();
grid_width = 500;grid_height = 500;
pixel_size = 1;
win_height = grid_height*pixel_size;win_width = grid_width*pixel_size;
glutInit(&argc,argv);glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(win_width,win_height);
glutCreateWindow("glut demo");glutDisplayFunc(display2); //rendering calls hereglutReshapeFunc(reshape); //update GL on window size changeglutMouseFunc(mouse); //mouse button eventsglutMotionFunc(motion); //mouse movement eventsglutKeyboardFunc(key); //Keyboard eventsglutIdleFunc(idle); //Function called while program is sitting "idle" for(int i=0;i<k;i++){ //clipping(200,300,200,300,arr[i]); }
//initialize opengl variables init(); //start glut event loopglutMainLoop();
return 0;
}
标签:idt mode inux iso struct ras main void single
原文地址:http://www.cnblogs.com/wsggb123/p/7679983.html