forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlolwut.cc
186 lines (164 loc) · 5.64 KB
/
lolwut.cc
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
/*
* Copyright (C) 2020-present ScyllaDB
*/
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
#define _USE_MATH_DEFINES
#include <cmath>
#include <random>
#include <seastar/core/sstring.hh>
#include "redis/lolwut.hh"
#include "redis/version.hh"
using namespace seastar;
namespace redis {
class canvas {
const int _width, _height;
std::unique_ptr<char[]> _pixels;
public:
canvas(const int width, int height) : _width(width), _height(height), _pixels(std::make_unique<char[]>(width * height)) {}
void draw_pixel(const int x, const int y, const int color) {
if (x < 0 || x >= _width || y < 0 || y >= _height) {
return;
}
_pixels[x + y * _width] = color;
}
static void translate_pixels_group(const int byte, int8_t *output) {
const int code = 0x2800 + byte;
output[0] = 0xe0 | (code >> 12);
output[1] = 0x80 | ((code >> 6) & 0x3f);
output[2] = 0x80 | (code & 0x3f);
}
int get_pixel(const int x, const int y) {
if (x < 0 || x >= _width || y < 0 || y >= _height) {
return 0;
}
return _pixels[x + y * _width];
}
void draw_line(int x1, int y1, const int x2, const int y2, const int color) {
const int dx = abs(x2 - x1);
const int dy = abs(y2 - y1);
const int sx = (x1 < x2) ? 1 : -1;
const int sy = (y1 < y2) ? 1 : -1;
int err = dx - dy;
int e2;
while (1) {
draw_pixel(x1, y1, color);
if (x1 == x2 && y1 == y2) {
break;
}
e2 = err * 2;
if (e2 > -dy) {
err -= dy;
x1 += sx;
}
if (e2 < dx) {
err += dx;
y1 += sy;
}
}
}
void draw_square(const int x, const int y, float size, const float angle) {
int px[4], py[4];
size /= 1.4142135623;
size = round(size);
float k = M_PI/4 + angle;
for (int j = 0; j < 4; j++) {
px[j] = round(sin(k) * size + x);
py[j] = round(cos(k) * size + y);
k += M_PI/2;
}
for (int j = 0; j < 4; j++) {
draw_line(px[j], py[j], px[(j + 1) % 4], py[(j + 1) % 4], 1);
}
}
static canvas draw_schotter(const int cols, const int squares_per_row, const int squares_per_col) {
const int width = cols * 2;
const int padding = width > 4 ? 2 : 0;
const float side = static_cast<float>((width - padding * 2) / squares_per_row);
const int height = side * squares_per_col + padding * 2;
canvas c {width, height};
std::random_device seed_gen;
std::default_random_engine engine(seed_gen());
std::uniform_real_distribution<float> dist(0.0, 1.0);
for (int y = 0; y < squares_per_col; y++) {
for (int x = 0; x < squares_per_row; x++) {
int sx = x * side + side / 2 + padding;
int sy = y * side + side / 2 + padding;
float angle = 0;
if (y > 1) {
float r1 = dist(engine) / squares_per_col * y;
float r2 = dist(engine) / squares_per_col * y;
float r3 = dist(engine) / squares_per_col * y;
if (engine() % 2) {
r1 = -r1;
}
if (engine() % 2) {
r2 = -r2;
}
if (engine() % 2) {
r3 = -r3;
}
angle = r1;
sx += r2 * side / 3;
sy += r3 * side / 3;
}
c.draw_square(sx, sy, side, angle);
}
}
return c;
}
bytes render_canvas() {
// txt size = height * width * pixels 3byte + height * return code 1byte
bytes txt(bytes::initialized_later(), (_height / 4) * (_width / 2) * 3 + (_height / 4) * 1);
int i = 0;
for (int y = 0; y < _height; y += 4) {
for (int x = 0; x < _width; x += 2) {
int byte = 0;
int8_t unicode[3];
if (get_pixel(x, y)) {
byte |= (1 << 0);
}
if (get_pixel(x, y + 1)) {
byte |= (1 << 1);
}
if (get_pixel(x, y + 2)) {
byte |= (1 << 2);
}
if (get_pixel(x + 1, y)) {
byte |= (1 << 3);
}
if (get_pixel(x + 1, y + 1)) {
byte |= (1 << 4);
}
if (get_pixel(x + 1, y + 2)) {
byte |= (1 << 5);
}
if (get_pixel(x, y + 3)) {
byte |= (1 << 6);
}
if (get_pixel(x + 1, y + 3)) {
byte |= (1 << 7);
}
translate_pixels_group(byte, unicode);
std::memcpy(&txt[i], &unicode[0], 3);
i += 3;
}
if (y != _height - 1) {
txt[i++] = '\n';
}
}
return txt;
}
};
future<bytes> lolwut5(const int cols, const int squares_per_row, const int squares_per_col) {
auto c = canvas::draw_schotter(cols, squares_per_row, squares_per_col);
auto rendered = c.render_canvas();
const bytes msg {"\nGeorg Nees - schotter, plotter on paper, 1968. Redis ver. "};
const bytes nr {"\n"};
rendered += msg;
rendered += redis_version;
rendered += nr;
return make_ready_future<bytes>(rendered);
}
}