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 <stdio.h>
- 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
- typedef struct {
- int x, y;
- } Point;
- Point *polygonVertices = NULL; // Dynamic array for storing polygon vertices
- // Function to generate initial polygon vertices
- void initPolygonVertices() {
- polygonVertices = realloc(polygonVertices, numVertices * sizeof(Point));
- for (int i = 0; i < numVertices; ++i) {
- polygonVertices[i].x = rand() % WINDOW_WIDTH;
- polygonVertices[i].y = 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 (int i = 0; i < numVertices; ++i) {
- glVertex2f(polygonVertices[i].x, polygonVertices[i].y);
- }
- 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();
- }
- // Check if point is inside polygon using ray-casting algorithm
- int isPointInsidePolygon(int x, int y) {
- int inside = 0;
- int n = numVertices;
- for (int i = 0, j = n - 1; i < n; j = i++) {
- int intersect = ((polygonVertices[i].y > y) != (polygonVertices[j].y > y)) &&
- (x < (polygonVertices[j].x - polygonVertices[i].x) * (y - polygonVertices[i].y) / (polygonVertices[j].y - polygonVertices[i].y) + polygonVertices[i].x);
- if (intersect) {
- inside = !inside;
- }
- }
- return inside;
- }
- // Function to handle window drawing
- void display() {
- glClear(GL_COLOR_BUFFER_BIT);
- // Draw the polygon
- drawPolygon();
- // Draw the point
- drawPoint();
- // Check if point is inside polygon and print the result
- if (isPointInsidePolygon(pointX, pointY)) {
- printf("The point is inside the polygon.\n");
- } else {
- printf("The point is outside the polygon.\n");
- }
- 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) {
- 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 in C");
- 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