Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.30 KB | None | 0 0
  1. /*
  2.  * GLUT Shapes Demo
  3.  *
  4.  * Written by Nigel Stewart November 2003
  5.  *
  6.  * This program is test harness for the sphere, cone
  7.  * and torus shapes in GLUT.
  8.  *
  9.  * Spinning wireframe and smooth shaded shapes are
  10.  * displayed until the ESC or q key is pressed.  The
  11.  * number of geometry stacks and slices can be adjusted
  12.  * using the + and - keys.
  13.  */
  14.  
  15. #ifdef __APPLE__
  16. #include <GLUT/glut.h>
  17. #else
  18. #include <GL/glut.h>
  19. #endif
  20.  
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23.  
  24. static int slices = 16;
  25. static int stacks = 16;
  26.  
  27. /* GLUT callback Handlers */
  28.  
  29. static void resize(int width, int height)
  30. {
  31.     const float ar = (float) width / (float) height;
  32.     glViewport(0, 0, width, height);
  33.     glMatrixMode(GL_PROJECTION);
  34.     glLoadIdentity();
  35.     glOrtho(-320, 319, -240, 239, -1, 1);
  36.     glMatrixMode(GL_MODELVIEW);
  37.     glLoadIdentity() ;
  38. }
  39.  
  40. void draw8waySyemmtry(int x,int y,int zone)
  41. {
  42.  
  43.     if(zone==0)
  44.         glVertex2i(x,y);
  45.     else if(zone==1)
  46.         glVertex2i(y,x);
  47.     else if(zone==2)
  48.         glVertex2i(-y,x);
  49.     else if(zone==3)
  50.         glVertex2i(-x,y);
  51.     else if(zone==4)
  52.         glVertex2i(-x,-y);
  53.     else if(zone==5)
  54.         glVertex2i(-y,-x);
  55.     else if(zone==6)
  56.         glVertex2i(y,-x);
  57.     else if(zone==7)
  58.         glVertex2i(x,-y);
  59.  
  60. }
  61.  
  62. int cx0,cx1,cy0,cy1,cnt=0;
  63.  
  64.  
  65.  
  66. void mouse(int button,int state,int x,int y)
  67. {
  68.     if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
  69.     {
  70.         printf("%d %d\n",x,y);
  71.         if(cnt==0)
  72.         {
  73.             cx0=x-320;
  74.             cy0=-y+240;
  75.  
  76.             cx1 = cx0;
  77.             cy1 = cy0;
  78.  
  79.             cnt=1;
  80.         }
  81.         else if(cnt==1)
  82.         {
  83.             cx1=x-320;
  84.             cy1=-y+240;
  85.             cnt=2;
  86.             printf("%d %d %d %d\n",cx0,cy0,cx1,cy1);
  87.             int dx=cx1-cx0,dy=cy1-cy0;
  88.             if(dx>=0 && dy>=0)
  89.             {
  90.                 if(abs(dx)>abs(dy))
  91.                 {
  92.                     printf("Zone 0, ");
  93.                     printf("COLOR = Blue\n");
  94.                 }
  95.                 else
  96.                 {
  97.                     printf("Zone 1, ");
  98.                     printf("COLOR = Green\n");
  99.                 }
  100.             }
  101.             else if(dx<=0 && dy>=0)
  102.             {
  103.                 if(abs(dx)>abs(dy))
  104.                 {
  105.                     printf("Zone 3, ");
  106.                     printf("COLOR = Red\n");
  107.                 }
  108.                 else
  109.                 {
  110.                     printf("Zone 2, ");
  111.                     printf("COLOR = Yellow\n");
  112.                 }
  113.             }
  114.             else if(dx<=0 && dy<=0)
  115.             {
  116.                 if(abs(dx)>abs(dy))
  117.                 {
  118.                     printf("Zone 4, ");
  119.                     printf("COLOR = White\n");
  120.                 }
  121.                 else
  122.                 {
  123.                     printf("Zone 5, ");
  124.                     printf("COLOR = Orange\n");
  125.                 }
  126.             }
  127.             else if(dx>=0 && dy<=0)
  128.             {
  129.                 if(abs(dx)>abs(dy))
  130.                 {
  131.                     printf("Zone 7, ");
  132.                     printf("Color = Thin Green\n");
  133.                 }
  134.                 else
  135.                 {
  136.                     printf("Zone 6, ");
  137.                     printf("Color = Sky Blue\n");
  138.                 }
  139.             }
  140.             //lineDraw(cx0,cy0,cx1,cy1);
  141.  
  142.         }
  143.  
  144.     }
  145. }
  146.  
  147. void drawLine_0(int x0,int y0,int x1,int y1,int zone)
  148. {
  149.     int dx=x1-x0,dy=y1-y0;
  150.     int x,y,d=2*dy-dx;
  151.     int dE=2*dy,dNE=2*(dy-dx);
  152.     x=x0,y=y0;
  153.     draw8waySyemmtry(x,y,zone);
  154.     while(!(x==x1 && y==y1))
  155.     {
  156.         if(d<0)
  157.         {
  158.             x++;
  159.             d+=dE;
  160.         }
  161.         else
  162.         {
  163.             x++,y++;
  164.             d+=dNE;
  165.         }
  166.         draw8waySyemmtry(x,y,zone);
  167.     }
  168. }
  169.  
  170.  
  171.  
  172. void lineDraw(int x0,int y0,int x1,int y1)
  173. {
  174.     int dx=x1-x0,dy=y1-y0;
  175.     if(dx>=0 && dy>=0)
  176.     {
  177.         if(abs(dx)>abs(dy))
  178.         {
  179.             glColor3d(0,0,1);
  180.             drawLine_0(x0,y0,x1,y1,0);
  181.  
  182.         }
  183.         else
  184.         {
  185.             glColor3d(0,1,0);
  186.             drawLine_0(y0,x0,y1,x1,1);
  187.         }
  188.     }
  189.     else if(dx<=0 && dy>=0)
  190.     {
  191.         if(abs(dx)>abs(dy))
  192.         {
  193.             glColor3d(1,0,0);
  194.             drawLine_0(-x0,y0,-x1,y1,3);
  195.         }
  196.         else
  197.         {
  198.             glColor3d(1,1,0);
  199.             drawLine_0(y0,-x0,y1,-x1,2);
  200.         }
  201.     }
  202.     else if(dx<=0 && dy<=0)
  203.     {
  204.         if(abs(dx)>abs(dy))
  205.         {
  206.             glColor3d(1,1,1);
  207.             drawLine_0(-x0,-y0,-x1,-y1,4);
  208.         }
  209.         else
  210.         {
  211.             glColor3d(1,.5,0);
  212.             drawLine_0(-y0,-x0,-y1,-x1,5);
  213.         }
  214.     }
  215.     else if(dx>=0 && dy<=0)
  216.     {
  217.         if(abs(dx)>abs(dy))
  218.         {
  219.             glColor3d(0,.5,0);
  220.             drawLine_0(x0,-y0,x1,-y1,7);
  221.         }
  222.         else
  223.         {
  224.             glColor3d(0,.5,.5);
  225.             drawLine_0(-y0,x0,-y1,x1,6);
  226.         }
  227.     }
  228. }
  229.  
  230. static void display(void)
  231. {
  232.     int x = 10, y = 20;
  233.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  234.     glColor3d(1,1,1);
  235.     glBegin(GL_LINES);
  236.     glVertex2i(-320, 0);
  237.     glVertex2i(319, 0);
  238.     glVertex2i(0, -240);
  239.     glVertex2i(0, 239);
  240.     glEnd();
  241.     glBegin(GL_POINTS);
  242.     //glColor3d(.4,.6,.8);
  243.     if(cnt == 2){
  244.         cnt = 0;
  245.     }
  246.     else{
  247.         lineDraw(cx0,cy0,cx1,cy1);
  248.     }
  249.     glEnd();
  250.     glutSwapBuffers();
  251. }
  252.  
  253.  
  254. static void key(unsigned char key, int x, int y)
  255. {
  256.     switch (key)
  257.     {
  258.     case 27 :
  259.     case 'q':
  260.         exit(0);
  261.         break;
  262.  
  263.     case '+':
  264.         slices++;
  265.         stacks++;
  266.         break;
  267.  
  268.     case '-':
  269.         if (slices>3 && stacks>3)
  270.         {
  271.             slices--;
  272.             stacks--;
  273.         }
  274.         break;
  275.     }
  276.  
  277.     glutPostRedisplay();
  278. }
  279.  
  280. static void idle(void)
  281. {
  282.     glutPostRedisplay();
  283. }
  284.  
  285. /* Program entry point */
  286.  
  287. int main(int argc, char *argv[])
  288. {
  289.     glutInit(&argc, argv);
  290.     glutInitWindowSize(640,480);
  291.     glutInitWindowPosition(10,10);
  292.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  293.  
  294.     glutCreateWindow("Experiment 01");
  295.  
  296.     glutReshapeFunc(resize);
  297.     glutDisplayFunc(display);
  298.     glutKeyboardFunc(key);
  299.     glutMouseFunc(mouse);
  300.     glutIdleFunc(idle);
  301.  
  302.     glutMainLoop();
  303.  
  304.     return EXIT_SUCCESS;
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement