-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.cpp
More file actions
executable file
·307 lines (279 loc) · 8.49 KB
/
Graphics.cpp
File metadata and controls
executable file
·307 lines (279 loc) · 8.49 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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
Made with ❤ by srinSkit.
Created on 17 April 2018.
*/
#include "Graphics.h"
#include <GL/gl.h>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include "Common.h"
#include <stack>
#include <vector>
#include <algorithm>
#define RADIAN(x) ((x)*M_PI/180.0)
using namespace std;
#define SAFETY_PAD 10
int rangeRandom(int low, int high) {
return rand() % (high - low + 1) + low;
}
void screenToWorld(int &x, int &y) {
y = static_cast<int>((float) worldHeight * (screenHeight - y) / screenHeight);
x = static_cast<int>((float) worldWidth * (x - (float) (screenWidth - screenHeight) / 2) / screenHeight);
limitX(x);
}
void screenToGrid(int &x, int &y) {
screenToWorld(x, y);
x = x / cellWidth;
y = y / cellHeight;
}
void worldToGrid(int &x, int &y) {
x = x / cellWidth;
y = y / cellHeight;
}
void circle(int xc, int yc, int radius, bool fill) {
int x = 0, y = radius, p = 5 / 4 - radius;
int inc = 1 + 2 * x, dec = 2 * y;
int count = 0;
auto buff = new GLint[((int) (radius * M_SQRT2) + SAFETY_PAD) * 4];
while (x <= y) {
buff[count++] = x;
buff[count++] = y;
buff[count++] = x;
buff[count++] = -y;
if (p >= 0) {
--y;
dec -= 2;
p -= dec;
}
++x;
inc += 2;
p += inc;
}
count /= 4;
glPushMatrix();
glTranslatef(xc, yc, 0);
auto fig = fill ? GL_LINES : GL_POINTS;
for (int i = 0; i < 4; ++i) {
glRotated(i * 90, 0, 0, 1);
glVertexPointer(2, GL_INT, 2 * sizeof(GLint), buff);
glDrawArrays(fig, 0, 2 * count);
}
glPopMatrix();
}
void gridToWorld(int &x, int &y) {
x = x * cellWidth + cellWidth / 2;
y = y * cellHeight + cellHeight / 2;
}
stack<vector<vector<double>> *> matrixStack;
vector<vector<double>> *matrix = nullptr;
void loadGraphicsMod() {
matrix = new vector<vector<double>>(3, vector<double>(3));
for (int i = 0; i < matrix->size(); ++i)
for (int j = 0; j < (*matrix)[i].size(); ++j)
(*matrix)[i][j] = (i == j ? 1 : 0);
}
void unloadGraphicsMod() {
delete (matrix);
while (!matrixStack.empty()) {
delete (matrixStack.top());
matrixStack.pop();
}
}
void myPushMatrix() {
matrixStack.push(matrix);
auto tmp = matrix;
matrix = new vector<vector<double>>(3, vector<double>(3));
for (int i = 0; i < matrix->size(); ++i)
for (int j = 0; j < (*matrix)[i].size(); ++j)
(*matrix)[i][j] = (*tmp)[i][j];
}
void myPopMatrix() {
if (matrixStack.empty())
return;
delete (matrix);
matrix = (vector<vector<double>> *) matrixStack.top();
matrixStack.pop();
}
void multiplyToMatrix(const vector<vector<double>> &mat) {
auto tm = vector<vector<double>>(3, vector<double>(3, 0));
if (matrix == nullptr)return;
for (int i = 0; i < tm.size(); ++i)
for (int j = 0; j < tm[i].size(); ++j)
for (int k = 0; k < mat.size(); ++k)
tm[i][j] += (mat[i][k] * ((*matrix)[k][j]));
for (int i = 0; i < tm.size(); ++i)
for (int j = 0; j < tm[i].size(); ++j)
(*matrix)[i][j] = tm[i][j];
}
void myTranslatef(int x, int y, int z) {
auto tm = vector<vector<double>>(3, vector<double>(3));
tm[0][2] = x;
tm[1][2] = y;
tm[1][1] = tm[0][0] = tm[2][2] = 1;
multiplyToMatrix(tm);
}
void fillPoly(int n, int v[][2]) {
struct EdgeBucket {
int yMax, yMin, sign, dx, dy, rem;
float x;
};
EdgeBucket edgeTable[n];
int m = 0;
for (int i = 0; i < n; ++i) {
int i1 = i;
int i2 = (i + 1) % n;
if (v[i1][1] != v[i2][1]) {
edgeTable[m].yMax = max(v[i1][1], v[i2][1]);
edgeTable[m].yMin = min(v[i1][1], v[i2][1]);
edgeTable[m].rem = edgeTable[m].yMax - edgeTable[m].yMin;
edgeTable[m].x = v[i1][1] < v[i2][1] ? v[i1][0] : v[i2][0];
if (edgeTable[m].yMax == v[i2][1])
edgeTable[m].sign = (v[i2][0] - v[i1][0]) < 0 ? -1 : 1;
else
edgeTable[m].sign = (v[i2][0] - v[i1][0]) <= 0 ? 1 : -1;
edgeTable[m].dy = abs(v[i1][1] - v[i2][1]);
edgeTable[m].dx = abs(v[i1][0] - v[i2][0]);
m++;
}
}
sort(edgeTable, edgeTable + m, [](EdgeBucket &e1, EdgeBucket &e2) {
return e1.yMin < e2.yMin;
});
int activeList[n];
for (int y = edgeTable[0].yMin; true; ++y) {
int j = 0;
for (int i = 0; i < m; ++i)
if (edgeTable[i].yMin <= y && edgeTable[i].rem > 0)
activeList[j++] = i;
if (j == 0)
break;
sort(activeList, activeList + j, [&edgeTable](int &e1, int &e2) {
return edgeTable[e1].x < edgeTable[e2].x;
});
for (int k = 0; k < j; k += 2) {
int e1 = activeList[k];
int e2 = activeList[k + 1];
drawLineBresenham((int) round(edgeTable[e1].x), y, (int) round(edgeTable[e2].x), y);
edgeTable[e1].x += edgeTable[e1].sign * (float) edgeTable[e1].dx / edgeTable[e1].dy;
edgeTable[e2].x += edgeTable[e2].sign * (float) edgeTable[e2].dx / edgeTable[e2].dy;
edgeTable[e1].rem--;
edgeTable[e2].rem--;
}
}
}
void drawLineBresenham(int x11, int y11, int x21, int y21) {
int x1 = x11, x2 = x21, y1 = y11, y2 = y21;
bool draw_xy = true;
if (y1 != y2) {
float m = (float) (y2 - y1) / (x2 - x1);
if (y2 > y1) {
// oct 1
if (0 <= m && m <= 1) {
}
// oct 2 & 3
else if (m > 1 || m < -1) {
swap(x1, y1);
swap(x2, y2);
draw_xy = false;
}
// oct 4
else if (m > -1) {
swap(x1, x2);
swap(y1, y2);
}
} else {
// oct 5
if (0 <= m && m <= 1) {
swap(x1, x2);
swap(y1, y2);
}
// oct 6 & 7
else if (m > 1 || m < -1) {
swap(x1, x2);
swap(y1, y2);
swap(x1, y1);
swap(x2, y2);
draw_xy = false;
}
// oct 8
else if (m > -1) {
}
}
} else {
x1 = min(x11, x21);
x2 = max(x11, x21);
}
int dx = abs(x2 - x1), dy = abs(y2 - y1);
int p = 2 * dy - dx;
int x = x1, y = y1;
int dec = 2 * dx, inc = 2 * dy, i = 0;
auto *vertices = new GLint[2 * (abs(dx) + 2)];
bool inc_y = y < y2;
if (y1 != y2) {
while (x <= x2) {
vertices[i] = draw_xy ? x : y;
vertices[i + 1] = draw_xy ? y : x;
i += 2;
++x;
if (p >= 0) {
y = y + (inc_y ? 1 : -1);
p -= dec;
}
p += inc;
}
} else
while (x <= x2) {
vertices[i] = x;
vertices[i + 1] = y;
++x;
i += 2;
}
glVertexPointer(2, GL_INT, 2 * sizeof(GLint), vertices);
glDrawArrays(GL_POINTS, 0, i / 2);
delete (vertices);
}
GLsizei stride;
void *ptr;
void myVertexPointer(int size, GLenum type, GLsizei stride, void *ptr) {
::stride = stride;
::ptr = ptr;
}
#define MAX_COUNT 100
int vvv[MAX_COUNT][2];
void myDrawArrays(GLenum mode, GLint first, GLsizei count) {
auto tmp = vector<vector<int>>(3, vector<int>(count, 1));
auto tm = vector<vector<double >>(3, vector<double>(count, 0));
for (int vi = 0; vi < count; ++vi) {
auto arr = (GLint *) ((char *) ptr + vi * stride);
tmp[0][vi] = arr[0];
tmp[1][vi] = arr[1];
}
for (int i = 0; i < tm.size(); ++i)
for (int j = 0; j < tm[i].size(); ++j)
for (int k = 0; k < matrix->size(); ++k)
tm[i][j] += (((*matrix)[i][k]) * tmp[k][j]);
for (int i = 0; i < count; ++i) {
vvv[i][0] = static_cast<int>(tm[0][i]);
vvv[i][1] = static_cast<int>(tm[1][i]);
}
switch (mode) {
case GL_POLYGON:
fillPoly(count, vvv);
break;
case GL_LINES:
for (int i = 0; i < count; i += 2)
drawLineBresenham(vvv[i][0], vvv[i][1], vvv[i + 1][0], vvv[i + 1][1]);
break;
default:
break;
}
}
void myRotated(int angle, int x, int y, int z) {
auto tm = vector<vector<double>>(3, vector<double>(3, 0));
tm[0][0] = tm[1][1] = cos(RADIAN(angle));
tm[0][1] = -sin(RADIAN(angle));
tm[1][0] = -tm[0][1];
tm[2][2] = 1;
multiplyToMatrix(tm);
}