计算机图形学:窗口画线

来源:互联网 发布:js导出excel兼容ie11 编辑:程序博客网 时间:2024/05/22 17:09
  计算机图形学:窗口画线
#include<gl/glut.h>
#include<stdio.h>
class Point
{
private:
       double x,y;
public:
       void operator =(const Point a)
       {
              x=a.x;
              y=a.y;
       }
       double getX()
       {
              return x;
       }
       double getY()
       {
              return y;
       }
       void setX(double a)
       {
              x=a;
       }
       void setY(double a)
       {
              y=a;
       }
       void setXY(double a,double b)
       {
              x=a;
              y=b;
       }
};
Point pt1,pt2;
int PointCount=0;
bool OnePoint=false,TwoPoint=false;
double Xl=50,Xr=350,Yt=50,Yb=250;
 
void mouseEvent(int button,int state,int x,int y);
void DisplayEvent();
void CodeClip(Point,Point);
void init(void)
{
     glClearColor(1.0,1.0,1.0,0.0);
        glClear(GL_COLOR_BUFFER_BIT);
        glFlush();
        glutSwapBuffers();
}
 
 
void resizeEvent(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
   
    gluOrtho2D(0, w, h, 0);
    glMatrixMode(GL_MODELVIEW);
    glutPostRedisplay();
}
void DrawLine(Point a,Point b)
{
       //printf("DrawLine????:/n");
       //printf("x1=%f,y1=%f x2=%f,y2=%f ",a.getX(),a.getY(),b.getX(),b.getY());
     glColor3f(0.0,0.0,0.0);
     glBegin(GL_LINES);
            glVertex2d(a.getX(),a.getY());
         glVertex2d(b.getX(),b.getY());
     glEnd();                            printf("pt1.x=%f,pt1.y=%f ",pt1.getX(),pt1.getY());
        CodeClip(pt1,pt2);
}
 
void DrawPolygon()
{                                     //printf("DrawPolygon?? ");
       Point a1,a2,a3,a4;
       a1.setXY(Xl,Yt);
       a2.setXY(Xr,Yt);
       a3.setXY(Xr,Yb);
       a4.setXY(Xl,Yb);
 
       glColor3f(0,0,1);
       glBegin(GL_LINES);
        glVertex2f(a1.getX(),a1.getY());
        glVertex2f(a2.getX(),a2.getY());
        
        glVertex2f(a2.getX(),a2.getY());
        glVertex2f(a3.getX(),a3.getY());
 
        glVertex2f(a3.getX(),a3.getY());
        glVertex2f(a4.getX(),a4.getY());
 
        glVertex2f(a4.getX(),a4.getY());
        glVertex2f(a1.getX(),a1.getY());
   glEnd();
}
 
double PointCode(Point a)
{                                             //printf("PointCode?? ");
       if(a.getX()==Xl||a.getX()==Xr)
              if(a.getY()>=Yt&&a.getY()<=Yb)
                     return 0;//0x00;
              else if(a.getY()<Yt)
                     return 8;
              else if(a.getY()>Yb)
                     return 4;
       if(a.getY()==Yt||a.getY()==Yb)
              if(a.getX()<=Xr&&a.getY()>=Xl)
                     return 0;//0x00;
       if(a.getX()<Xl)
              if(a.getY()<=Yt)
                     return 9;//0x09;
              else if(a.getY()>Yt&&a.getY()<Yb)
                     return 1;//0x01;
              else if(a.getY()>=Yb)
                     return 5;//0x05;
              else
                     ;
       else if(a.getX()>Xl&&a.getX()<Xr)
              if(a.getY()<Yt)
                     return 8;//0x08;
              else if(a.getY()>Yt&&a.getY()<Yb)
                     return 0;//0x00;
              else if(a.getY()>Yb)
                     return 4;//0x04;
              else
                     ;
       else if(a.getX()>Xr)
              if(a.getY()<=Yt)
                     return 10;//0x0A;
              else if(a.getY()>Yt&&a.getY()<Yb)
                     return 2;//0x02;
              else if(a.getY()>=Yb)
                     return 6;//0x06;
              else
                     ;
}
 
void CodeClip(Point a1,Point a2)
{                                             printf("CodeClip??   ");
                                              printf("a1.x=%f,a1.y=%f ",a1.getX(),a1.getY());
       int done=0,accept=0,reject=0;
       double c1,c2;
       double x1,y1,x2,y2;
 
       x1=a1.getX(); y1=a1.getY();
       x2=a2.getX(); y2=a2.getY();
       c1=PointCode(a1);                         printf("c1=%f ",c1);
       c2=PointCode(a2);                       printf("c2=%f ",c2);
       while(done==0)
       {
              if((int)c1&(int)c2)
              {                            printf("!!!!!!!!!!!!!!!!!!!!!!reject=1 ");
                     reject=1;
                     done=1;
              }
              if(((int)c1&(int)c2)==0)
              {                                    //printf("???0 ");
                     if(c1==0&&c2==0)
                     {
                            done=1;
                            accept=1;
                     }
                     else if(c1==0)
                     {                               printf("a1.x=%f,a1.y=%f ",a1.getX(),a1.getY());
                            Point pt=a1;
                            a1=a2;
                            a2=pt;
                            double t=c1;
                            c1=c2;
                            c2=t;  
                            x1=a1.getX();y1=a1.getY();      printf("a1.x=%f,a1.y=%f ",a1.getX(),a1.getY());
                            x2=a2.getX();y2=a2.getY();      printf("c1=%f,c2=%f ",c1,c2);
                     }
                     else
                     {                              printf("???? ");
                         if(a1.getX()<Xl)     //a1??????
                            {
                                   a1.setXY(Xl,(y2-y1)*(Xl-x1)/(x2-x1)+y1);//y1-(x1-Xl)*(y1-y2)/(x1-x2));
                                   c1=PointCode(a1);
                            }
                            if(a1.getX()>Xr)    //?
                            {
                                   a1.setXY(Xr,(y2-y1)*(Xr-x1)/(x2-x1)+y1);
                                   c1=PointCode(a1);
                            }
                            if(a1.getY()<Yt)    //?
                            {
                                   a1.setXY((Yt-y1)*(x2-x1)/(y2-y1)+x1,Yt);
                                   c1=PointCode(a1);
                            }
                            if(a1.getY()>Yb)   //?
                            {
                                   a1.setXY((Yb-y1)*(x2-x1)/(y2-y1)+x1,Yb);
                                   c1=PointCode(a1);
                            }
                     }
              }
       }
       if(accept==1)
       {                                 printf("accept==1 ");
              glColor3f(0,1,0);
              glBegin(GL_LINES);
               glVertex2f(a1.getX(),a1.getY());
               glVertex2f(a2.getX(),a2.getY());
              glEnd();
       }
 
}
 
void DrawPointPolygon(Point a)
{                                           //printf("DrawPointPolygon??: ");
       Point b1,b2,b3,b4;
       b1.setXY(a.getX()-5,a.getY()+5);
       b2.setXY(a.getX()-5,a.getY()-5);
       b3.setXY(a.getX()+5,a.getY()-5);
       b4.setXY(a.getX()+5,a.getY()+5);
       glColor3f(0.0,0.0,1.0);
       glBegin(GL_LINES);
           glVertex2f(b1.getX(),b1.getY());
              glVertex2f(b2.getX(),b2.getY());
 
              glVertex2f(b2.getX(),b2.getY());
              glVertex2f(b3.getX(),b3.getY());
 
              glVertex2f(b3.getX(),b3.getY());
              glVertex2f(b4.getX(),b4.getY());
 
              glVertex2f(b4.getX(),b4.getY());
              glVertex2f(b1.getX(),b1.getY());
       glEnd();
}
 
 
void DrawPointCross(Point a)
{                                       //printf("DrawPointCross??: ");
       Point b1,b2,b3,b4;
       b1.setXY(a.getX()-6,a.getY());
       b2.setXY(a.getX()+6,a.getY());
       b3.setXY(a.getX(),a.getY()+6);
       b4.setXY(a.getX(),a.getY()-6);
       glColor3f(1.0,0.0,0.0);
       glBegin(GL_LINES);
           glVertex2f(b1.getX(),b1.getY());
              glVertex2f(b2.getX(),b2.getY());
 
              glVertex2f(b3.getX(),b3.getY());
              glVertex2f(b4.getX(),b4.getY());
       glEnd();
}
 
 
void mouseMoveEvent(int x,int y)
{
       if(OnePoint==true)
       {                                                 //printf("mouseMoveEvent?? ");
              pt2.setXY((double)x,(double)y);
              TwoPoint=true;
              glutPostRedisplay();
       }
}
 
 
 
   
 
int main(int argc, char *argv[])
{
       //??windows??
       glutInit(&argc, argv);
       glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);
       glutInitWindowSize(400, 300);
       glutInitWindowPosition(200, 200);
       glutCreateWindow("Line 2D");
 
    glutReshapeFunc(resizeEvent);
       glutDisplayFunc(DisplayEvent);
       glutMouseFunc(mouseEvent);
       glutMotionFunc(mouseMoveEvent);
      
       glutMainLoop();
      
       return 0;
}
void mouseEvent(int button,int state,int x,int y)
{
       TwoPoint=false;
       if(PointCount%2==0)
       {                                        //printf("pointcount=0,PointCount=%d   ",PointCount);
              if(button==GLUT_LEFT_BUTTON)
              {                                    //printf("button=GLUT_LEFT_BUTTON   ");
            if(state==GLUT_DOWN)
                     {  
 
                            pt1.setX((double)x);
                            pt1.setY((double)y);
 
 
                         glClearColor(1.0,1.0,1.0,0.0);
                            glClear(GL_COLOR_BUFFER_BIT);
                            glColor3f(1.0,0.0,0.0);
                            glBegin(GL_POINTS);
                                glVertex2f(pt1.getX(),pt1.getY());
                            glEnd();
                            PointCount++;
                            OnePoint=true;
                     }
              }
       }
       else
       {                                         //printf("pointcount=1,PointCount=%d ",PointCount);
              if(button==GLUT_LEFT_BUTTON)
              {                                     //printf("button=GLUT_LEFT_BUTTON   ");    
            if(state==GLUT_UP)
                     {
                            pt2.setX((double)x);
                            pt2.setY((double)y);
                            PointCount++;
                            OnePoint=false;
                            TwoPoint=true;             // printf("x2=%f,y2=%f ",pt2.getX(),pt2.getY());
                     }
              }
       }
       if(TwoPoint==true)
              DisplayEvent();
}
void DisplayEvent()
{                                          // printf("DisplayEvent??: ");
       glClearColor(1.0,1.0,1.0,0.0);
       glClear(GL_COLOR_BUFFER_BIT);
       DrawPolygon();
       if(TwoPoint==true)
       {                                       //printf("TwoPoint==true ");
              DrawPointCross(pt1);
              DrawPointPolygon(pt2);
              DrawLine(pt1,pt2);
       }
       glFlush();
       glutSwapBuffers();
}
 
 
原创粉丝点击