ConsoleFramework 1.2.2__tar.gz → 1.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {consoleframework-1.2.2/consoletools → consoleframework-1.3.1/ConsoleFramework}/core.cpp +346 -400
- consoleframework-1.3.1/ConsoleFramework/core.cpython-313-aarch64-linux-android.so +0 -0
- {consoleframework-1.2.2 → consoleframework-1.3.1}/ConsoleFramework.egg-info/PKG-INFO +2 -2
- {consoleframework-1.2.2 → consoleframework-1.3.1}/ConsoleFramework.egg-info/SOURCES.txt +5 -3
- consoleframework-1.3.1/ConsoleFramework.egg-info/top_level.txt +1 -0
- consoleframework-1.3.1/MANIFEST.in +4 -0
- {consoleframework-1.2.2 → consoleframework-1.3.1}/PKG-INFO +2 -2
- {consoleframework-1.2.2 → consoleframework-1.3.1}/setup.py +6 -6
- consoleframework-1.2.2/ConsoleFramework.egg-info/top_level.txt +0 -1
- consoleframework-1.2.2/MANIFEST.in +0 -4
- consoleframework-1.2.2/consoletools/core.cpython-313-aarch64-linux-android.so +0 -0
- {consoleframework-1.2.2/consoletools → consoleframework-1.3.1/ConsoleFramework}/__init__.py +0 -0
- {consoleframework-1.2.2 → consoleframework-1.3.1}/ConsoleFramework.egg-info/dependency_links.txt +0 -0
- {consoleframework-1.2.2 → consoleframework-1.3.1}/ConsoleFramework.egg-info/not-zip-safe +0 -0
- {consoleframework-1.2.2 → consoleframework-1.3.1}/setup.cfg +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
|
|
2
|
+
ConsoleFramework - The best Console Library of the Python.
|
|
3
3
|
By: Suleiman
|
|
4
4
|
Copyright В© Suleiman 2026
|
|
5
5
|
All rights are reserved.
|
|
@@ -13,6 +13,8 @@ All rights are reserved.
|
|
|
13
13
|
#include <thread>
|
|
14
14
|
#include <chrono>
|
|
15
15
|
#include <sstream>
|
|
16
|
+
#include <cstring>
|
|
17
|
+
#include <ctime>
|
|
16
18
|
|
|
17
19
|
#ifdef _WIN32
|
|
18
20
|
#include <windows.h>
|
|
@@ -33,44 +35,94 @@ All rights are reserved.
|
|
|
33
35
|
|
|
34
36
|
using namespace std;
|
|
35
37
|
|
|
36
|
-
|
|
38
|
+
struct Point {
|
|
39
|
+
int x, y;
|
|
40
|
+
char ch;
|
|
41
|
+
string color;
|
|
42
|
+
string bg;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
struct Vec3 {
|
|
46
|
+
double x, y, z;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
static bool point_in_triangle(int px, int py, int x1, int y1, int x2, int y2, int x3, int y3) {
|
|
50
|
+
int d1 = (x2 - x1) * (py - y1) - (y2 - y1) * (px - x1);
|
|
51
|
+
int d2 = (x3 - x2) * (py - y2) - (y3 - y2) * (px - x2);
|
|
52
|
+
int d3 = (x1 - x3) * (py - y3) - (y1 - y3) * (px - x3);
|
|
53
|
+
bool has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0);
|
|
54
|
+
bool has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0);
|
|
55
|
+
return !(has_neg && has_pos);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static Vec3 rotx(Vec3 v, double angle) {
|
|
59
|
+
double rad = angle * M_PI / 180.0;
|
|
60
|
+
double ca = cos(rad), sa = sin(rad);
|
|
61
|
+
return {v.x, v.y * ca - v.z * sa, v.y * sa + v.z * ca};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
static Vec3 roty(Vec3 v, double angle) {
|
|
65
|
+
double rad = angle * M_PI / 180.0;
|
|
66
|
+
double ca = cos(rad), sa = sin(rad);
|
|
67
|
+
return {v.x * ca + v.z * sa, v.y, -v.x * sa + v.z * ca};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static Vec3 rotz(Vec3 v, double angle) {
|
|
71
|
+
double rad = angle * M_PI / 180.0;
|
|
72
|
+
double ca = cos(rad), sa = sin(rad);
|
|
73
|
+
return {v.x * ca - v.y * sa, v.x * sa + v.y * ca, v.z};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
static void project_3d(Vec3 v, double rx, double ry, double rz, double scale, int cx, int cy, int* ox, int* oy) {
|
|
77
|
+
v = rotx(v, rx);
|
|
78
|
+
v = roty(v, ry);
|
|
79
|
+
v = rotz(v, rz);
|
|
80
|
+
*ox = (int)(v.x * scale + cx);
|
|
81
|
+
*oy = (int)(-v.y * scale + cy);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
static PyObject* rgb(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
37
85
|
int r, g, b;
|
|
38
|
-
|
|
86
|
+
static char* kwlist[] = {"r", "g", "b", NULL};
|
|
87
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii", kwlist, &r, &g, &b))
|
|
88
|
+
return NULL;
|
|
39
89
|
char buf[32];
|
|
40
90
|
sprintf(buf, "\033[38;2;%d;%d;%dm", r, g, b);
|
|
41
91
|
return PyUnicode_FromString(buf);
|
|
42
92
|
}
|
|
43
93
|
|
|
44
|
-
static PyObject* bg_rgb(PyObject* self, PyObject* args) {
|
|
94
|
+
static PyObject* bg_rgb(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
45
95
|
int r, g, b;
|
|
46
|
-
|
|
96
|
+
static char* kwlist[] = {"r", "g", "b", NULL};
|
|
97
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii", kwlist, &r, &g, &b))
|
|
98
|
+
return NULL;
|
|
47
99
|
char buf[32];
|
|
48
100
|
sprintf(buf, "\033[48;2;%d;%d;%dm", r, g, b);
|
|
49
101
|
return PyUnicode_FromString(buf);
|
|
50
102
|
}
|
|
51
103
|
|
|
52
|
-
static PyObject* colorize(PyObject* self, PyObject* args) {
|
|
104
|
+
static PyObject* colorize(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
53
105
|
const char* text, *color;
|
|
54
106
|
int reset = 1;
|
|
55
|
-
|
|
107
|
+
static char* kwlist[] = {"text", "color", "reset", NULL};
|
|
108
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ss|i", kwlist, &text, &color, &reset))
|
|
109
|
+
return NULL;
|
|
56
110
|
string result = string(color) + text + (reset ? "\033[0m" : "");
|
|
57
111
|
return PyUnicode_FromString(result.c_str());
|
|
58
112
|
}
|
|
59
113
|
|
|
60
|
-
static PyObject* get_line_points(PyObject* self, PyObject* args) {
|
|
114
|
+
static PyObject* get_line_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
61
115
|
int x1, y1, x2, y2;
|
|
62
116
|
const char* ch = "#";
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
117
|
+
static char* kwlist[] = {"x1", "y1", "x2", "y2", "char", NULL};
|
|
118
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iiii|s", kwlist, &x1, &y1, &x2, &y2, &ch))
|
|
119
|
+
return NULL;
|
|
67
120
|
PyObject* list = PyList_New(0);
|
|
68
121
|
int dx = abs(x2 - x1);
|
|
69
122
|
int dy = abs(y2 - y1);
|
|
70
123
|
int sx = (x1 < x2) ? 1 : -1;
|
|
71
124
|
int sy = (y1 < y2) ? 1 : -1;
|
|
72
125
|
int err = dx - dy;
|
|
73
|
-
|
|
74
126
|
while (true) {
|
|
75
127
|
PyObject* tuple = PyTuple_New(3);
|
|
76
128
|
PyTuple_SetItem(tuple, 0, PyLong_FromLong(x1));
|
|
@@ -86,11 +138,12 @@ static PyObject* get_line_points(PyObject* self, PyObject* args) {
|
|
|
86
138
|
return list;
|
|
87
139
|
}
|
|
88
140
|
|
|
89
|
-
static PyObject* get_circle_points(PyObject* self, PyObject* args) {
|
|
141
|
+
static PyObject* get_circle_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
90
142
|
int cx, cy, r;
|
|
91
143
|
const char* ch = "#";
|
|
92
|
-
|
|
93
|
-
|
|
144
|
+
static char* kwlist[] = {"cx", "cy", "r", "char", NULL};
|
|
145
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii|s", kwlist, &cx, &cy, &r, &ch))
|
|
146
|
+
return NULL;
|
|
94
147
|
PyObject* list = PyList_New(0);
|
|
95
148
|
for (int angle = 0; angle < 360; angle += 5) {
|
|
96
149
|
double rad = angle * M_PI / 180.0;
|
|
@@ -106,11 +159,12 @@ static PyObject* get_circle_points(PyObject* self, PyObject* args) {
|
|
|
106
159
|
return list;
|
|
107
160
|
}
|
|
108
161
|
|
|
109
|
-
static PyObject* get_filled_circle_points(PyObject* self, PyObject* args) {
|
|
162
|
+
static PyObject* get_filled_circle_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
110
163
|
int cx, cy, r;
|
|
111
164
|
const char* ch = "#";
|
|
112
|
-
|
|
113
|
-
|
|
165
|
+
static char* kwlist[] = {"cx", "cy", "r", "char", NULL};
|
|
166
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii|s", kwlist, &cx, &cy, &r, &ch))
|
|
167
|
+
return NULL;
|
|
114
168
|
PyObject* list = PyList_New(0);
|
|
115
169
|
for (int y = cy - r; y <= cy + r; y++) {
|
|
116
170
|
for (int x = cx - r; x <= cx + r; x++) {
|
|
@@ -127,11 +181,12 @@ static PyObject* get_filled_circle_points(PyObject* self, PyObject* args) {
|
|
|
127
181
|
return list;
|
|
128
182
|
}
|
|
129
183
|
|
|
130
|
-
static PyObject* get_rect_points(PyObject* self, PyObject* args) {
|
|
184
|
+
static PyObject* get_rect_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
131
185
|
int x1, y1, x2, y2;
|
|
132
186
|
const char* ch = "#";
|
|
133
|
-
|
|
134
|
-
|
|
187
|
+
static char* kwlist[] = {"x1", "y1", "x2", "y2", "char", NULL};
|
|
188
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iiii|s", kwlist, &x1, &y1, &x2, &y2, &ch))
|
|
189
|
+
return NULL;
|
|
135
190
|
PyObject* list = PyList_New(0);
|
|
136
191
|
for (int x = x1; x <= x2; x++) {
|
|
137
192
|
PyObject* t1 = PyTuple_New(3);
|
|
@@ -164,11 +219,12 @@ static PyObject* get_rect_points(PyObject* self, PyObject* args) {
|
|
|
164
219
|
return list;
|
|
165
220
|
}
|
|
166
221
|
|
|
167
|
-
static PyObject* get_fill_rect_points(PyObject* self, PyObject* args) {
|
|
222
|
+
static PyObject* get_fill_rect_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
168
223
|
int x1, y1, x2, y2;
|
|
169
224
|
const char* ch = "#";
|
|
170
|
-
|
|
171
|
-
|
|
225
|
+
static char* kwlist[] = {"x1", "y1", "x2", "y2", "char", NULL};
|
|
226
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iiii|s", kwlist, &x1, &y1, &x2, &y2, &ch))
|
|
227
|
+
return NULL;
|
|
172
228
|
PyObject* list = PyList_New(0);
|
|
173
229
|
for (int y = y1; y <= y2; y++) {
|
|
174
230
|
for (int x = x1; x <= x2; x++) {
|
|
@@ -183,18 +239,19 @@ static PyObject* get_fill_rect_points(PyObject* self, PyObject* args) {
|
|
|
183
239
|
return list;
|
|
184
240
|
}
|
|
185
241
|
|
|
186
|
-
static PyObject* get_triangle_points(PyObject* self, PyObject* args) {
|
|
242
|
+
static PyObject* get_triangle_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
187
243
|
int x1, y1, x2, y2, x3, y3;
|
|
188
244
|
const char* ch = "#";
|
|
189
|
-
|
|
190
|
-
|
|
245
|
+
static char* kwlist[] = {"x1", "y1", "x2", "y2", "x3", "y3", "char", NULL};
|
|
246
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iiiiii|s", kwlist, &x1, &y1, &x2, &y2, &x3, &y3, &ch))
|
|
247
|
+
return NULL;
|
|
191
248
|
PyObject* list = PyList_New(0);
|
|
192
|
-
vector<pair<int,int>>
|
|
249
|
+
vector<pair<int,int>> pts;
|
|
193
250
|
int dx = abs(x2 - x1), dy = abs(y2 - y1);
|
|
194
251
|
int sx = (x1 < x2) ? 1 : -1, sy = (y1 < y2) ? 1 : -1;
|
|
195
252
|
int err = dx - dy;
|
|
196
253
|
while (true) {
|
|
197
|
-
|
|
254
|
+
pts.push_back({x1, y1});
|
|
198
255
|
if (x1 == x2 && y1 == y2) break;
|
|
199
256
|
int e2 = err * 2;
|
|
200
257
|
if (e2 > -dy) { err -= dy; x1 += sx; }
|
|
@@ -204,7 +261,7 @@ static PyObject* get_triangle_points(PyObject* self, PyObject* args) {
|
|
|
204
261
|
sx = (x2 < x3) ? 1 : -1; sy = (y2 < y3) ? 1 : -1;
|
|
205
262
|
err = dx - dy;
|
|
206
263
|
while (true) {
|
|
207
|
-
|
|
264
|
+
pts.push_back({x2, y2});
|
|
208
265
|
if (x2 == x3 && y2 == y3) break;
|
|
209
266
|
int e2 = err * 2;
|
|
210
267
|
if (e2 > -dy) { err -= dy; x2 += sx; }
|
|
@@ -214,13 +271,13 @@ static PyObject* get_triangle_points(PyObject* self, PyObject* args) {
|
|
|
214
271
|
sx = (x3 < x1) ? 1 : -1; sy = (y3 < y1) ? 1 : -1;
|
|
215
272
|
err = dx - dy;
|
|
216
273
|
while (true) {
|
|
217
|
-
|
|
274
|
+
pts.push_back({x3, y3});
|
|
218
275
|
if (x3 == x1 && y3 == y1) break;
|
|
219
276
|
int e2 = err * 2;
|
|
220
277
|
if (e2 > -dy) { err -= dy; x3 += sx; }
|
|
221
278
|
if (e2 < dx) { err += dx; y3 += sy; }
|
|
222
279
|
}
|
|
223
|
-
for (auto p :
|
|
280
|
+
for (auto p : pts) {
|
|
224
281
|
PyObject* tuple = PyTuple_New(3);
|
|
225
282
|
PyTuple_SetItem(tuple, 0, PyLong_FromLong(p.first));
|
|
226
283
|
PyTuple_SetItem(tuple, 1, PyLong_FromLong(p.second));
|
|
@@ -230,29 +287,21 @@ static PyObject* get_triangle_points(PyObject* self, PyObject* args) {
|
|
|
230
287
|
}
|
|
231
288
|
return list;
|
|
232
289
|
}
|
|
233
|
-
|
|
290
|
+
|
|
291
|
+
static PyObject* get_filled_triangle_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
234
292
|
int x1, y1, x2, y2, x3, y3;
|
|
235
293
|
const char* ch = "#";
|
|
236
|
-
|
|
237
|
-
|
|
294
|
+
static char* kwlist[] = {"x1", "y1", "x2", "y2", "x3", "y3", "char", NULL};
|
|
295
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iiiiii|s", kwlist, &x1, &y1, &x2, &y2, &x3, &y3, &ch))
|
|
296
|
+
return NULL;
|
|
238
297
|
PyObject* list = PyList_New(0);
|
|
239
298
|
int min_x = min(x1, min(x2, x3));
|
|
240
299
|
int max_x = max(x1, max(x2, x3));
|
|
241
300
|
int min_y = min(y1, min(y2, y3));
|
|
242
301
|
int max_y = max(y1, max(y2, y3));
|
|
243
|
-
|
|
244
|
-
auto sign = [](int px, int py, int ax, int ay, int bx, int by) {
|
|
245
|
-
return (px - bx) * (ay - by) - (ax - bx) * (py - by);
|
|
246
|
-
};
|
|
247
|
-
|
|
248
302
|
for (int y = min_y; y <= max_y; y++) {
|
|
249
303
|
for (int x = min_x; x <= max_x; x++) {
|
|
250
|
-
|
|
251
|
-
int d2 = sign(x, y, x2, y2, x3, y3);
|
|
252
|
-
int d3 = sign(x, y, x3, y3, x1, y1);
|
|
253
|
-
bool has_neg = (d1 < 0) || (d2 < 0) || (d3 < 0);
|
|
254
|
-
bool has_pos = (d1 > 0) || (d2 > 0) || (d3 > 0);
|
|
255
|
-
if (!(has_neg && has_pos)) {
|
|
304
|
+
if (point_in_triangle(x, y, x1, y1, x2, y2, x3, y3)) {
|
|
256
305
|
PyObject* tuple = PyTuple_New(3);
|
|
257
306
|
PyTuple_SetItem(tuple, 0, PyLong_FromLong(x));
|
|
258
307
|
PyTuple_SetItem(tuple, 1, PyLong_FromLong(y));
|
|
@@ -265,11 +314,12 @@ static PyObject* get_filled_triangle_points(PyObject* self, PyObject* args) {
|
|
|
265
314
|
return list;
|
|
266
315
|
}
|
|
267
316
|
|
|
268
|
-
static PyObject* get_heart_points(PyObject* self, PyObject* args) {
|
|
317
|
+
static PyObject* get_heart_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
269
318
|
int cx, cy, size = 5;
|
|
270
319
|
const char* ch = "#";
|
|
271
|
-
|
|
272
|
-
|
|
320
|
+
static char* kwlist[] = {"cx", "cy", "size", "char", NULL};
|
|
321
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|is", kwlist, &cx, &cy, &size, &ch))
|
|
322
|
+
return NULL;
|
|
273
323
|
PyObject* list = PyList_New(0);
|
|
274
324
|
for (int y = -size; y <= size; y++) {
|
|
275
325
|
for (int x = -size; x <= size; x++) {
|
|
@@ -287,16 +337,16 @@ static PyObject* get_heart_points(PyObject* self, PyObject* args) {
|
|
|
287
337
|
return list;
|
|
288
338
|
}
|
|
289
339
|
|
|
290
|
-
static PyObject* get_star_points(PyObject* self, PyObject* args) {
|
|
340
|
+
static PyObject* get_star_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
291
341
|
int cx, cy, radius = 5, points_count = 5;
|
|
292
342
|
const char* ch = "*";
|
|
293
|
-
|
|
294
|
-
|
|
343
|
+
static char* kwlist[] = {"cx", "cy", "radius", "points_count", "char", NULL};
|
|
344
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|iis", kwlist, &cx, &cy, &radius, &points_count, &ch))
|
|
345
|
+
return NULL;
|
|
295
346
|
PyObject* list = PyList_New(0);
|
|
296
347
|
double angle = -M_PI / 2.0;
|
|
297
348
|
double step = M_PI / points_count;
|
|
298
349
|
vector<pair<double,double>> outer, inner;
|
|
299
|
-
|
|
300
350
|
for (int i = 0; i < points_count * 2; i++) {
|
|
301
351
|
double r = (i % 2 == 0) ? radius : radius * 0.4;
|
|
302
352
|
double x = cx + r * cos(angle + i * step);
|
|
@@ -304,7 +354,6 @@ static PyObject* get_star_points(PyObject* self, PyObject* args) {
|
|
|
304
354
|
if (i % 2 == 0) outer.push_back({x, y});
|
|
305
355
|
else inner.push_back({x, y});
|
|
306
356
|
}
|
|
307
|
-
|
|
308
357
|
for (int i = 0; i < points_count; i++) {
|
|
309
358
|
int j = (i + 1) % points_count;
|
|
310
359
|
int x1 = (int)outer[i].first, y1 = (int)outer[i].second;
|
|
@@ -344,11 +393,13 @@ static PyObject* get_star_points(PyObject* self, PyObject* args) {
|
|
|
344
393
|
}
|
|
345
394
|
return list;
|
|
346
395
|
}
|
|
347
|
-
|
|
396
|
+
|
|
397
|
+
static PyObject* get_wave_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
348
398
|
int cx, cy, amplitude = 5, length = 20;
|
|
349
399
|
const char* ch = "~";
|
|
350
|
-
|
|
351
|
-
|
|
400
|
+
static char* kwlist[] = {"cx", "cy", "amplitude", "length", "char", NULL};
|
|
401
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|iis", kwlist, &cx, &cy, &litude, &length, &ch))
|
|
402
|
+
return NULL;
|
|
352
403
|
PyObject* list = PyList_New(0);
|
|
353
404
|
for (int x = 0; x < length; x++) {
|
|
354
405
|
int y = cy + (int)(amplitude * sin(x * 0.5));
|
|
@@ -362,12 +413,13 @@ static PyObject* get_wave_points(PyObject* self, PyObject* args) {
|
|
|
362
413
|
return list;
|
|
363
414
|
}
|
|
364
415
|
|
|
365
|
-
static PyObject* get_spiral_points(PyObject* self, PyObject* args) {
|
|
416
|
+
static PyObject* get_spiral_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
366
417
|
int cx, cy, turns = 3, radius = 10;
|
|
367
418
|
double step = 0.1;
|
|
368
419
|
const char* ch = "*";
|
|
369
|
-
|
|
370
|
-
|
|
420
|
+
static char* kwlist[] = {"cx", "cy", "turns", "radius", "step", "char", NULL};
|
|
421
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|iids", kwlist, &cx, &cy, &turns, &radius, &step, &ch))
|
|
422
|
+
return NULL;
|
|
371
423
|
PyObject* list = PyList_New(0);
|
|
372
424
|
double angle = 0, r = 0;
|
|
373
425
|
while (r < radius) {
|
|
@@ -385,11 +437,12 @@ static PyObject* get_spiral_points(PyObject* self, PyObject* args) {
|
|
|
385
437
|
return list;
|
|
386
438
|
}
|
|
387
439
|
|
|
388
|
-
static PyObject* get_bezier_points(PyObject* self, PyObject* args) {
|
|
440
|
+
static PyObject* get_bezier_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
389
441
|
int x1, y1, x2, y2, x3, y3, steps = 20;
|
|
390
442
|
const char* ch = "#";
|
|
391
|
-
|
|
392
|
-
|
|
443
|
+
static char* kwlist[] = {"x1", "y1", "x2", "y2", "x3", "y3", "steps", "char", NULL};
|
|
444
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iiiiii|is", kwlist, &x1, &y1, &x2, &y2, &x3, &y3, &steps, &ch))
|
|
445
|
+
return NULL;
|
|
393
446
|
PyObject* list = PyList_New(0);
|
|
394
447
|
for (int t = 0; t <= steps; t++) {
|
|
395
448
|
double tt = (double)t / steps;
|
|
@@ -405,11 +458,12 @@ static PyObject* get_bezier_points(PyObject* self, PyObject* args) {
|
|
|
405
458
|
return list;
|
|
406
459
|
}
|
|
407
460
|
|
|
408
|
-
static PyObject* get_grid_points(PyObject* self, PyObject* args) {
|
|
461
|
+
static PyObject* get_grid_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
409
462
|
int x1, y1, x2, y2, step = 2;
|
|
410
463
|
const char* ch = "+";
|
|
411
|
-
|
|
412
|
-
|
|
464
|
+
static char* kwlist[] = {"x1", "y1", "x2", "y2", "step", "char", NULL};
|
|
465
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iiii|is", kwlist, &x1, &y1, &x2, &y2, &step, &ch))
|
|
466
|
+
return NULL;
|
|
413
467
|
PyObject* list = PyList_New(0);
|
|
414
468
|
for (int x = x1; x <= x2; x += step) {
|
|
415
469
|
for (int y = y1; y <= y2; y += step) {
|
|
@@ -424,11 +478,12 @@ static PyObject* get_grid_points(PyObject* self, PyObject* args) {
|
|
|
424
478
|
return list;
|
|
425
479
|
}
|
|
426
480
|
|
|
427
|
-
static PyObject* get_noise_points(PyObject* self, PyObject* args) {
|
|
481
|
+
static PyObject* get_noise_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
428
482
|
int cx, cy, radius, count = 50;
|
|
429
483
|
const char* ch = ".";
|
|
430
|
-
|
|
431
|
-
|
|
484
|
+
static char* kwlist[] = {"cx", "cy", "radius", "count", "char", NULL};
|
|
485
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "iii|is", kwlist, &cx, &cy, &radius, &count, &ch))
|
|
486
|
+
return NULL;
|
|
432
487
|
PyObject* list = PyList_New(0);
|
|
433
488
|
srand(time(NULL));
|
|
434
489
|
for (int i = 0; i < count; i++) {
|
|
@@ -446,51 +501,32 @@ static PyObject* get_noise_points(PyObject* self, PyObject* args) {
|
|
|
446
501
|
return list;
|
|
447
502
|
}
|
|
448
503
|
|
|
449
|
-
static PyObject* get_cube_3d_points(PyObject* self, PyObject* args) {
|
|
504
|
+
static PyObject* get_cube_3d_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
450
505
|
int size = 5;
|
|
451
506
|
double rotx = 0, roty = 0, rotz = 0, scale = 10.0;
|
|
452
507
|
int cx = 40, cy = 12;
|
|
453
508
|
const char* ch = "#";
|
|
454
|
-
|
|
455
|
-
|
|
509
|
+
static char* kwlist[] = {"size", "rotx", "roty", "rotz", "scale", "cx", "cy", "char", NULL};
|
|
510
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iddddis", kwlist, &size, &rotx, &roty, &rotz, &scale, &cx, &cy, &ch))
|
|
511
|
+
return NULL;
|
|
456
512
|
PyObject* list = PyList_New(0);
|
|
457
513
|
double s = size / 2.0;
|
|
458
|
-
|
|
514
|
+
Vec3 vertices[8] = {
|
|
459
515
|
{-s, -s, -s}, {s, -s, -s}, {s, s, -s}, {-s, s, -s},
|
|
460
516
|
{-s, -s, s}, {s, -s, s}, {s, s, s}, {-s, s, s}
|
|
461
517
|
};
|
|
462
|
-
|
|
518
|
+
int edges[12][2] = {
|
|
463
519
|
{0,1}, {1,2}, {2,3}, {3,0},
|
|
464
520
|
{4,5}, {5,6}, {6,7}, {7,4},
|
|
465
521
|
{0,4}, {1,5}, {2,6}, {3,7}
|
|
466
522
|
};
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
double ca = cos(rad), sa = sin(rad);
|
|
471
|
-
double ny = y * ca - z * sa, nz = y * sa + z * ca;
|
|
472
|
-
y = ny; z = nz;
|
|
473
|
-
rad = roty * M_PI / 180.0;
|
|
474
|
-
ca = cos(rad); sa = sin(rad);
|
|
475
|
-
double nx = x * ca + z * sa;
|
|
476
|
-
nz = -x * sa + z * ca;
|
|
477
|
-
x = nx; z = nz;
|
|
478
|
-
rad = rotz * M_PI / 180.0;
|
|
479
|
-
ca = cos(rad); sa = sin(rad);
|
|
480
|
-
nx = x * ca - y * sa;
|
|
481
|
-
ny = x * sa + y * ca;
|
|
482
|
-
x = nx; y = ny;
|
|
483
|
-
return tuple<int,int>({(int)(x * scale + cx), (int)(-y * scale + cy)});
|
|
484
|
-
};
|
|
485
|
-
|
|
486
|
-
vector<pair<int,int>> projected;
|
|
487
|
-
for (auto v : vertices) {
|
|
488
|
-
projected.push_back(rotate(get<0>(v), get<1>(v), get<2>(v)));
|
|
523
|
+
int proj[8][2];
|
|
524
|
+
for (int i = 0; i < 8; i++) {
|
|
525
|
+
project_3d(vertices[i], rotx, roty, rotz, scale, cx, cy, &proj[i][0], &proj[i][1]);
|
|
489
526
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
int
|
|
493
|
-
int x2 = projected[e.second].first, y2 = projected[e.second].second;
|
|
527
|
+
for (int i = 0; i < 12; i++) {
|
|
528
|
+
int x1 = proj[edges[i][0]][0], y1 = proj[edges[i][0]][1];
|
|
529
|
+
int x2 = proj[edges[i][1]][0], y2 = proj[edges[i][1]][1];
|
|
494
530
|
int dx = abs(x2 - x1), dy = abs(y2 - y1);
|
|
495
531
|
int sx = (x1 < x2) ? 1 : -1, sy = (y1 < y2) ? 1 : -1;
|
|
496
532
|
int err = dx - dy;
|
|
@@ -510,37 +546,22 @@ static PyObject* get_cube_3d_points(PyObject* self, PyObject* args) {
|
|
|
510
546
|
return list;
|
|
511
547
|
}
|
|
512
548
|
|
|
513
|
-
static PyObject* get_sphere_3d_points(PyObject* self, PyObject* args) {
|
|
549
|
+
static PyObject* get_sphere_3d_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
514
550
|
int radius = 5, segments = 12;
|
|
515
551
|
double rotx = 0, roty = 0, rotz = 0, scale = 10.0;
|
|
516
552
|
int cx = 40, cy = 12;
|
|
517
553
|
const char* ch = "#";
|
|
518
|
-
|
|
519
|
-
|
|
554
|
+
static char* kwlist[] = {"radius", "segments", "rotx", "roty", "rotz", "scale", "cx", "cy", "char", NULL};
|
|
555
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iidddis", kwlist, &radius, &segments, &rotx, &roty, &rotz, &scale, &cx, &cy, &ch))
|
|
556
|
+
return NULL;
|
|
520
557
|
PyObject* list = PyList_New(0);
|
|
521
558
|
for (int i = 0; i < segments; i++) {
|
|
522
559
|
double theta = (double)i / segments * M_PI;
|
|
523
560
|
for (int j = 0; j < segments; j++) {
|
|
524
561
|
double phi = (double)j / segments * 2 * M_PI;
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
double rad = rotx * M_PI / 180.0;
|
|
529
|
-
double ca = cos(rad), sa = sin(rad);
|
|
530
|
-
double ny = y * ca - z * sa, nz = y * sa + z * ca;
|
|
531
|
-
y = ny; z = nz;
|
|
532
|
-
rad = roty * M_PI / 180.0;
|
|
533
|
-
ca = cos(rad); sa = sin(rad);
|
|
534
|
-
double nx = x * ca + z * sa;
|
|
535
|
-
nz = -x * sa + z * ca;
|
|
536
|
-
x = nx; z = nz;
|
|
537
|
-
rad = rotz * M_PI / 180.0;
|
|
538
|
-
ca = cos(rad); sa = sin(rad);
|
|
539
|
-
nx = x * ca - y * sa;
|
|
540
|
-
ny = x * sa + y * ca;
|
|
541
|
-
x = nx; y = ny;
|
|
542
|
-
int px = (int)(x * scale + cx);
|
|
543
|
-
int py = (int)(-y * scale + cy);
|
|
562
|
+
Vec3 v = {radius * sin(theta) * cos(phi), radius * cos(theta), radius * sin(theta) * sin(phi)};
|
|
563
|
+
int px, py;
|
|
564
|
+
project_3d(v, rotx, roty, rotz, scale, cx, cy, &px, &py);
|
|
544
565
|
PyObject* tuple = PyTuple_New(3);
|
|
545
566
|
PyTuple_SetItem(tuple, 0, PyLong_FromLong(px));
|
|
546
567
|
PyTuple_SetItem(tuple, 1, PyLong_FromLong(py));
|
|
@@ -552,48 +573,33 @@ static PyObject* get_sphere_3d_points(PyObject* self, PyObject* args) {
|
|
|
552
573
|
return list;
|
|
553
574
|
}
|
|
554
575
|
|
|
555
|
-
|
|
576
|
+
|
|
577
|
+
static PyObject* get_cylinder_3d_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
556
578
|
int radius = 5, height = 10, segments = 12;
|
|
557
579
|
double rotx = 0, roty = 0, rotz = 0, scale = 10.0;
|
|
558
580
|
int cx = 40, cy = 12;
|
|
559
581
|
const char* ch = "#";
|
|
560
|
-
|
|
561
|
-
|
|
582
|
+
static char* kwlist[] = {"radius", "height", "segments", "rotx", "roty", "rotz", "scale", "cx", "cy", "char", NULL};
|
|
583
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiidddis", kwlist, &radius, &height, &segments, &rotx, &roty, &rotz, &scale, &cx, &cy, &ch))
|
|
584
|
+
return NULL;
|
|
562
585
|
PyObject* list = PyList_New(0);
|
|
563
|
-
|
|
586
|
+
Vec3 top[100], bottom[100];
|
|
564
587
|
for (int i = 0; i < segments; i++) {
|
|
565
588
|
double rad = (double)i / segments * 2 * M_PI;
|
|
566
|
-
double x = radius * cos(rad)
|
|
567
|
-
|
|
568
|
-
|
|
589
|
+
double x = radius * cos(rad);
|
|
590
|
+
double z = radius * sin(rad);
|
|
591
|
+
top[i] = {x, (double)height / 2, z};
|
|
592
|
+
bottom[i] = {x, -(double)height / 2, z};
|
|
593
|
+
}
|
|
594
|
+
int proj_top[100][2], proj_bottom[100][2];
|
|
595
|
+
for (int i = 0; i < segments; i++) {
|
|
596
|
+
project_3d(top[i], rotx, roty, rotz, scale, cx, cy, &proj_top[i][0], &proj_top[i][1]);
|
|
597
|
+
project_3d(bottom[i], rotx, roty, rotz, scale, cx, cy, &proj_bottom[i][0], &proj_bottom[i][1]);
|
|
569
598
|
}
|
|
570
|
-
|
|
571
|
-
auto rotate = [&](double x, double y, double z) {
|
|
572
|
-
double rad = rotx * M_PI / 180.0;
|
|
573
|
-
double ca = cos(rad), sa = sin(rad);
|
|
574
|
-
double ny = y * ca - z * sa, nz = y * sa + z * ca;
|
|
575
|
-
y = ny; z = nz;
|
|
576
|
-
rad = roty * M_PI / 180.0;
|
|
577
|
-
ca = cos(rad); sa = sin(rad);
|
|
578
|
-
double nx = x * ca + z * sa;
|
|
579
|
-
nz = -x * sa + z * ca;
|
|
580
|
-
x = nx; z = nz;
|
|
581
|
-
rad = rotz * M_PI / 180.0;
|
|
582
|
-
ca = cos(rad); sa = sin(rad);
|
|
583
|
-
nx = x * ca - y * sa;
|
|
584
|
-
ny = x * sa + y * ca;
|
|
585
|
-
x = nx; y = ny;
|
|
586
|
-
return tuple<int,int>({(int)(x * scale + cx), (int)(-y * scale + cy)});
|
|
587
|
-
};
|
|
588
|
-
|
|
589
|
-
vector<pair<int,int>> proj_top, proj_bottom;
|
|
590
|
-
for (auto v : top) proj_top.push_back(rotate(get<0>(v), get<1>(v), get<2>(v)));
|
|
591
|
-
for (auto v : bottom) proj_bottom.push_back(rotate(get<0>(v), get<1>(v), get<2>(v)));
|
|
592
|
-
|
|
593
599
|
for (int i = 0; i < segments; i++) {
|
|
594
600
|
int j = (i + 1) % segments;
|
|
595
|
-
int x1 = proj_top[i]
|
|
596
|
-
int x2 = proj_top[j]
|
|
601
|
+
int x1 = proj_top[i][0], y1 = proj_top[i][1];
|
|
602
|
+
int x2 = proj_top[j][0], y2 = proj_top[j][1];
|
|
597
603
|
int dx = abs(x2 - x1), dy = abs(y2 - y1);
|
|
598
604
|
int sx = (x1 < x2) ? 1 : -1, sy = (y1 < y2) ? 1 : -1;
|
|
599
605
|
int err = dx - dy;
|
|
@@ -609,8 +615,8 @@ static PyObject* get_cylinder_3d_points(PyObject* self, PyObject* args) {
|
|
|
609
615
|
if (e2 > -dy) { err -= dy; x1 += sx; }
|
|
610
616
|
if (e2 < dx) { err += dx; y1 += sy; }
|
|
611
617
|
}
|
|
612
|
-
x1 = proj_bottom[i]
|
|
613
|
-
x2 = proj_bottom[j]
|
|
618
|
+
x1 = proj_bottom[i][0]; y1 = proj_bottom[i][1];
|
|
619
|
+
x2 = proj_bottom[j][0]; y2 = proj_bottom[j][1];
|
|
614
620
|
dx = abs(x2 - x1); dy = abs(y2 - y1);
|
|
615
621
|
sx = (x1 < x2) ? 1 : -1; sy = (y1 < y2) ? 1 : -1;
|
|
616
622
|
err = dx - dy;
|
|
@@ -626,8 +632,8 @@ static PyObject* get_cylinder_3d_points(PyObject* self, PyObject* args) {
|
|
|
626
632
|
if (e2 > -dy) { err -= dy; x1 += sx; }
|
|
627
633
|
if (e2 < dx) { err += dx; y1 += sy; }
|
|
628
634
|
}
|
|
629
|
-
x1 = proj_top[i]
|
|
630
|
-
x2 = proj_bottom[i]
|
|
635
|
+
x1 = proj_top[i][0]; y1 = proj_top[i][1];
|
|
636
|
+
x2 = proj_bottom[i][0]; y2 = proj_bottom[i][1];
|
|
631
637
|
dx = abs(x2 - x1); dy = abs(y2 - y1);
|
|
632
638
|
sx = (x1 < x2) ? 1 : -1; sy = (y1 < y2) ? 1 : -1;
|
|
633
639
|
err = dx - dy;
|
|
@@ -646,47 +652,31 @@ static PyObject* get_cylinder_3d_points(PyObject* self, PyObject* args) {
|
|
|
646
652
|
}
|
|
647
653
|
return list;
|
|
648
654
|
}
|
|
649
|
-
|
|
655
|
+
|
|
656
|
+
static PyObject* get_cone_3d_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
650
657
|
int radius = 5, height = 10, segments = 12;
|
|
651
658
|
double rotx = 0, roty = 0, rotz = 0, scale = 10.0;
|
|
652
659
|
int cx = 40, cy = 12;
|
|
653
660
|
const char* ch = "#";
|
|
654
|
-
|
|
655
|
-
|
|
661
|
+
static char* kwlist[] = {"radius", "height", "segments", "rotx", "roty", "rotz", "scale", "cx", "cy", "char", NULL};
|
|
662
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiidddis", kwlist, &radius, &height, &segments, &rotx, &roty, &rotz, &scale, &cx, &cy, &ch))
|
|
663
|
+
return NULL;
|
|
656
664
|
PyObject* list = PyList_New(0);
|
|
657
|
-
|
|
665
|
+
Vec3 base[100];
|
|
658
666
|
for (int i = 0; i < segments; i++) {
|
|
659
667
|
double rad = (double)i / segments * 2 * M_PI;
|
|
660
|
-
base
|
|
668
|
+
base[i] = {radius * cos(rad), -(double)height / 2, radius * sin(rad)};
|
|
661
669
|
}
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
y = ny; z = nz;
|
|
669
|
-
rad = roty * M_PI / 180.0;
|
|
670
|
-
ca = cos(rad); sa = sin(rad);
|
|
671
|
-
double nx = x * ca + z * sa;
|
|
672
|
-
nz = -x * sa + z * ca;
|
|
673
|
-
x = nx; z = nz;
|
|
674
|
-
rad = rotz * M_PI / 180.0;
|
|
675
|
-
ca = cos(rad); sa = sin(rad);
|
|
676
|
-
nx = x * ca - y * sa;
|
|
677
|
-
ny = x * sa + y * ca;
|
|
678
|
-
x = nx; y = ny;
|
|
679
|
-
return tuple<int,int>({(int)(x * scale + cx), (int)(-y * scale + cy)});
|
|
680
|
-
};
|
|
681
|
-
|
|
682
|
-
vector<pair<int,int>> proj_base;
|
|
683
|
-
for (auto v : base) proj_base.push_back(rotate(get<0>(v), get<1>(v), get<2>(v)));
|
|
684
|
-
pair<int,int> proj_apex = rotate(get<0>(apex), get<1>(apex), get<2>(apex));
|
|
685
|
-
|
|
670
|
+
Vec3 apex = {0, (double)height / 2, 0};
|
|
671
|
+
int proj_base[100][2], proj_apex[2];
|
|
672
|
+
for (int i = 0; i < segments; i++) {
|
|
673
|
+
project_3d(base[i], rotx, roty, rotz, scale, cx, cy, &proj_base[i][0], &proj_base[i][1]);
|
|
674
|
+
}
|
|
675
|
+
project_3d(apex, rotx, roty, rotz, scale, cx, cy, &proj_apex[0], &proj_apex[1]);
|
|
686
676
|
for (int i = 0; i < segments; i++) {
|
|
687
677
|
int j = (i + 1) % segments;
|
|
688
|
-
int x1 = proj_base[i]
|
|
689
|
-
int x2 = proj_base[j]
|
|
678
|
+
int x1 = proj_base[i][0], y1 = proj_base[i][1];
|
|
679
|
+
int x2 = proj_base[j][0], y2 = proj_base[j][1];
|
|
690
680
|
int dx = abs(x2 - x1), dy = abs(y2 - y1);
|
|
691
681
|
int sx = (x1 < x2) ? 1 : -1, sy = (y1 < y2) ? 1 : -1;
|
|
692
682
|
int err = dx - dy;
|
|
@@ -702,8 +692,8 @@ static PyObject* get_cone_3d_points(PyObject* self, PyObject* args) {
|
|
|
702
692
|
if (e2 > -dy) { err -= dy; x1 += sx; }
|
|
703
693
|
if (e2 < dx) { err += dx; y1 += sy; }
|
|
704
694
|
}
|
|
705
|
-
x1 = proj_base[i]
|
|
706
|
-
x2 = proj_apex
|
|
695
|
+
x1 = proj_base[i][0]; y1 = proj_base[i][1];
|
|
696
|
+
x2 = proj_apex[0]; y2 = proj_apex[1];
|
|
707
697
|
dx = abs(x2 - x1); dy = abs(y2 - y1);
|
|
708
698
|
sx = (x1 < x2) ? 1 : -1; sy = (y1 < y2) ? 1 : -1;
|
|
709
699
|
err = dx - dy;
|
|
@@ -723,37 +713,22 @@ static PyObject* get_cone_3d_points(PyObject* self, PyObject* args) {
|
|
|
723
713
|
return list;
|
|
724
714
|
}
|
|
725
715
|
|
|
726
|
-
static PyObject* get_torus_3d_points(PyObject* self, PyObject* args) {
|
|
716
|
+
static PyObject* get_torus_3d_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
727
717
|
int radius = 8, tube = 3, segments = 16;
|
|
728
718
|
double rotx = 0, roty = 0, rotz = 0, scale = 10.0;
|
|
729
719
|
int cx = 40, cy = 12;
|
|
730
720
|
const char* ch = "#";
|
|
731
|
-
|
|
732
|
-
|
|
721
|
+
static char* kwlist[] = {"radius", "tube", "segments", "rotx", "roty", "rotz", "scale", "cx", "cy", "char", NULL};
|
|
722
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiidddis", kwlist, &radius, &tube, &segments, &rotx, &roty, &rotz, &scale, &cx, &cy, &ch))
|
|
723
|
+
return NULL;
|
|
733
724
|
PyObject* list = PyList_New(0);
|
|
734
725
|
for (int i = 0; i < segments; i++) {
|
|
735
726
|
double theta = (double)i / segments * 2 * M_PI;
|
|
736
727
|
for (int j = 0; j < segments; j++) {
|
|
737
728
|
double phi = (double)j / segments * 2 * M_PI;
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
double rad = rotx * M_PI / 180.0;
|
|
742
|
-
double ca = cos(rad), sa = sin(rad);
|
|
743
|
-
double ny = y * ca - z * sa, nz = y * sa + z * ca;
|
|
744
|
-
y = ny; z = nz;
|
|
745
|
-
rad = roty * M_PI / 180.0;
|
|
746
|
-
ca = cos(rad); sa = sin(rad);
|
|
747
|
-
double nx = x * ca + z * sa;
|
|
748
|
-
nz = -x * sa + z * ca;
|
|
749
|
-
x = nx; z = nz;
|
|
750
|
-
rad = rotz * M_PI / 180.0;
|
|
751
|
-
ca = cos(rad); sa = sin(rad);
|
|
752
|
-
nx = x * ca - y * sa;
|
|
753
|
-
ny = x * sa + y * ca;
|
|
754
|
-
x = nx; y = ny;
|
|
755
|
-
int px = (int)(x * scale + cx);
|
|
756
|
-
int py = (int)(-y * scale + cy);
|
|
729
|
+
Vec3 v = {(radius + tube * cos(phi)) * cos(theta), tube * sin(phi), (radius + tube * cos(phi)) * sin(theta)};
|
|
730
|
+
int px, py;
|
|
731
|
+
project_3d(v, rotx, roty, rotz, scale, cx, cy, &px, &py);
|
|
757
732
|
PyObject* tuple = PyTuple_New(3);
|
|
758
733
|
PyTuple_SetItem(tuple, 0, PyLong_FromLong(px));
|
|
759
734
|
PyTuple_SetItem(tuple, 1, PyLong_FromLong(py));
|
|
@@ -764,58 +739,45 @@ static PyObject* get_torus_3d_points(PyObject* self, PyObject* args) {
|
|
|
764
739
|
}
|
|
765
740
|
return list;
|
|
766
741
|
}
|
|
767
|
-
|
|
742
|
+
|
|
743
|
+
static PyObject* get_ico_sphere_3d_points(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
768
744
|
int radius = 5;
|
|
769
745
|
double rotx = 0, roty = 0, rotz = 0, scale = 10.0;
|
|
770
746
|
int cx = 40, cy = 12;
|
|
771
747
|
const char* ch = "#";
|
|
772
|
-
|
|
773
|
-
|
|
748
|
+
static char* kwlist[] = {"radius", "rotx", "roty", "rotz", "scale", "cx", "cy", "char", NULL};
|
|
749
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|idddis", kwlist, &radius, &rotx, &roty, &rotz, &scale, &cx, &cy, &ch))
|
|
750
|
+
return NULL;
|
|
774
751
|
PyObject* list = PyList_New(0);
|
|
775
752
|
double phi = (1 + sqrt(5)) / 2.0;
|
|
776
|
-
|
|
753
|
+
Vec3 verts[12] = {
|
|
777
754
|
{-1, phi, 0}, {1, phi, 0}, {-1, -phi, 0}, {1, -phi, 0},
|
|
778
755
|
{0, -1, phi}, {0, 1, phi}, {0, -1, -phi}, {0, 1, -phi},
|
|
779
756
|
{phi, 0, -1}, {phi, 0, 1}, {-phi, 0, -1}, {-phi, 0, 1}
|
|
780
757
|
};
|
|
781
|
-
for (
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
758
|
+
for (int i = 0; i < 12; i++) {
|
|
759
|
+
verts[i].x *= radius / 1.618;
|
|
760
|
+
verts[i].y *= radius / 1.618;
|
|
761
|
+
verts[i].z *= radius / 1.618;
|
|
785
762
|
}
|
|
786
|
-
|
|
763
|
+
int edges[30][2] = {
|
|
787
764
|
{0,1}, {0,4}, {0,5}, {0,10}, {0,11},
|
|
788
765
|
{1,2}, {1,5}, {1,7}, {1,8}, {1,9},
|
|
789
766
|
{2,3}, {2,6}, {2,7}, {2,10}, {2,11},
|
|
790
767
|
{3,4}, {3,6}, {3,8}, {3,9}, {3,11},
|
|
791
|
-
{4,5}, {4,9}, {4,11},
|
|
792
|
-
{
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
double rad = rotx * M_PI / 180.0;
|
|
797
|
-
double ca = cos(rad), sa = sin(rad);
|
|
798
|
-
double ny = y * ca - z * sa, nz = y * sa + z * ca;
|
|
799
|
-
y = ny; z = nz;
|
|
800
|
-
rad = roty * M_PI / 180.0;
|
|
801
|
-
ca = cos(rad); sa = sin(rad);
|
|
802
|
-
double nx = x * ca + z * sa;
|
|
803
|
-
nz = -x * sa + z * ca;
|
|
804
|
-
x = nx; z = nz;
|
|
805
|
-
rad = rotz * M_PI / 180.0;
|
|
806
|
-
ca = cos(rad); sa = sin(rad);
|
|
807
|
-
nx = x * ca - y * sa;
|
|
808
|
-
ny = x * sa + y * ca;
|
|
809
|
-
x = nx; y = ny;
|
|
810
|
-
return tuple<int,int>({(int)(x * scale + cx), (int)(-y * scale + cy)});
|
|
768
|
+
{4,5}, {4,9}, {4,11},
|
|
769
|
+
{5,7}, {5,9},
|
|
770
|
+
{6,7}, {6,10},
|
|
771
|
+
{7,8},
|
|
772
|
+
{8,9}, {8,10}
|
|
811
773
|
};
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
for (
|
|
817
|
-
int x1 =
|
|
818
|
-
int x2 =
|
|
774
|
+
int proj[12][2];
|
|
775
|
+
for (int i = 0; i < 12; i++) {
|
|
776
|
+
project_3d(verts[i], rotx, roty, rotz, scale, cx, cy, &proj[i][0], &proj[i][1]);
|
|
777
|
+
}
|
|
778
|
+
for (int i = 0; i < 30; i++) {
|
|
779
|
+
int x1 = proj[edges[i][0]][0], y1 = proj[edges[i][0]][1];
|
|
780
|
+
int x2 = proj[edges[i][1]][0], y2 = proj[edges[i][1]][1];
|
|
819
781
|
int dx = abs(x2 - x1), dy = abs(y2 - y1);
|
|
820
782
|
int sx = (x1 < x2) ? 1 : -1, sy = (y1 < y2) ? 1 : -1;
|
|
821
783
|
int err = dx - dy;
|
|
@@ -835,12 +797,13 @@ static PyObject* get_ico_sphere_3d_points(PyObject* self, PyObject* args) {
|
|
|
835
797
|
return list;
|
|
836
798
|
}
|
|
837
799
|
|
|
838
|
-
static PyObject* DrawPic(PyObject* self, PyObject* args) {
|
|
800
|
+
static PyObject* DrawPic(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
839
801
|
PyObject* points = NULL;
|
|
840
802
|
int width = 80, height = 25, clear_before = 0;
|
|
841
803
|
const char* default_char = " ";
|
|
842
|
-
|
|
843
|
-
|
|
804
|
+
static char* kwlist[] = {"points", "width", "height", "clear_before", "default_char", NULL};
|
|
805
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|Oiiis", kwlist, &points, &width, &height, &clear_before, &default_char))
|
|
806
|
+
return NULL;
|
|
844
807
|
if (clear_before) {
|
|
845
808
|
#ifdef _WIN32
|
|
846
809
|
system("cls");
|
|
@@ -848,9 +811,7 @@ static PyObject* DrawPic(PyObject* self, PyObject* args) {
|
|
|
848
811
|
system("clear");
|
|
849
812
|
#endif
|
|
850
813
|
}
|
|
851
|
-
|
|
852
814
|
vector<vector<string>> field(height, vector<string>(width, default_char));
|
|
853
|
-
|
|
854
815
|
if (points && PyList_Check(points)) {
|
|
855
816
|
Py_ssize_t n = PyList_Size(points);
|
|
856
817
|
for (Py_ssize_t i = 0; i < n; i++) {
|
|
@@ -866,18 +827,16 @@ static PyObject* DrawPic(PyObject* self, PyObject* args) {
|
|
|
866
827
|
}
|
|
867
828
|
}
|
|
868
829
|
}
|
|
869
|
-
|
|
870
830
|
for (int y = 0; y < height; y++) {
|
|
871
831
|
for (int x = 0; x < width; x++) {
|
|
872
832
|
cout << field[y][x];
|
|
873
833
|
}
|
|
874
834
|
cout << endl;
|
|
875
835
|
}
|
|
876
|
-
|
|
877
836
|
Py_RETURN_NONE;
|
|
878
837
|
}
|
|
879
838
|
|
|
880
|
-
static PyObject* GetKey(PyObject* self, PyObject* args) {
|
|
839
|
+
static PyObject* GetKey(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
881
840
|
#ifdef _WIN32
|
|
882
841
|
int ch = _getch();
|
|
883
842
|
#else
|
|
@@ -891,21 +850,24 @@ static PyObject* GetKey(PyObject* self, PyObject* args) {
|
|
|
891
850
|
#endif
|
|
892
851
|
return PyUnicode_FromStringAndSize(&ch, 1);
|
|
893
852
|
}
|
|
894
|
-
|
|
853
|
+
|
|
854
|
+
static PyObject* StartPyCode(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
895
855
|
const char* code;
|
|
896
856
|
int wait = 0;
|
|
897
|
-
|
|
898
|
-
|
|
857
|
+
static char* kwlist[] = {"code", "wait", NULL};
|
|
858
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", kwlist, &code, &wait))
|
|
859
|
+
return NULL;
|
|
899
860
|
string cmd = "python -c \"" + string(code) + "\"";
|
|
900
861
|
if (!wait) cmd += " &";
|
|
901
862
|
int result = system(cmd.c_str());
|
|
902
863
|
return PyLong_FromLong(result);
|
|
903
864
|
}
|
|
904
865
|
|
|
905
|
-
static PyObject* KillPyScript(PyObject* self, PyObject* args) {
|
|
866
|
+
static PyObject* KillPyScript(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
906
867
|
int pid;
|
|
907
|
-
|
|
908
|
-
|
|
868
|
+
static char* kwlist[] = {"pid", NULL};
|
|
869
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &pid))
|
|
870
|
+
return NULL;
|
|
909
871
|
#ifdef _WIN32
|
|
910
872
|
string cmd = "taskkill /PID " + to_string(pid);
|
|
911
873
|
#else
|
|
@@ -915,10 +877,11 @@ static PyObject* KillPyScript(PyObject* self, PyObject* args) {
|
|
|
915
877
|
return PyLong_FromLong(result);
|
|
916
878
|
}
|
|
917
879
|
|
|
918
|
-
static PyObject* IsRunning(PyObject* self, PyObject* args) {
|
|
880
|
+
static PyObject* IsRunning(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
919
881
|
int pid;
|
|
920
|
-
|
|
921
|
-
|
|
882
|
+
static char* kwlist[] = {"pid", NULL};
|
|
883
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &pid))
|
|
884
|
+
return NULL;
|
|
922
885
|
#ifdef _WIN32
|
|
923
886
|
HANDLE process = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, pid);
|
|
924
887
|
if (process) {
|
|
@@ -934,7 +897,7 @@ static PyObject* IsRunning(PyObject* self, PyObject* args) {
|
|
|
934
897
|
#endif
|
|
935
898
|
}
|
|
936
899
|
|
|
937
|
-
static PyObject* GetCurrentPID(PyObject* self, PyObject* args) {
|
|
900
|
+
static PyObject* GetCurrentPID(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
938
901
|
#ifdef _WIN32
|
|
939
902
|
return PyLong_FromLong(GetCurrentProcessId());
|
|
940
903
|
#else
|
|
@@ -942,11 +905,11 @@ static PyObject* GetCurrentPID(PyObject* self, PyObject* args) {
|
|
|
942
905
|
#endif
|
|
943
906
|
}
|
|
944
907
|
|
|
945
|
-
static PyObject* WaitForProcess(PyObject* self, PyObject* args) {
|
|
946
|
-
int pid;
|
|
947
|
-
|
|
948
|
-
if (!
|
|
949
|
-
|
|
908
|
+
static PyObject* WaitForProcess(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
909
|
+
int pid, timeout = -1;
|
|
910
|
+
static char* kwlist[] = {"pid", "timeout", NULL};
|
|
911
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist, &pid, &timeout))
|
|
912
|
+
return NULL;
|
|
950
913
|
int elapsed = 0;
|
|
951
914
|
while (true) {
|
|
952
915
|
#ifdef _WIN32
|
|
@@ -966,10 +929,9 @@ static PyObject* WaitForProcess(PyObject* self, PyObject* args) {
|
|
|
966
929
|
}
|
|
967
930
|
}
|
|
968
931
|
|
|
969
|
-
static PyObject* GetProcessList(PyObject* self, PyObject* args) {
|
|
932
|
+
static PyObject* GetProcessList(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
970
933
|
PyObject* list = PyList_New(0);
|
|
971
934
|
#ifdef _WIN32
|
|
972
|
-
// Windows implementation would require PSAPI or Toolhelp
|
|
973
935
|
PyObject* tuple = PyTuple_New(2);
|
|
974
936
|
PyTuple_SetItem(tuple, 0, PyLong_FromLong(0));
|
|
975
937
|
PyTuple_SetItem(tuple, 1, PyUnicode_FromString("Not implemented on Windows"));
|
|
@@ -997,10 +959,12 @@ static PyObject* GetProcessList(PyObject* self, PyObject* args) {
|
|
|
997
959
|
#endif
|
|
998
960
|
return list;
|
|
999
961
|
}
|
|
1000
|
-
|
|
962
|
+
|
|
963
|
+
static PyObject* GetProcessInfo(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1001
964
|
int pid;
|
|
1002
|
-
|
|
1003
|
-
|
|
965
|
+
static char* kwlist[] = {"pid", NULL};
|
|
966
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &pid))
|
|
967
|
+
return NULL;
|
|
1004
968
|
#ifdef _WIN32
|
|
1005
969
|
return PyUnicode_FromString("Not implemented on Windows");
|
|
1006
970
|
#else
|
|
@@ -1028,7 +992,7 @@ static PyObject* GetProcessInfo(PyObject* self, PyObject* args) {
|
|
|
1028
992
|
#endif
|
|
1029
993
|
}
|
|
1030
994
|
|
|
1031
|
-
static PyObject* clear_screen(PyObject* self, PyObject* args) {
|
|
995
|
+
static PyObject* clear_screen(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1032
996
|
#ifdef _WIN32
|
|
1033
997
|
system("cls");
|
|
1034
998
|
#else
|
|
@@ -1037,12 +1001,13 @@ static PyObject* clear_screen(PyObject* self, PyObject* args) {
|
|
|
1037
1001
|
Py_RETURN_NONE;
|
|
1038
1002
|
}
|
|
1039
1003
|
|
|
1040
|
-
static PyObject* animate_print(PyObject* self, PyObject* args) {
|
|
1004
|
+
static PyObject* animate_print(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1041
1005
|
const char* text;
|
|
1042
1006
|
double delay = 0.05;
|
|
1043
1007
|
int backspace = 0;
|
|
1044
|
-
|
|
1045
|
-
|
|
1008
|
+
static char* kwlist[] = {"text", "delay", "backspace", NULL};
|
|
1009
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|di", kwlist, &text, &delay, &backspace))
|
|
1010
|
+
return NULL;
|
|
1046
1011
|
if (backspace) {
|
|
1047
1012
|
for (int i = 0; text[i]; i++) {
|
|
1048
1013
|
cout << text[i] << flush;
|
|
@@ -1067,13 +1032,14 @@ static PyObject* animate_print(PyObject* self, PyObject* args) {
|
|
|
1067
1032
|
Py_RETURN_NONE;
|
|
1068
1033
|
}
|
|
1069
1034
|
|
|
1070
|
-
static PyObject* spinning_cursor(PyObject* self, PyObject* args) {
|
|
1035
|
+
static PyObject* spinning_cursor(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1071
1036
|
const char* message = "Loading";
|
|
1072
1037
|
double duration = 3.0, delay = 0.1;
|
|
1073
1038
|
int clear_before = 0;
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1039
|
+
static char* kwlist[] = {"message", "duration", "delay", "clear_before", NULL};
|
|
1040
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|sddi", kwlist, &message, &duration, &delay, &clear_before))
|
|
1041
|
+
return NULL;
|
|
1042
|
+
if (clear_before) clear_screen(NULL, NULL, NULL);
|
|
1077
1043
|
vector<string> frames = {"|", "/", "-", "\\"};
|
|
1078
1044
|
double end_time = time(NULL) + duration;
|
|
1079
1045
|
int idx = 0;
|
|
@@ -1086,13 +1052,14 @@ static PyObject* spinning_cursor(PyObject* self, PyObject* args) {
|
|
|
1086
1052
|
Py_RETURN_NONE;
|
|
1087
1053
|
}
|
|
1088
1054
|
|
|
1089
|
-
static PyObject* countdown(PyObject* self, PyObject* args) {
|
|
1055
|
+
static PyObject* countdown(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1090
1056
|
int seconds;
|
|
1091
1057
|
const char* message = "Countdown";
|
|
1092
1058
|
int clear_before = 0;
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1059
|
+
static char* kwlist[] = {"seconds", "message", "clear_before", NULL};
|
|
1060
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|si", kwlist, &seconds, &message, &clear_before))
|
|
1061
|
+
return NULL;
|
|
1062
|
+
if (clear_before) clear_screen(NULL, NULL, NULL);
|
|
1096
1063
|
for (int i = seconds; i > 0; i--) {
|
|
1097
1064
|
cout << "\r" << message << ": " << i << " seconds remaining " << flush;
|
|
1098
1065
|
this_thread::sleep_for(chrono::seconds(1));
|
|
@@ -1101,7 +1068,7 @@ static PyObject* countdown(PyObject* self, PyObject* args) {
|
|
|
1101
1068
|
Py_RETURN_NONE;
|
|
1102
1069
|
}
|
|
1103
1070
|
|
|
1104
|
-
static PyObject* GetPipVer(PyObject* self, PyObject* args) {
|
|
1071
|
+
static PyObject* GetPipVer(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1105
1072
|
#ifdef _WIN32
|
|
1106
1073
|
FILE* fp = _popen("python -m pip --version", "r");
|
|
1107
1074
|
#else
|
|
@@ -1124,61 +1091,58 @@ static PyObject* GetPipVer(PyObject* self, PyObject* args) {
|
|
|
1124
1091
|
#endif
|
|
1125
1092
|
return PyUnicode_FromString("Error: Python not installed");
|
|
1126
1093
|
}
|
|
1094
|
+
|
|
1127
1095
|
#ifdef _WIN32
|
|
1128
|
-
static PyObject* Connect(PyObject* self, PyObject* args) {
|
|
1096
|
+
static PyObject* Connect(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1129
1097
|
const char* host = "127.0.0.1";
|
|
1130
1098
|
int port = 54321;
|
|
1131
|
-
|
|
1132
|
-
|
|
1099
|
+
static char* kwlist[] = {"host", "port", NULL};
|
|
1100
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|si", kwlist, &host, &port))
|
|
1101
|
+
return NULL;
|
|
1133
1102
|
WSADATA wsaData;
|
|
1134
1103
|
WSAStartup(MAKEWORD(2,2), &wsaData);
|
|
1135
|
-
|
|
1136
1104
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
1137
1105
|
if (sock == INVALID_SOCKET) {
|
|
1138
1106
|
WSACleanup();
|
|
1139
1107
|
return PyLong_FromLong(-1);
|
|
1140
1108
|
}
|
|
1141
|
-
|
|
1142
1109
|
struct sockaddr_in server;
|
|
1143
1110
|
server.sin_family = AF_INET;
|
|
1144
1111
|
server.sin_port = htons(port);
|
|
1145
1112
|
server.sin_addr.s_addr = inet_addr(host);
|
|
1146
|
-
|
|
1147
1113
|
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
|
|
1148
1114
|
closesocket(sock);
|
|
1149
1115
|
WSACleanup();
|
|
1150
1116
|
return PyLong_FromLong(-1);
|
|
1151
1117
|
}
|
|
1152
|
-
|
|
1153
1118
|
return PyLong_FromLong(sock);
|
|
1154
1119
|
}
|
|
1155
1120
|
#else
|
|
1156
|
-
static PyObject* Connect(PyObject* self, PyObject* args) {
|
|
1121
|
+
static PyObject* Connect(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1157
1122
|
const char* host = "127.0.0.1";
|
|
1158
1123
|
int port = 54321;
|
|
1159
|
-
|
|
1160
|
-
|
|
1124
|
+
static char* kwlist[] = {"host", "port", NULL};
|
|
1125
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|si", kwlist, &host, &port))
|
|
1126
|
+
return NULL;
|
|
1161
1127
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
1162
1128
|
if (sock < 0) return PyLong_FromLong(-1);
|
|
1163
|
-
|
|
1164
1129
|
struct sockaddr_in server;
|
|
1165
1130
|
server.sin_family = AF_INET;
|
|
1166
1131
|
server.sin_port = htons(port);
|
|
1167
1132
|
server.sin_addr.s_addr = inet_addr(host);
|
|
1168
|
-
|
|
1169
1133
|
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
|
|
1170
1134
|
close(sock);
|
|
1171
1135
|
return PyLong_FromLong(-1);
|
|
1172
1136
|
}
|
|
1173
|
-
|
|
1174
1137
|
return PyLong_FromLong(sock);
|
|
1175
1138
|
}
|
|
1176
1139
|
#endif
|
|
1177
1140
|
|
|
1178
|
-
static PyObject* ConnectPublic(PyObject* self, PyObject* args) {
|
|
1141
|
+
static PyObject* ConnectPublic(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1179
1142
|
int port = 54321;
|
|
1180
|
-
|
|
1181
|
-
|
|
1143
|
+
static char* kwlist[] = {"port", NULL};
|
|
1144
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &port))
|
|
1145
|
+
return NULL;
|
|
1182
1146
|
#ifdef _WIN32
|
|
1183
1147
|
WSADATA wsaData;
|
|
1184
1148
|
WSAStartup(MAKEWORD(2,2), &wsaData);
|
|
@@ -1188,7 +1152,6 @@ static PyObject* ConnectPublic(PyObject* self, PyObject* args) {
|
|
|
1188
1152
|
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
1189
1153
|
if (sock < 0) return PyLong_FromLong(-1);
|
|
1190
1154
|
#endif
|
|
1191
|
-
|
|
1192
1155
|
struct sockaddr_in server;
|
|
1193
1156
|
server.sin_family = AF_INET;
|
|
1194
1157
|
server.sin_port = htons(80);
|
|
@@ -1197,7 +1160,6 @@ static PyObject* ConnectPublic(PyObject* self, PyObject* args) {
|
|
|
1197
1160
|
#else
|
|
1198
1161
|
inet_aton("216.58.200.78", &server.sin_addr);
|
|
1199
1162
|
#endif
|
|
1200
|
-
|
|
1201
1163
|
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
|
|
1202
1164
|
#ifdef _WIN32
|
|
1203
1165
|
closesocket(sock); WSACleanup();
|
|
@@ -1206,7 +1168,6 @@ static PyObject* ConnectPublic(PyObject* self, PyObject* args) {
|
|
|
1206
1168
|
#endif
|
|
1207
1169
|
return PyLong_FromLong(-1);
|
|
1208
1170
|
}
|
|
1209
|
-
|
|
1210
1171
|
string request = "GET /ip HTTP/1.1\r\nHost: api.ipify.org\r\nConnection: close\r\n\r\n";
|
|
1211
1172
|
send(sock, request.c_str(), request.length(), 0);
|
|
1212
1173
|
char buffer[4096];
|
|
@@ -1222,7 +1183,6 @@ static PyObject* ConnectPublic(PyObject* self, PyObject* args) {
|
|
|
1222
1183
|
#else
|
|
1223
1184
|
close(sock);
|
|
1224
1185
|
#endif
|
|
1225
|
-
|
|
1226
1186
|
size_t pos = response.find("\r\n\r\n");
|
|
1227
1187
|
if (pos != string::npos) {
|
|
1228
1188
|
string body = response.substr(pos + 4);
|
|
@@ -1232,20 +1192,21 @@ static PyObject* ConnectPublic(PyObject* self, PyObject* args) {
|
|
|
1232
1192
|
return PyUnicode_FromString("");
|
|
1233
1193
|
}
|
|
1234
1194
|
|
|
1235
|
-
static PyObject* Send(PyObject* self, PyObject* args) {
|
|
1195
|
+
static PyObject* Send(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1236
1196
|
int sock;
|
|
1237
1197
|
const char* message;
|
|
1238
|
-
|
|
1239
|
-
|
|
1198
|
+
static char* kwlist[] = {"sock", "message", NULL};
|
|
1199
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "is", kwlist, &sock, &message))
|
|
1200
|
+
return NULL;
|
|
1240
1201
|
int result = send(sock, message, strlen(message), 0);
|
|
1241
1202
|
return PyLong_FromLong(result);
|
|
1242
1203
|
}
|
|
1243
1204
|
|
|
1244
|
-
static PyObject* Receive(PyObject* self, PyObject* args) {
|
|
1245
|
-
int sock;
|
|
1246
|
-
|
|
1247
|
-
if (!
|
|
1248
|
-
|
|
1205
|
+
static PyObject* Receive(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1206
|
+
int sock, size = 1024;
|
|
1207
|
+
static char* kwlist[] = {"sock", "size", NULL};
|
|
1208
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i|i", kwlist, &sock, &size))
|
|
1209
|
+
return NULL;
|
|
1249
1210
|
char* buffer = (char*)malloc(size + 1);
|
|
1250
1211
|
int result = recv(sock, buffer, size, 0);
|
|
1251
1212
|
if (result > 0) {
|
|
@@ -1257,10 +1218,12 @@ static PyObject* Receive(PyObject* self, PyObject* args) {
|
|
|
1257
1218
|
free(buffer);
|
|
1258
1219
|
return PyUnicode_FromString("");
|
|
1259
1220
|
}
|
|
1260
|
-
|
|
1221
|
+
|
|
1222
|
+
static PyObject* Disconnect(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1261
1223
|
int sock;
|
|
1262
|
-
|
|
1263
|
-
|
|
1224
|
+
static char* kwlist[] = {"sock", NULL};
|
|
1225
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "i", kwlist, &sock))
|
|
1226
|
+
return NULL;
|
|
1264
1227
|
#ifdef _WIN32
|
|
1265
1228
|
closesocket(sock);
|
|
1266
1229
|
WSACleanup();
|
|
@@ -1270,126 +1233,110 @@ static PyObject* Disconnect(PyObject* self, PyObject* args) {
|
|
|
1270
1233
|
Py_RETURN_NONE;
|
|
1271
1234
|
}
|
|
1272
1235
|
|
|
1273
|
-
static PyObject* GetPublicIP(PyObject* self, PyObject* args) {
|
|
1274
|
-
PyObject* ip = ConnectPublic(self, args);
|
|
1236
|
+
static PyObject* GetPublicIP(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1237
|
+
PyObject* ip = ConnectPublic(self, args, kwargs);
|
|
1275
1238
|
return ip;
|
|
1276
1239
|
}
|
|
1277
1240
|
|
|
1278
|
-
static PyObject* CreateServer(PyObject* self, PyObject* args) {
|
|
1241
|
+
static PyObject* CreateServer(PyObject* self, PyObject* args, PyObject* kwargs) {
|
|
1279
1242
|
int port = 54321;
|
|
1280
|
-
|
|
1281
|
-
|
|
1243
|
+
static char* kwlist[] = {"port", NULL};
|
|
1244
|
+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|i", kwlist, &port))
|
|
1245
|
+
return NULL;
|
|
1282
1246
|
#ifdef _WIN32
|
|
1283
1247
|
WSADATA wsaData;
|
|
1284
1248
|
WSAStartup(MAKEWORD(2,2), &wsaData);
|
|
1285
1249
|
int server_sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
1286
|
-
if (server_sock == INVALID_SOCKET) {
|
|
1287
|
-
WSACleanup();
|
|
1288
|
-
return PyLong_FromLong(-1);
|
|
1289
|
-
}
|
|
1250
|
+
if (server_sock == INVALID_SOCKET) { WSACleanup(); return PyLong_FromLong(-1); }
|
|
1290
1251
|
#else
|
|
1291
1252
|
int server_sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
1292
|
-
if (server_sock < 0)
|
|
1293
|
-
return PyLong_FromLong(-1);
|
|
1294
|
-
}
|
|
1253
|
+
if (server_sock < 0) return PyLong_FromLong(-1);
|
|
1295
1254
|
#endif
|
|
1296
|
-
|
|
1297
1255
|
int opt = 1;
|
|
1298
1256
|
#ifdef _WIN32
|
|
1299
1257
|
setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));
|
|
1300
1258
|
#else
|
|
1301
1259
|
setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
|
1302
1260
|
#endif
|
|
1303
|
-
|
|
1304
1261
|
struct sockaddr_in server;
|
|
1305
1262
|
server.sin_family = AF_INET;
|
|
1306
1263
|
server.sin_addr.s_addr = INADDR_ANY;
|
|
1307
1264
|
server.sin_port = htons(port);
|
|
1308
|
-
|
|
1309
1265
|
#ifdef _WIN32
|
|
1310
1266
|
if (::bind(server_sock, (struct sockaddr*)&server, sizeof(server)) == SOCKET_ERROR) {
|
|
1311
|
-
closesocket(server_sock);
|
|
1312
|
-
WSACleanup();
|
|
1313
|
-
return PyLong_FromLong(-1);
|
|
1267
|
+
closesocket(server_sock); WSACleanup(); return PyLong_FromLong(-1);
|
|
1314
1268
|
}
|
|
1315
1269
|
if (listen(server_sock, 1) == SOCKET_ERROR) {
|
|
1316
|
-
closesocket(server_sock);
|
|
1317
|
-
WSACleanup();
|
|
1318
|
-
return PyLong_FromLong(-1);
|
|
1270
|
+
closesocket(server_sock); WSACleanup(); return PyLong_FromLong(-1);
|
|
1319
1271
|
}
|
|
1320
1272
|
#else
|
|
1321
1273
|
if (::bind(server_sock, (struct sockaddr*)&server, sizeof(server)) < 0) {
|
|
1322
|
-
close(server_sock);
|
|
1323
|
-
return PyLong_FromLong(-1);
|
|
1274
|
+
close(server_sock); return PyLong_FromLong(-1);
|
|
1324
1275
|
}
|
|
1325
1276
|
if (listen(server_sock, 1) < 0) {
|
|
1326
|
-
close(server_sock);
|
|
1327
|
-
return PyLong_FromLong(-1);
|
|
1277
|
+
close(server_sock); return PyLong_FromLong(-1);
|
|
1328
1278
|
}
|
|
1329
1279
|
#endif
|
|
1330
|
-
|
|
1331
1280
|
return PyLong_FromLong(server_sock);
|
|
1332
1281
|
}
|
|
1333
1282
|
|
|
1334
|
-
static PyMethodDef
|
|
1335
|
-
{"rgb", rgb, METH_VARARGS, "RGB color"},
|
|
1336
|
-
{"bg_rgb", bg_rgb, METH_VARARGS, "RGB background"},
|
|
1337
|
-
{"colorize", colorize, METH_VARARGS, "Colorize text"},
|
|
1338
|
-
{"get_line_points", get_line_points, METH_VARARGS, "Get line points"},
|
|
1339
|
-
{"get_circle_points", get_circle_points, METH_VARARGS, "Get circle points"},
|
|
1340
|
-
{"get_filled_circle_points", get_filled_circle_points, METH_VARARGS, "Get filled circle points"},
|
|
1341
|
-
{"get_rect_points", get_rect_points, METH_VARARGS, "Get rectangle points"},
|
|
1342
|
-
{"get_fill_rect_points", get_fill_rect_points, METH_VARARGS, "Get filled rectangle points"},
|
|
1343
|
-
{"get_triangle_points", get_triangle_points, METH_VARARGS, "Get triangle points"},
|
|
1344
|
-
{"get_filled_triangle_points", get_filled_triangle_points, METH_VARARGS, "Get filled triangle points"},
|
|
1345
|
-
{"get_heart_points", get_heart_points, METH_VARARGS, "Get heart points"},
|
|
1346
|
-
{"get_star_points", get_star_points, METH_VARARGS, "Get star points"},
|
|
1347
|
-
{"get_wave_points", get_wave_points, METH_VARARGS, "Get wave points"},
|
|
1348
|
-
{"get_spiral_points", get_spiral_points, METH_VARARGS, "Get spiral points"},
|
|
1349
|
-
{"get_bezier_points", get_bezier_points, METH_VARARGS, "Get bezier points"},
|
|
1350
|
-
{"get_grid_points", get_grid_points, METH_VARARGS, "Get grid points"},
|
|
1351
|
-
{"get_noise_points", get_noise_points, METH_VARARGS, "Get noise points"},
|
|
1352
|
-
{"get_cube_3d_points", get_cube_3d_points, METH_VARARGS, "Get 3D cube points"},
|
|
1353
|
-
{"get_sphere_3d_points", get_sphere_3d_points, METH_VARARGS, "Get 3D sphere points"},
|
|
1354
|
-
{"get_cylinder_3d_points", get_cylinder_3d_points, METH_VARARGS, "Get 3D cylinder points"},
|
|
1355
|
-
{"get_cone_3d_points", get_cone_3d_points, METH_VARARGS, "Get 3D cone points"},
|
|
1356
|
-
{"get_torus_3d_points", get_torus_3d_points, METH_VARARGS, "Get 3D torus points"},
|
|
1357
|
-
{"get_ico_sphere_3d_points", get_ico_sphere_3d_points, METH_VARARGS, "Get 3D ico sphere points"},
|
|
1358
|
-
{"DrawPic", DrawPic, METH_VARARGS, "Draw points"},
|
|
1359
|
-
{"GetKey", GetKey, METH_VARARGS, "Get key without Enter"},
|
|
1360
|
-
{"StartPyCode", StartPyCode, METH_VARARGS, "Start Python code"},
|
|
1361
|
-
{"KillPyScript", KillPyScript, METH_VARARGS, "Kill Python script"},
|
|
1362
|
-
{"IsRunning", IsRunning, METH_VARARGS, "Check if process is running"},
|
|
1363
|
-
{"GetCurrentPID", GetCurrentPID, METH_VARARGS, "Get current PID"},
|
|
1364
|
-
{"WaitForProcess", WaitForProcess, METH_VARARGS, "Wait for process
|
|
1365
|
-
{"GetProcessList", GetProcessList, METH_VARARGS, "Get list
|
|
1366
|
-
{"GetProcessInfo", GetProcessInfo, METH_VARARGS, "Get process info"},
|
|
1367
|
-
{"clear_screen", clear_screen, METH_VARARGS, "Clear screen"},
|
|
1368
|
-
{"animate_print", animate_print, METH_VARARGS, "Animate print"},
|
|
1369
|
-
{"spinning_cursor", spinning_cursor, METH_VARARGS, "Spinning cursor
|
|
1370
|
-
{"countdown", countdown, METH_VARARGS, "Countdown
|
|
1371
|
-
{"GetPipVer", GetPipVer, METH_VARARGS, "Get pip version"},
|
|
1372
|
-
{"Connect", Connect, METH_VARARGS, "Connect to server"},
|
|
1373
|
-
{"
|
|
1374
|
-
{"
|
|
1375
|
-
{"
|
|
1376
|
-
{"
|
|
1377
|
-
{"
|
|
1378
|
-
{"CreateServer", CreateServer, METH_VARARGS, "Create server"},
|
|
1283
|
+
static PyMethodDef CoreMethods[] = {
|
|
1284
|
+
{"rgb", (PyCFunction)rgb, METH_VARARGS | METH_KEYWORDS, "RGB color"},
|
|
1285
|
+
{"bg_rgb", (PyCFunction)bg_rgb, METH_VARARGS | METH_KEYWORDS, "RGB background"},
|
|
1286
|
+
{"colorize", (PyCFunction)colorize, METH_VARARGS | METH_KEYWORDS, "Colorize text"},
|
|
1287
|
+
{"get_line_points", (PyCFunction)get_line_points, METH_VARARGS | METH_KEYWORDS, "Get line points"},
|
|
1288
|
+
{"get_circle_points", (PyCFunction)get_circle_points, METH_VARARGS | METH_KEYWORDS, "Get circle points"},
|
|
1289
|
+
{"get_filled_circle_points", (PyCFunction)get_filled_circle_points, METH_VARARGS | METH_KEYWORDS, "Get filled circle points"},
|
|
1290
|
+
{"get_rect_points", (PyCFunction)get_rect_points, METH_VARARGS | METH_KEYWORDS, "Get rectangle points"},
|
|
1291
|
+
{"get_fill_rect_points", (PyCFunction)get_fill_rect_points, METH_VARARGS | METH_KEYWORDS, "Get filled rectangle points"},
|
|
1292
|
+
{"get_triangle_points", (PyCFunction)get_triangle_points, METH_VARARGS | METH_KEYWORDS, "Get triangle points"},
|
|
1293
|
+
{"get_filled_triangle_points", (PyCFunction)get_filled_triangle_points, METH_VARARGS | METH_KEYWORDS, "Get filled triangle points"},
|
|
1294
|
+
{"get_heart_points", (PyCFunction)get_heart_points, METH_VARARGS | METH_KEYWORDS, "Get heart points"},
|
|
1295
|
+
{"get_star_points", (PyCFunction)get_star_points, METH_VARARGS | METH_KEYWORDS, "Get star points"},
|
|
1296
|
+
{"get_wave_points", (PyCFunction)get_wave_points, METH_VARARGS | METH_KEYWORDS, "Get wave points"},
|
|
1297
|
+
{"get_spiral_points", (PyCFunction)get_spiral_points, METH_VARARGS | METH_KEYWORDS, "Get spiral points"},
|
|
1298
|
+
{"get_bezier_points", (PyCFunction)get_bezier_points, METH_VARARGS | METH_KEYWORDS, "Get bezier points"},
|
|
1299
|
+
{"get_grid_points", (PyCFunction)get_grid_points, METH_VARARGS | METH_KEYWORDS, "Get grid points"},
|
|
1300
|
+
{"get_noise_points", (PyCFunction)get_noise_points, METH_VARARGS | METH_KEYWORDS, "Get noise points"},
|
|
1301
|
+
{"get_cube_3d_points", (PyCFunction)get_cube_3d_points, METH_VARARGS | METH_KEYWORDS, "Get 3D cube points"},
|
|
1302
|
+
{"get_sphere_3d_points", (PyCFunction)get_sphere_3d_points, METH_VARARGS | METH_KEYWORDS, "Get 3D sphere points"},
|
|
1303
|
+
{"get_cylinder_3d_points", (PyCFunction)get_cylinder_3d_points, METH_VARARGS | METH_KEYWORDS, "Get 3D cylinder points"},
|
|
1304
|
+
{"get_cone_3d_points", (PyCFunction)get_cone_3d_points, METH_VARARGS | METH_KEYWORDS, "Get 3D cone points"},
|
|
1305
|
+
{"get_torus_3d_points", (PyCFunction)get_torus_3d_points, METH_VARARGS | METH_KEYWORDS, "Get 3D torus points"},
|
|
1306
|
+
{"get_ico_sphere_3d_points", (PyCFunction)get_ico_sphere_3d_points, METH_VARARGS | METH_KEYWORDS, "Get 3D ico sphere points"},
|
|
1307
|
+
{"DrawPic", (PyCFunction)DrawPic, METH_VARARGS | METH_KEYWORDS, "Draw points"},
|
|
1308
|
+
{"GetKey", (PyCFunction)GetKey, METH_VARARGS | METH_KEYWORDS, "Get key without Enter"},
|
|
1309
|
+
{"StartPyCode", (PyCFunction)StartPyCode, METH_VARARGS | METH_KEYWORDS, "Start Python code"},
|
|
1310
|
+
{"KillPyScript", (PyCFunction)KillPyScript, METH_VARARGS | METH_KEYWORDS, "Kill Python script"},
|
|
1311
|
+
{"IsRunning", (PyCFunction)IsRunning, METH_VARARGS | METH_KEYWORDS, "Check if process is running"},
|
|
1312
|
+
{"GetCurrentPID", (PyCFunction)GetCurrentPID, METH_VARARGS | METH_KEYWORDS, "Get current PID"},
|
|
1313
|
+
{"WaitForProcess", (PyCFunction)WaitForProcess, METH_VARARGS | METH_KEYWORDS, "Wait for process"},
|
|
1314
|
+
{"GetProcessList", (PyCFunction)GetProcessList, METH_VARARGS | METH_KEYWORDS, "Get process list"},
|
|
1315
|
+
{"GetProcessInfo", (PyCFunction)GetProcessInfo, METH_VARARGS | METH_KEYWORDS, "Get process info"},
|
|
1316
|
+
{"clear_screen", (PyCFunction)clear_screen, METH_VARARGS | METH_KEYWORDS, "Clear screen"},
|
|
1317
|
+
{"animate_print", (PyCFunction)animate_print, METH_VARARGS | METH_KEYWORDS, "Animate print"},
|
|
1318
|
+
{"spinning_cursor", (PyCFunction)spinning_cursor, METH_VARARGS | METH_KEYWORDS, "Spinning cursor"},
|
|
1319
|
+
{"countdown", (PyCFunction)countdown, METH_VARARGS | METH_KEYWORDS, "Countdown"},
|
|
1320
|
+
{"GetPipVer", (PyCFunction)GetPipVer, METH_VARARGS | METH_KEYWORDS, "Get pip version"},
|
|
1321
|
+
{"Connect", (PyCFunction)Connect, METH_VARARGS | METH_KEYWORDS, "Connect to server"},
|
|
1322
|
+
{"Send", (PyCFunction)Send, METH_VARARGS | METH_KEYWORDS, "Send message"},
|
|
1323
|
+
{"Receive", (PyCFunction)Receive, METH_VARARGS | METH_KEYWORDS, "Receive message"},
|
|
1324
|
+
{"Disconnect", (PyCFunction)Disconnect, METH_VARARGS | METH_KEYWORDS, "Disconnect"},
|
|
1325
|
+
{"GetPublicIP", (PyCFunction)GetPublicIP, METH_VARARGS | METH_KEYWORDS, "Get public IP"},
|
|
1326
|
+
{"CreateServer", (PyCFunction)CreateServer, METH_VARARGS | METH_KEYWORDS, "Create server"},
|
|
1379
1327
|
{NULL, NULL, 0, NULL}
|
|
1380
1328
|
};
|
|
1381
1329
|
|
|
1382
|
-
static struct PyModuleDef
|
|
1330
|
+
static struct PyModuleDef coremodule = {
|
|
1383
1331
|
PyModuleDef_HEAD_INIT,
|
|
1384
|
-
"
|
|
1332
|
+
"core",
|
|
1385
1333
|
"The best Console Library of the Python.",
|
|
1386
1334
|
-1,
|
|
1387
|
-
|
|
1335
|
+
CoreMethods
|
|
1388
1336
|
};
|
|
1389
1337
|
|
|
1390
|
-
PyMODINIT_FUNC
|
|
1391
|
-
PyObject* module = PyModule_Create(&
|
|
1392
|
-
|
|
1338
|
+
PyMODINIT_FUNC PyInit_core(void) {
|
|
1339
|
+
PyObject* module = PyModule_Create(&coremodule);
|
|
1393
1340
|
PyModule_AddStringConstant(module, "RED", "\033[31m");
|
|
1394
1341
|
PyModule_AddStringConstant(module, "GREEN", "\033[32m");
|
|
1395
1342
|
PyModule_AddStringConstant(module, "BLUE", "\033[34m");
|
|
@@ -1404,6 +1351,5 @@ PyMODINIT_FUNC PyInit_ConsoleUtils(void) {
|
|
|
1404
1351
|
PyModule_AddStringConstant(module, "BG_BLUE", "\033[44m");
|
|
1405
1352
|
PyModule_AddStringConstant(module, "BOLD", "\033[1m");
|
|
1406
1353
|
PyModule_AddStringConstant(module, "UNDERLINE", "\033[4m");
|
|
1407
|
-
|
|
1408
1354
|
return module;
|
|
1409
1355
|
}
|