Advertisement
Niftl

Untitled

May 4th, 2024
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.18 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <vector>
  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. std::vector<std::pair<int, int>> polygonVertices; // Store polygon vertices
  12.  
  13. // Function to generate initial polygon vertices
  14. void initPolygonVertices() {
  15.     polygonVertices.clear();
  16.     for (int i = 0; i < numVertices; ++i) {
  17.         polygonVertices.push_back({rand() % WINDOW_WIDTH, rand() % WINDOW_HEIGHT});
  18.     }
  19. }
  20.  
  21. // Function to draw a polygon
  22. void drawPolygon() {
  23.     glColor3f(0.0, 0.5, 0.8); // Set color for the polygon (light blue)
  24.     glBegin(GL_POLYGON);
  25.     for (auto &vertex : polygonVertices) {
  26.         glVertex2f(vertex.first, vertex.second);
  27.     }
  28.     glEnd();
  29. }
  30.  
  31. // Function to draw a point
  32. void drawPoint() {
  33.     glColor3f(1.0, 0.0, 0.0); // Set color for the point (red)
  34.     glPointSize(5); // Set point size for better visibility
  35.     glBegin(GL_POINTS);
  36.     glVertex2f(pointX, pointY);
  37.     glEnd();
  38. }
  39.  
  40. // Function to handle window drawing
  41. void display() {
  42.     glClear(GL_COLOR_BUFFER_BIT);
  43.    
  44.     // Draw the polygon
  45.     drawPolygon();
  46.    
  47.     // Draw the point
  48.     drawPoint();
  49.    
  50.     glFlush();
  51. }
  52.  
  53. // Function to handle window resizing
  54. void reshape(int w, int h) {
  55.     glViewport(0, 0, w, h);
  56.     glMatrixMode(GL_PROJECTION);
  57.     glLoadIdentity();
  58.     gluOrtho2D(0, w, 0, h);
  59.     glMatrixMode(GL_MODELVIEW);
  60.     WINDOW_WIDTH = w;
  61.     WINDOW_HEIGHT = h;
  62. }
  63.  
  64. // Function to handle keyboard input
  65. void keyboard(unsigned char key, int x, int y) {
  66.     switch (key) {
  67.         case '+':
  68.             numVertices++;
  69.             if (numVertices > 2) {
  70.                 initPolygonVertices(); // Regenerate polygon vertices
  71.                 glutPostRedisplay(); // Redraw window
  72.             }
  73.             break;
  74.         case '-':
  75.             if (numVertices > 3) { // Ensure at least 3 vertices
  76.                 numVertices--;
  77.                 initPolygonVertices(); // Regenerate polygon vertices
  78.                 glutPostRedisplay(); // Redraw window
  79.             }
  80.             break;
  81.         default:
  82.             break;
  83.     }
  84. }
  85.  
  86. // Function to handle mouse input (move the point)
  87. void mouse(int button, int state, int x, int y) {
  88.     if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
  89.         pointX = x;
  90.         pointY = WINDOW_HEIGHT - y; // Invert y-coordinate to match OpenGL convention
  91.         glutPostRedisplay(); // Redraw window
  92.     }
  93. }
  94.  
  95. int main(int argc, char** argv) {
  96.     srand(time(NULL)); // Seed random number generator
  97.     initPolygonVertices(); // Initialize polygon vertices
  98.  
  99.     glutInit(&argc, argv);
  100.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  101.     glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  102.     glutCreateWindow("Polygon and Point");
  103.    
  104.     glutDisplayFunc(display);
  105.     glutReshapeFunc(reshape);
  106.    
  107.     glutKeyboardFunc(keyboard);
  108.     glutMouseFunc(mouse);
  109.    
  110.     glClearColor(1.0, 1.0, 1.0, 1.0); // Set background color to white
  111.    
  112.     glutMainLoop();
  113.    
  114.     return 0;
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement