Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <iostream>
- #define KCLR "\x1B[0m"
- #define KRED "\x1B[31m"
- #define KGRN "\x1B[32m"
- struct coords{
- int x, y;
- };
- void pointDistance (coords prev, coords next, coords &edges);
- int xProd(coords prev, coords next);
- bool isConcave(int[], int size);
- void display();
- int main(int argc,char *argv[]){
- int nrVertices= 6;
- int xpSum [nrVertices];
- coords edgeDist [nrVertices];
- coords vertices [nrVertices]= {{0,0},{1,0},{2,1},{3,0},{3,3},{0,3}};
- fprintf(stderr, "\t#Calculating Edge distance#\n");
- //calculate point distance for adjacent edges
- for (size_t ctr= 0; ctr<nrVertices; ++ctr){
- //compare last with first
- if (ctr == nrVertices-1){
- pointDistance(vertices[ctr], vertices[0], edgeDist[ctr]);
- fprintf(stderr, "E%d= (%2d,%2d)\n", ctr+1, edgeDist[ctr].x, edgeDist[ctr].y);
- continue;
- }
- pointDistance(vertices[ctr], vertices[ctr+1], edgeDist[ctr]);
- fprintf(stderr, "E%d= (%2d,%2d)\n", ctr+1, edgeDist[ctr].x, edgeDist[ctr].y);
- }
- fprintf(stderr, "\n\n\t#Calculating Cross Product#\n");
- //calculate cross-product of adjacent edges
- for (size_t ctr= 0; ctr<nrVertices; ++ctr){
- //compare last with first
- if (ctr == nrVertices-1){
- xpSum [ctr]= xProd(edgeDist[ctr], edgeDist[0]);
- fprintf(stderr, "xp%d: %2d\n", ctr+1, xpSum[ctr]);
- continue;
- }
- xpSum [ctr]= xProd(edgeDist[ctr], edgeDist[ctr+1]);
- fprintf(stderr, "xp%d: %2d\n", ctr+1, xpSum[ctr]);
- }
- //determine if polygon is complex using cross-product results
- fprintf(stderr, "\n\n\t#Cross Product is:\n");
- if (isConcave(&xpSum[0], nrVertices))
- fprintf(stderr, "\n\n\t/!\\ The polygon is CONCAVE");
- else
- fprintf(stderr, "\n\n\t/!\\ The polygon is CONVEX");
- //draw the polygon
- glutInit(&argc,argv); //prepare glut
- glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); //set the display mode
- glutInitWindowSize (500, 500); //the size of the program window
- glutInitWindowPosition (100, 100); //initial position for the window
- glutCreateWindow ("points and lines"); //create window with a title
- glClearColor(255,255,255,0.0); //background color
- glMatrixMode (GL_PROJECTION); //2d envyronment
- gluOrtho2D (-10.0, 310.0, -10.0, 310.0); //canvas area (LRBT)
- glutDisplayFunc(display); //show thee actual program
- glutMainLoop();
- return EXIT_SUCCESS; //exit the program
- }
- //calculate the distance between vertices: V= (x2-x1, y2-y1)
- void pointDistance (coords prev, coords next, coords &edges){
- //calculate point distance
- edges.x= next.x - prev.x;
- edges.y= next.y - prev.y;
- }
- //determine cross-product: |V1 * V2| = (x1*y2) - (y1*x2)
- int xProd (coords prev, coords next){
- return (prev.x * next.y) - (prev.y * next.x);
- }
- //determine if polygon is concave usign cross-product results
- bool isConcave(int xpSum[], int size){
- bool hasPos;
- bool hasNeg;
- for (size_t ctr= 0; ctr<size; ++ctr){
- if (xpSum[ctr] > 0){
- hasPos= 1;
- fprintf(stderr, "Positive\n");
- } else{
- hasNeg= 1;
- fprintf(stderr, "Negative\n");
- }
- }
- if (hasPos && hasNeg) return 1;
- return 0;
- }
- void display(){
- glClear(GL_COLOR_BUFFER_BIT); //flush gl
- glColor3f(0.0, 0.0, 1.0); //blue line color
- //outer pentagram, clockwise
- glBegin(GL_LINE_LOOP);
- glVertex2i(0,0);
- glVertex2i(100,0);
- glVertex2i(200,100);
- glVertex2i(300,0);
- glVertex2i(300,300);
- glVertex2i(0,300);
- glEnd();
- glFlush(); //force immidiate display
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement