forked from Manan07Ag/LogicLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminor.cpp
153 lines (124 loc) · 4.09 KB
/
minor.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <GL/glut.h>
#include <cmath>
class InteractiveLogicCircuit {
public:
InteractiveLogicCircuit() : windowWidth(400), windowHeight(400), dragging(0), rectX(0.0), rectY(-0.9), rectWidth(0.1), boxInSpace(false) {
instance = this;
}
void run(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutCreateWindow("Interactive Logic Circuit");
glutDisplayFunc(displayCallback);
glutReshapeFunc(reshapeCallback);
glutMouseFunc(mouseCallback);
glutMotionFunc(motionCallback);
glutMainLoop();
}
private:
int windowWidth;
int windowHeight;
int dragging;
float rectX;
float rectY;
float rectWidth;
bool boxInSpace;
static InteractiveLogicCircuit* instance;
static void displayCallback() {
instance->display();
}
static void reshapeCallback(int width, int height) {
instance->reshape(width, height);
}
static void mouseCallback(int button, int state, int x, int y) {
instance->mouse(button, state, x, y);
}
static void motionCallback(int x, int y) {
instance->motion(x, y);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glLineWidth(2.0);
glBegin(GL_LINES);
glVertex2f(-0.5, 0.0);
glVertex2f(0.5, 0.0);
glEnd();
float squareSize = 0.2;
glBegin(GL_QUADS);
glVertex2f(-squareSize, 0.1);
glVertex2f(squareSize, 0.1);
glVertex2f(squareSize, -0.1);
glVertex2f(-squareSize, -0.1);
glEnd();
glColor3f(1.0, 0.0, 0.0);
float minX = -1.0 + rectWidth;
float maxX = 1.0 - rectWidth;
float minY = -1.0;
float maxY = 1.0;
if (rectX < minX) rectX = minX;
if (rectX > maxX) rectX = maxX;
if (rectY < minY) rectY = minY;
if (rectY > maxY) rectY = maxY;
if (rectX >= -0.5 && rectX <= 0.5 && rectY >= -0.1 && rectY <= 0.1) {
boxInSpace = true;
} else {
boxInSpace = false;
}
glBegin(GL_QUADS);
glVertex2f(rectX - rectWidth, rectY + 0.05);
glVertex2f(rectX + rectWidth, rectY + 0.05);
glVertex2f(rectX + rectWidth, rectY - 0.05);
glVertex2f(rectX - rectWidth, rectY - 0.05);
glEnd();
if (boxInSpace) {
glColor3f(1.0, 1.0, 0.0);
} else {
glColor3f(1.0, 1.0, 1.0);
}
float circleRadius = 0.05;
glBegin(GL_TRIANGLE_FAN);
for (int i = 0; i < 360; i++) {
float angle = i * 3.1415926 / 180;
float x = 0.7 + circleRadius * cos(angle);
float y = 0.0 + circleRadius * sin(angle);
glVertex2f(x, y);
}
glEnd();
glFlush();
}
void reshape(int width, int height) {
windowWidth = width;
windowHeight = height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
float mouseX = (float)x / windowWidth * 2.0 - 1.0;
float mouseY = 1.0 - (float)y / windowHeight * 2.0;
if (mouseY >= rectY - 0.05 && mouseY <= rectY + 0.05 && mouseX >= rectX - rectWidth && mouseX <= rectX + rectWidth) {
dragging = 1;
}
} else if (button == GLUT_LEFT_BUTTON && state == GLUT_UP) {
dragging = 0;
}
}
void motion(int x, int y) {
if (dragging) {
rectX = (float)x / windowWidth * 2.0 - 1.0;
rectY = 1.0 - (float)y / windowHeight * 2.0;
glutPostRedisplay();
}
}
};
InteractiveLogicCircuit* InteractiveLogicCircuit::instance = nullptr;
int main(int argc, char** argv) {
InteractiveLogicCircuit app;
app.run(argc, argv);
return 0;
}