-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive.cpp
More file actions
102 lines (88 loc) · 2.9 KB
/
Copy pathinteractive.cpp
File metadata and controls
102 lines (88 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <GL/glut.h>
#include <math.h>
// Global variables for background color
float red = 1.0, green = 1.0, blue = 1.0;
// Stores mouse position
float mouseX = 0.0, mouseY = 0.0;
int shape = 0; // 1 = Circle, 2 = Square
// Function to draw a circle at (x, y)
void drawCircle(float x, float y, float radius) {
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y); // Center
for (int i = 0; i <= 100; i++) {
float theta = 2.0f * 3.1415926f * float(i) / 100;
float dx = radius * cos(theta);
float dy = radius * sin(theta);
glVertex2f(x + dx, y + dy);
}
glEnd();
}
// Function to draw a square at (x, y)
void drawSquare(float x, float y, float size) {
glBegin(GL_QUADS);
glVertex2f(x - size, y - size);
glVertex2f(x + size, y - size);
glVertex2f(x + size, y + size);
glVertex2f(x - size, y + size);
glEnd();
}
// Display function
void display() {
glClearColor(red, green, blue, 1.0); // Set background color
glClear(GL_COLOR_BUFFER_BIT);
// Draw selected shape at mouse position
if (shape == 1) {
glColor3f(1.0, 0.0, 0.0); // Red Circle
drawCircle(mouseX, mouseY, 0.1);
} else if (shape == 2) {
glColor3f(0.0, 0.0, 1.0); // Blue Square
drawSquare(mouseX, mouseY, 0.1);
}
glFlush();
}
// Keyboard interaction function
void handleKeypress(unsigned char key, int x, int y) {
switch (key) {
case 'r': // Change background to Red
red = 1.0; green = 0.0; blue = 0.0;
break;
case 'g': // Change background to Green
red = 0.0; green = 1.0; blue = 0.0;
break;
case 'b': // Change background to Blue
red = 0.0; green = 0.0; blue = 1.0;
break;
case 27: // Press ESC to exit
exit(0);
}
glutPostRedisplay(); // Update the screen
}
// Mouse interaction function
void handleMouse(int button, int state, int x, int y) {
if (state == GLUT_DOWN) { // When a button is clicked
mouseX = (x - 250) / 250.0; // Convert to OpenGL coordinates
mouseY = -(y - 250) / 250.0;
if (button == GLUT_LEFT_BUTTON) {
shape = 1; // Left Click -> Draw a Circle
} else if (button == GLUT_RIGHT_BUTTON) {
shape = 2; // Right Click -> Draw a Square
} else if (button == GLUT_MIDDLE_BUTTON) {
shape = 0; // Middle Click -> Clear the shape
}
}
glutPostRedisplay(); // Update the screen
}
// Main function
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Keyboard & Mouse Interaction");
glClearColor(1.0, 1.0, 1.0, 1.0); // White background
gluOrtho2D(-1, 1, -1, 1); // Set coordinate system
glutDisplayFunc(display);
glutKeyboardFunc(handleKeypress);
glutMouseFunc(handleMouse);
glutMainLoop();
return 0;
}