Advertisement
axyd

lab_6

Mar 7th, 2019
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.72 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <stdlib.h>
  3. #include <iostream>
  4.  
  5. #define KCLR  "\x1B[0m"
  6. #define KRED  "\x1B[31m"
  7. #define KGRN  "\x1B[32m"
  8.  
  9.  
  10. struct coords{
  11.     int x, y;
  12. };
  13.  
  14. void pointDistance (coords prev, coords next, coords &edges);
  15. int xProd(coords prev, coords next);
  16. bool isConcave(int[], int size);
  17. void display();
  18.  
  19. int main(int argc,char *argv[]){
  20.     int nrVertices= 6;
  21.     int xpSum [nrVertices];
  22.     coords edgeDist [nrVertices];  
  23.     coords vertices [nrVertices]= {{0,0},{1,0},{2,1},{3,0},{3,3},{0,3}};
  24.    
  25.     fprintf(stderr, "\t#Calculating Edge distance#\n");
  26.    
  27.     //calculate point distance for adjacent edges
  28.     for (size_t ctr= 0; ctr<nrVertices; ++ctr){
  29.         //compare last with first
  30.         if (ctr == nrVertices-1){
  31.            pointDistance(vertices[ctr], vertices[0], edgeDist[ctr]);
  32.            fprintf(stderr, "E%d= (%2d,%2d)\n", ctr+1, edgeDist[ctr].x, edgeDist[ctr].y);
  33.            continue;  
  34.         }
  35.        
  36.         pointDistance(vertices[ctr], vertices[ctr+1], edgeDist[ctr]);
  37.         fprintf(stderr, "E%d= (%2d,%2d)\n", ctr+1, edgeDist[ctr].x, edgeDist[ctr].y);
  38.     }  
  39.    
  40.     fprintf(stderr, "\n\n\t#Calculating Cross Product#\n");
  41.    
  42.     //calculate cross-product of adjacent edges
  43.     for (size_t ctr= 0; ctr<nrVertices; ++ctr){
  44.         //compare last with first
  45.         if (ctr == nrVertices-1){      
  46.            xpSum [ctr]= xProd(edgeDist[ctr], edgeDist[0]); 
  47.            fprintf(stderr, "xp%d: %2d\n", ctr+1, xpSum[ctr]);
  48.            continue;
  49.         }
  50.  
  51.         xpSum [ctr]= xProd(edgeDist[ctr], edgeDist[ctr+1]);
  52.         fprintf(stderr, "xp%d: %2d\n", ctr+1, xpSum[ctr]);
  53.     }
  54.    
  55.     //determine if polygon is complex using cross-product results
  56.     fprintf(stderr, "\n\n\t#Cross Product is:\n");
  57.    
  58.     if (isConcave(&xpSum[0], nrVertices))
  59.         fprintf(stderr, "\n\n\t/!\\ The polygon is CONCAVE");
  60.     else
  61.         fprintf(stderr, "\n\n\t/!\\ The polygon is CONVEX");
  62.    
  63.    
  64.     //draw the polygon 
  65.     glutInit(&argc,argv);                   //prepare glut
  66.     glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);       //set the display mode
  67.     glutInitWindowSize (500, 500);          //the size of the program window
  68.     glutInitWindowPosition (100, 100);      //initial position for the window
  69.     glutCreateWindow ("points and lines");  //create window with a title
  70.    
  71.     glClearColor(255,255,255,0.0);          //background color
  72.     glMatrixMode (GL_PROJECTION);           //2d envyronment
  73.     gluOrtho2D (-10.0, 310.0, -10.0, 310.0);   //canvas area (LRBT)
  74.    
  75.     glutDisplayFunc(display);               //show thee actual program
  76.     glutMainLoop();
  77.    
  78.     return EXIT_SUCCESS;                    //exit the program
  79. }
  80.  
  81.  
  82. //calculate the distance between vertices: V= (x2-x1, y2-y1)
  83. void pointDistance (coords prev, coords next, coords &edges){
  84.     //calculate point distance
  85.     edges.x= next.x - prev.x;
  86.     edges.y= next.y - prev.y;
  87. }
  88.  
  89. //determine cross-product: |V1 * V2| = (x1*y2) - (y1*x2)
  90. int xProd (coords prev, coords next){
  91.     return (prev.x * next.y) - (prev.y * next.x);
  92. }
  93.  
  94. //determine if polygon is concave usign cross-product results
  95. bool isConcave(int xpSum[], int size){
  96.     bool hasPos;
  97.     bool hasNeg;
  98.    
  99.     for (size_t ctr= 0; ctr<size; ++ctr){
  100.         if (xpSum[ctr] > 0){
  101.             hasPos= 1;
  102.             fprintf(stderr, "Positive\n"); 
  103.         } else{
  104.             hasNeg= 1;
  105.             fprintf(stderr, "Negative\n"); 
  106.         }        
  107.     }
  108.    
  109.     if (hasPos && hasNeg) return 1;
  110.  
  111.     return 0;      
  112. }
  113.  
  114. void display(){
  115.     glClear(GL_COLOR_BUFFER_BIT);   //flush gl
  116.     glColor3f(0.0, 0.0, 1.0);       //blue line color
  117.  
  118.     //outer pentagram, clockwise
  119.     glBegin(GL_LINE_LOOP);
  120.         glVertex2i(0,0);
  121.         glVertex2i(100,0);
  122.         glVertex2i(200,100);
  123.         glVertex2i(300,0);
  124.         glVertex2i(300,300);
  125.         glVertex2i(0,300);  
  126.     glEnd();
  127.      
  128.     glFlush();  //force immidiate display
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement