前院女生吧 关注:9贴子:2,077
  • 3回复贴,共1

【前院女生】剪裁

只看楼主收藏回复

void DDA_line(int x0, int y0, int x1, int y1,int color)
 {
int  dx = x1 – x0, dy = y1 – y0, k;
float xIncrement , yIncrement ,steps, x = x0, y = y0;
if (fabs (dx) > fabs (dy))   steps = fabs (dx);
else steps = fabs (dy);
xIncrement   = (float) (dx) /steps; 
yIncrement = (float) (dy) /steps;
For (k =0; k<steps; k++){
Putpixel(round(x), round(y),color);
x += xIncrement;  y += yIncrement;
}


void display(void)
{
   glClear(GL_COLOR_BUFFER_BIT);   
   glFlush();   
}
void init()
{
    glClearColor(0.0f,0.0f,1.0f,1.0f);
}

void main(int argc, char** argv)
{
   glutInit(&argc, argv);    
   glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);   
   glutCreateWindow(“hello”);  
   glutDisplayFunc(display);  
     init();
   glutMainLoop();   
}


1楼2007-11-07 13:54回复
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <windows.h>
    #include <gl/glut.h>

    #define N 1000 //maximum line numbers

    int ww,hh; // for display window width and height
    int line[N][4], k=0; //for line's endpoint coordinates and line number
    bool flag=true; // judge start point or end point for the line ot the clipping box
    int draw=1; // judge draw lines or draw box;
    int bx1=0,by1=0,bx2=0,by2=0; //for clipping box's size point
     int lift=1;
     int right=2; 
    int bottom=4;
    int top=8;

    void line_clipping(int x1,int y1,int x2,int y2,int xw_xmin,int yw_ymin,int xw_max,int yw_max);
    int get_code(int xxx,int yyy,int xw_xmin,int yw_ymin,int xw_max,int yw_max);
    void Myinit(void)
    {
    glClearColor(0.0,0.0,0.0,0.0);
    glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    glLineWidth(2.0);
    }


    //鼠标...............................................................................................................................
    void myMouse(int button,int state,int x,int y)
    {

    if (draw==1) //drawlines case
    {
    if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
    {
     if (flag)
     {
     // get the line's start point
     line[k][0]=x;
     line[k][1]=hh-y;
     flag=!flag;
     }
    }

    if(button==GLUT_LEFT_BUTTON&&state==GLUT_UP)
    {
    if (!flag)
    {
    //draw=3;
    // get the line's end point , count lines's number
    line[k][2]=x;
    line[k][3]=hh-y;
    k++;
    flag=!flag;
    }
    glutPostRedisplay();
     }
    }

    if (draw==2) //drawbox case
    {
    if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN)
    {
     if (flag)
     {
     // get the box's start point
     bx1=x;
     by1=hh-y;
     flag=!flag;
     }
    }

    if(button==GLUT_LEFT_BUTTON&&state==GLUT_UP)
    {
    if (!flag)
    {
    // get the box's another point

    bx2=x;
    by2=hh-y;draw=3;
    flag=!flag;
    }
    glutPostRedisplay();
     }
    }
    }

    //-----------------获得鼠标移动中的坐标-----------------------------------------------------------
    void myMotion(int x,int y)
    {
      if (draw==1) //get the line's motion point
    {
    line[k][2]=x;
    line[k][3]=hh-y;
    }
      if (draw==2) //get the box's motion point
    {
    bx2=x;
    by2=hh-y;
    }
    glutPostRedisplay();
    }


    void drawlines()
    {
    for(int i=0;i<=k;i++) //********
    {
    glBegin(GL_LINES);
    glVertex2f(line[i][0],line[i][1]);
    glVertex2f(line[i][2],line[i][3]);
    glEnd();
    }
    }
    void drawlines1()
    {
    for(int i=0;i<=k;i++) //********
    {
    line_clipping(line[i][0],line[i][1],line[i][2],line[i][3],bx1,by1,bx2,by2);
    }
    }

    void drawbox()
    {
     glRectf(bx1,by1,bx2,by2);
    }


    void Keyboard(unsigned char key, int x, int y)
    {
     switch(key)
     {
     case '1':
     { draw=1;
     for(int l=0;l<=k;l++)
     {
     line[l][0]=0;
     line[l][1]=0;
     line[l][2]=0;
     line[l][3]=0;
     bx1=0;by1=0;bx2=0;by2=0;
     }
     k=0;glClear(GL_COLOR_BUFFER_BIT);
     break; }  
     case '2':
     draw=2;
     break;
     case 27:
     exit(0);
    default:
     return;
     }
     glutPostRedisplay();
     }

    //画图---------------------------------------------------------------------------------------------------
    void Display(void)
    {
    glClear(GL_COLOR_BUFFER_BIT);
    if (draw==1)
    drawlines();
    if (draw==2)
    


    2楼2007-11-07 14:30
    回复
      2025-11-12 17:11:16
      广告
      不感兴趣
      开通SVIP免广告
      {
      drawlines();
      drawbox();//drawlines1(); 
      }
      if (draw==3)
      {

      drawbox();drawlines1(); 
      }
       glutSwapBuffers();
      }

      //-----------------------------------------------
      void Reshape(int w, int h)
      {
       glMatrixMode(GL_PROJECTION);
       glLoadIdentity(); 
       glViewport(0, 0, w, h);
       gluOrtho2D(0, w, 0, h); 
       ww=w;
       hh=h;
      }

      int main(int argc, char** argv)
      {
       glutInit(&argc, argv);
       glutInitWindowPosition(0, 0);
       glutInitWindowSize(800, 600);
       glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
       glutCreateWindow("Clip coordinates");
       Myinit();
       
       glutDisplayFunc(Display);
       glutMouseFunc(myMouse);
       glutMotionFunc(myMotion);

       glutKeyboardFunc(Keyboard);
       glutReshapeFunc(Reshape);
       glutMainLoop();
       
       return 0;
      }

      /**
      直线裁剪算法C语言版
      */
      void line_clipping(int x1,int y1,int x2,int y2,int xw_xmin,int yw_ymin,int xw_max,int yw_max)
      {
      int xxx,yyy;
      bool draws=FALSE,done=FALSE;
      int code1=0x00;
      int code2=0x00;
      int code=0x00;
      int changes;
      if(xw_xmin>xw_max) {changes=xw_xmin;xw_xmin=xw_max;xw_max=changes;}
      if(yw_ymin>yw_max) {changes=yw_ymin;yw_ymin=yw_max;yw_max=changes;}
      code1=get_code(x1,y1,xw_xmin,yw_ymin,xw_max,yw_max);
      code2=get_code(x2,y2,xw_xmin,yw_ymin,xw_max,yw_max);
      while(!done)
      {
      if(code1==0 && code2==0) {draws=true;done=true;}
      else if((code1 & code2)!=0) done=true;
      else 
      {
      if(code1!=0) code=code1;
      else code=code2;
      if((code & top)!=0)
      {
      yyy=yw_max;
      xxx=x1+(yyy-y1)*(x2-x1)/(y2-y1);
      }
      else if((code & bottom)!=0)
      {
      yyy=yw_ymin;
      xxx=x1+(yyy-y1)*(x2-x1)/(y2-y1);
      }
      else if((code & right)!=0)
      {
      xxx=xw_max;
      yyy=y1+(xxx-x1)*(y2-y1)/(x2-x1);
      }
      else if((code & lift)!=0)
      {
      xxx=xw_xmin;
      yyy=y1+(xxx-x1)*(y2-y1)/(x2-x1);
      }
      if(code==code1)
      {
      x1=xxx;
      y1=yyy;
      code1=get_code(x1,y1,xw_xmin,yw_ymin,xw_max,yw_max);
      }
      else
      {
      x2=xxx;y2=yyy;
      code2=get_code(x2,y2,xw_xmin,yw_ymin,xw_max,yw_max);
      }
      }
      if(draws)
      { glBegin(GL_LINES);
       glVertex2f(x1,y1);
        glVertex2f(x2,y2);
        glEnd();
      }
      }
      }

      int get_code(int x,int y,int xw_xmin,int yw_ymin,int xw_max,int yw_max)
      {
      int code=0;
      if(y>yw_max)
      code=code|top;
      else if(y<yw_ymin)
      code=code|bottom;
      if(x>xw_max)
      code|=right;
      else if(x<xw_xmin)
      code|=lift;
      return code;
      }


      3楼2007-11-07 14:30
      回复
        void Display(void) 里的大括号去掉还是可以运行的


        4楼2010-06-26 11:03
        回复