Advertisement
Niftl

Untitled

May 4th, 2024 (edited)
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <stdio.h>
  5.  
  6. int WINDOW_WIDTH = 800;
  7. int WINDOW_HEIGHT = 600;
  8. int numVertices = 5; // Initial number of vertices for the polygon
  9. int pointX = 400;    // Initial x-coordinate of the point
  10. int pointY = 300;    // Initial y-coordinate of the point
  11. typedef struct {
  12.     int x, y;
  13. } Point;
  14. Point *polygonVertices = NULL; // Dynamic array for storing polygon vertices
  15.  
  16. // Function to generate initial polygon vertices
  17. void initPolygonVertices() {
  18.     polygonVertices = realloc(polygonVertices, numVertices * sizeof(Point));
  19.     for (int i = 0; i < numVertices; ++i) {
  20.         polygonVertices[i].x = rand() % WINDOW_WIDTH;
  21.         polygonVertices[i].y = rand() % WINDOW_HEIGHT;
  22.     }
  23. }
  24.  
  25. // Function to draw a polygon
  26. void drawPolygon() {
  27.     glColor3f(0.0, 0.5, 0.8); // Set color for the polygon (light blue)
  28.     glBegin(GL_POLYGON);
  29.     for (int i = 0; i < numVertices; ++i) {
  30.         glVertex2f(polygonVertices[i].x, polygonVertices[i].y);
  31.     }
  32.     glEnd();
  33. }
  34.  
  35. // Function to draw a point
  36. void drawPoint() {
  37.     glColor3f(1.0, 0.0, 0.0); // Set color for the point (red)
  38.     glPointSize(5); // Set point size for better visibility
  39.     glBegin(GL_POINTS);
  40.     glVertex2f(pointX, pointY);
  41.     glEnd();
  42. }
  43.  
  44. // Check if point is inside polygon using ray-casting algorithm
  45. int isPointInsidePolygon(int x, int y) {
  46.     int inside = 0;
  47.     int n = numVertices;
  48.     for (int i = 0, j = n - 1; i < n; j = i++) {
  49.         int intersect = ((polygonVertices[i].y > y) != (polygonVertices[j].y > y)) &&
  50.                         (x < (polygonVertices[j].x - polygonVertices[i].x) * (y - polygonVertices[i].y) / (polygonVertices[j].y - polygonVertices[i].y) + polygonVertices[i].x);
  51.         if (intersect) {
  52.             inside = !inside;
  53.         }
  54.     }
  55.     return inside;
  56. }
  57.  
  58. // Function to handle window drawing
  59. void display() {
  60.     glClear(GL_COLOR_BUFFER_BIT);
  61.    
  62.     // Draw the polygon
  63.     drawPolygon();
  64.    
  65.     // Draw the point
  66.     drawPoint();
  67.  
  68.     // Check if point is inside polygon and print the result
  69.     if (isPointInsidePolygon(pointX, pointY)) {
  70.         printf("The point is inside the polygon.\n");
  71.     } else {
  72.         printf("The point is outside the polygon.\n");
  73.     }
  74.    
  75.     glFlush();
  76. }
  77.  
  78. // Function to handle window resizing
  79. void reshape(int w, int h) {
  80.     glViewport(0, 0, w, h);
  81.     glMatrixMode(GL_PROJECTION);
  82.     glLoadIdentity();
  83.     gluOrtho2D(0, w, 0, h);
  84.     glMatrixMode(GL_MODELVIEW);
  85.     WINDOW_WIDTH = w;
  86.     WINDOW_HEIGHT = h;
  87. }
  88.  
  89. // Function to handle keyboard input
  90. void keyboard(unsigned char key, int x, int y) {
  91.     switch (key) {
  92.         case '+':
  93.             numVertices++;
  94.             if (numVertices > 2) {
  95.                 initPolygonVertices(); // Regenerate polygon vertices
  96.                 glutPostRedisplay(); // Redraw window
  97.             }
  98.             break;
  99.         case '-':
  100.             if (numVertices > 3) {
  101.                 numVertices--;
  102.                 initPolygonVertices(); // Regenerate polygon vertices
  103.                 glutPostRedisplay(); // Redraw window
  104.             }
  105.             break;
  106.         default:
  107.             break;
  108.     }
  109. }
  110.  
  111. // Function to handle mouse input (move the point)
  112. void mouse(int button, int state, int x, int y) {
  113.     if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  114.         pointX = x;
  115.         pointY = WINDOW_HEIGHT - y; // Invert y-coordinate to match OpenGL convention
  116.         glutPostRedisplay(); // Redraw window
  117.     }
  118. }
  119.  
  120. int main(int argc, char** argv) {
  121.     srand(time(NULL)); // Seed random number generator
  122.     initPolygonVertices(); // Initialize polygon vertices
  123.  
  124.     glutInit(&argc, argv);
  125.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  126.     glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  127.     glutCreateWindow("Polygon and Point in C");
  128.  
  129.     glutDisplayFunc(display);
  130.     glutReshapeFunc(reshape);
  131.  
  132.     glutKeyboardFunc(keyboard);
  133.     glutMouseFunc(mouse);
  134.  
  135.     glClearColor(1.0, 1.0, 1.0, 1.0); // Set background color to white
  136.  
  137.     glutMainLoop();
  138.  
  139.     return 0;
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement