Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <time.h>
- #include <vector>
- int WINDOW_WIDTH = 800;
- int WINDOW_HEIGHT = 600;
- int numVertices = 5; // Initial number of vertices for the polygon
- int pointX = 400; // Initial x-coordinate of the point
- int pointY = 300; // Initial y-coordinate of the point
- std::vector<std::pair<int, int>> polygonVertices; // Store polygon vertices
- // Function to generate initial polygon vertices
- void initPolygonVertices() {
- polygonVertices.clear();
- for (int i = 0; i < numVertices; ++i) {
- polygonVertices.push_back({rand() % WINDOW_WIDTH, rand() % WINDOW_HEIGHT});
- }
- }
- // Function to draw a polygon
- void drawPolygon() {
- glColor3f(0.0, 0.5, 0.8); // Set color for the polygon (light blue)
- glBegin(GL_POLYGON);
- for (auto &vertex : polygonVertices) {
- glVertex2f(vertex.first, vertex.second);
- }
- glEnd();
- }
- // Function to draw a point
- void drawPoint() {
- glColor3f(1.0, 0.0, 0.0); // Set color for the point (red)
- glPointSize(5); // Set point size for better visibility
- glBegin(GL_POINTS);
- glVertex2f(pointX, pointY);
- glEnd();
- }
- // Function to handle window drawing
- void display() {
- glClear(GL_COLOR_BUFFER_BIT);
- // Draw the polygon
- drawPolygon();
- // Draw the point
- drawPoint();
- glFlush();
- }
- // Function to handle window resizing
- void reshape(int w, int h) {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0, w, 0, h);
- glMatrixMode(GL_MODELVIEW);
- WINDOW_WIDTH = w;
- WINDOW_HEIGHT = h;
- }
- // Function to handle keyboard input
- void keyboard(unsigned char key, int x, int y) {
- switch (key) {
- case '+':
- numVertices++;
- if (numVertices > 2) {
- initPolygonVertices(); // Regenerate polygon vertices
- glutPostRedisplay(); // Redraw window
- }
- break;
- case '-':
- if (numVertices > 3) { // Ensure at least 3 vertices
- numVertices--;
- initPolygonVertices(); // Regenerate polygon vertices
- glutPostRedisplay(); // Redraw window
- }
- break;
- default:
- break;
- }
- }
- // Function to handle mouse input (move the point)
- void mouse(int button, int state, int x, int y) {
- if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
- pointX = x;
- pointY = WINDOW_HEIGHT - y; // Invert y-coordinate to match OpenGL convention
- glutPostRedisplay(); // Redraw window
- }
- }
- int main(int argc, char** argv) {
- srand(time(NULL)); // Seed random number generator
- initPolygonVertices(); // Initialize polygon vertices
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
- glutCreateWindow("Polygon and Point");
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(keyboard);
- glutMouseFunc(mouse);
- glClearColor(1.0, 1.0, 1.0, 1.0); // Set background color to white
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement