forked from Manan07Ag/LogicLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
217 lines (175 loc) · 5.59 KB
/
test.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <GL/glut.h>
#include <cmath>
#include <iostream>
const int GRID_SIZE = 10; // 10x10 grid
class Grid {
public:
Grid(int size) : size(size) {}
void draw() const {
glColor3f(0.5f, 0.5f, 0.5f); // Set grid color to grey
for (int i = 0; i <= size; ++i) {
float x = -1.0f + i * cellWidth();
float y = -1.0f + i * cellHeight();
// Draw vertical line
glBegin(GL_LINES);
glVertex2f(x, -1.0f);
glVertex2f(x, 1.0f);
glEnd();
// Draw horizontal line
glBegin(GL_LINES);
glVertex2f(-1.0f, y);
glVertex2f(1.0f, y);
glEnd();
}
}
float cellWidth() const { return 2.0f / size; }
float cellHeight() const { return 2.0f / size; }
private:
int size;
};
class NANDGate {
public:
NANDGate(float width, float height)
: width(width), height(height) {}
void draw() const {
glColor3f(1.0f, 1.0f, 1.0f);
// Rectangle
glBegin(GL_LINE_LOOP);
glVertex2f(-width/2.0f, -height/2.0f);
glVertex2f(width/2.0f, -height/2.0f);
glVertex2f(width/2.0f, height/2.0f);
glVertex2f(-width/2.0f, height/2.0f);
glEnd();
// Lines to the left
glBegin(GL_LINES);
glVertex2f(-width, height/4.0f);
glVertex2f(-width/2.0f, height/4.0f);
glVertex2f(-width, -height/4.0f);
glVertex2f(-width/2.0f, -height/4.0f);
glEnd();
// Semi-circle
glBegin(GL_LINE_LOOP);
for (int i = 270; i <= 450; ++i) {
float angle = static_cast<float>(i) * M_PI / 180.0;
float x = width/2.0f * cos(angle);
float y = height/2.0f * sin(angle);
glVertex2f(width/2.0f + x, y);
}
glEnd();
// Line attached to the right side
glBegin(GL_LINES);
glVertex2f(width, 0.0f);
glVertex2f(width + width/10.0f, 0.0f);
glEnd();
}
float getWidth() const {
return width;
}
float getHeight() const {
return height;
}
private:
float width;
float height;
};
class ORGate {
public:
ORGate(float width, float height)
: width(width), height(height) {}
void draw() const {
glColor3f(1.0f, 1.0f, 1.0f);
// Draw the main body of the OR gate
glBegin(GL_POLYGON);
glVertex2f(-width / 2.0f, 0.0f); // Left point
glVertex2f(0.0f, height / 2.0f); // Top point
glVertex2f(width / 2.0f, 0.0f); // Right point
glVertex2f(0.0f, -height / 2.0f); // Bottom point
glEnd();
// Draw the input lines
glBegin(GL_LINES);
glVertex2f(-width / 2.0f, 0.05 * height);
glVertex2f(-0.15 * width, 0.05 * height);
glVertex2f(-width / 2.0f, -0.05 * height);
glVertex2f(-0.15 * width, -0.05 * height);
glEnd();
// Draw the output line
glBegin(GL_LINES);
glVertex2f(0.15 * width, 0.0f);
glVertex2f(width / 2.0f, 0.0f);
glEnd();
}
float getWidth() const {
return width;
}
float getHeight() const {
return height;
}
private:
float width; // Width of the OR gate
float height; // Height of the OR gate
};
// Define coordinates for gate positions and connections
int nandXPos = 6;
int nandYPos = 5;
int orXPos = 3;
int orYPos = 3;
void display() {
glClear(GL_COLOR_BUFFER_BIT);
Grid grid(GRID_SIZE);
grid.draw();
// Draw NAND Gate in its cell
float nandGateWidth = 0.2; // Set the NAND gate width
float nandGateHeight = 0.2; // Set the NAND gate height
glPushMatrix();
float nandTranslateX = -1.0f + nandXPos * grid.cellWidth() + grid.cellWidth() / 2.0f;
float nandTranslateY = 1.0f - nandYPos * grid.cellHeight() - grid.cellHeight() / 2.0f;
glTranslatef(nandTranslateX, nandTranslateY, 0.0f);
NANDGate nandGate(nandGateWidth, nandGateHeight);
nandGate.draw();
glPopMatrix();
// Draw OR Gate in its cell
float orGateWidth = 0.2; // Set the OR gate width
float orGateHeight = 0.2; // Set the OR gate height
glPushMatrix();
float orTranslateX = -1.0f + orXPos * grid.cellWidth() + grid.cellWidth() / 2.0f;
float orTranslateY = 1.0f - orYPos * grid.cellHeight() - grid.cellHeight() / 2.0f;
glTranslatef(orTranslateX, orTranslateY, 0.0f);
ORGate orGate(orGateWidth, orGateHeight);
orGate.draw();
glPopMatrix();
// Draw the connection line between NAND and OR gates
float nandOutputX = nandTranslateX + nandGateWidth / 2.0f;
float nandOutputY = nandTranslateY;
float orInputX = orTranslateX - orGateWidth / 2.0f;
float orInputY = orTranslateY;
glColor3f(1.0f, 1.0f, 1.0f); // Set the line color to white
glBegin(GL_LINES);
glVertex2f(nandOutputX, nandOutputY);
glVertex2f(orInputX, orInputY);
glEnd();
glFlush();
}
void reshape(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
float aspect = static_cast<float>(width) / static_cast<float>(height);
if (width >= height) {
glOrtho(-aspect, aspect, -1.0, 1.0, -1.0, 1.0);
} else {
glOrtho(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect, -1.0, 1.0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(100, 100);
glutCreateWindow("OpenGL Gates");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}