-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathEyeDrawer.h
325 lines (253 loc) · 10.2 KB
/
EyeDrawer.h
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/***************************************************
Copyright (c) 2020 Luis Llamas
(www.luisllamas.es)
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses
****************************************************/
#ifndef _EYEDRAWER_h
#define _EYEDRAWER_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "arduino.h"
#else
#include "WProgram.h"
#endif
#include <M5StickC.h>
#include "EyeConfig.h"
enum CornerType
{
T_R, T_L, B_L, B_R
};
class EyeDrawer
{
public:
static void Draw(TFT_eSprite& buffer, int16_t centerX, int16_t centerY, EyeConfig* config)
{
int32_t delta_y_top = config->Height * config->Slope_Top / 2.0;
int32_t delta_y_bottom = config->Height * config->Slope_Bottom / 2.0;
auto totalHeight = config->Height + delta_y_top - delta_y_bottom;
if (config->Radius_Bottom > 0 && config->Radius_Top > 0 && totalHeight - 1 < config->Radius_Bottom + config->Radius_Top)
{
int32_t corrected_radius_top = (float)config->Radius_Top * (totalHeight - 1) / (config->Radius_Bottom + config->Radius_Top);
int32_t corrected_radius_bottom = (float)config->Radius_Bottom * (totalHeight - 1) / (config->Radius_Bottom + config->Radius_Top);
config->Radius_Top = corrected_radius_top;
config->Radius_Bottom = corrected_radius_bottom;
}
int32_t TLc_x = centerY + config->OffsetY - config->Height / 2 + config->Radius_Top + delta_y_top;
int32_t TLc_y = centerX + config->OffsetX + config->Width / 2 - config->Radius_Top;
int32_t TL0_x = TLc_x;
int32_t TL0_y = TLc_y + config->Radius_Top;
int32_t TL1_x = TLc_x - config->Radius_Top;
int32_t TL1_y = TLc_y;
int32_t TRc_x = centerY + config->OffsetY - config->Height / 2 + config->Radius_Top - delta_y_top;
int32_t TRc_y = centerX + config->OffsetX - config->Width / 2 + config->Radius_Top;
int32_t TR0_x = TRc_x;
int32_t TR0_y = TRc_y - config->Radius_Top;
int32_t TR1_x = TRc_x - config->Radius_Top;
int32_t TR1_y = TRc_y;
int32_t BLc_x = centerY + config->OffsetY + config->Height / 2 - config->Radius_Bottom + delta_y_bottom;
int32_t BLc_y = centerX + config->OffsetX + config->Width / 2 - config->Radius_Bottom;
int32_t BL0_x = BLc_x;
int32_t BL0_y = BLc_y + config->Radius_Bottom;
int32_t BL1_x = BLc_x + config->Radius_Bottom;
int32_t BL1_y = BLc_y;
int32_t BRc_x = centerY + config->OffsetY + config->Height / 2 - config->Radius_Bottom - delta_y_bottom;
int32_t BRc_y = centerX + config->OffsetX - config->Width / 2 + config->Radius_Bottom;
int32_t BR0_x = BRc_x;
int32_t BR0_y = BRc_y - config->Radius_Bottom;
int32_t BR1_x = BRc_x + config->Radius_Bottom;
int32_t BR1_y = BRc_y;
int32_t iTLc_x = TL1_x;
int32_t iTLc_y = TL1_y - config->Inverse_Radius_Top;
int32_t iTL0_x = iTLc_x + config->Inverse_Radius_Top;
int32_t iTL0_y = iTLc_y;
int32_t iTL1_x = iTL0_x;
int32_t iTL1_y = iTLc_y + config->Inverse_Radius_Top;
int32_t iTRc_x = TR1_x;
int32_t iTRc_y = TR1_y + config->Inverse_Radius_Top;
int32_t iTR0_x = iTRc_x + config->Inverse_Radius_Top;
int32_t iTR0_y = iTRc_y;
int32_t iTR1_x = iTRc_x;
int32_t iTR1_y = iTRc_y - config->Inverse_Radius_Top;
int32_t iBLc_x = BL1_x;
int32_t iBLc_y = BL1_y - config->Inverse_Radius_Bottom;
int32_t iBL0_x = iBLc_x - config->Inverse_Radius_Bottom;
int32_t iBL0_y = iBLc_y;
int32_t iBL1_x = iBLc_x - config->Inverse_Radius_Bottom;
int32_t iBL1_y = iBLc_y;
int32_t iBRc_x = BR1_x;
int32_t iBRc_y = BR1_y + config->Inverse_Radius_Bottom;
int32_t iBR0_x = iBRc_x - config->Inverse_Radius_Bottom;
int32_t iBR0_y = iBRc_y;
int32_t iBR1_x = iBRc_x - config->Inverse_Radius_Bottom;
int32_t iBR1_y = iBRc_y;
int32_t min_c_x = max(TLc_x, TRc_x);
int32_t max_c_x = min(BLc_x, BRc_x);
int32_t min_c_y = max(TRc_y, BRc_y);
int32_t max_c_y = min(TLc_y, BLc_y);
int32_t min_e_x = max(TL1_x, TR1_x);
int32_t max_e_x = min(BL1_x, BR1_x);
int32_t min_e_y = max(BR0_y, TR0_y);
int32_t max_e_y = min(TL0_y, BL0_y);
EyeDrawer::FillReactangle(buffer, min_c_x, min_c_y, max_c_x, max_c_y, TFT_CYAN);
EyeDrawer::FillReactangle(buffer, TRc_x, min_e_y, BRc_x, min_c_y, TFT_CYAN);
EyeDrawer::FillReactangle(buffer, TLc_x, max_c_y, BLc_x, max_e_y, TFT_CYAN);
EyeDrawer::FillReactangle(buffer, min_e_x, TRc_y, min_c_x, TLc_y, TFT_CYAN);
EyeDrawer::FillReactangle(buffer, max_c_x, BRc_y, max_e_x, BLc_y, TFT_CYAN);
if (config->Slope_Top > 0)
{
EyeDrawer::FillRectangularTriangle(buffer, TL1_x, TL1_y, TR1_x, TR1_y, TFT_BLACK);
EyeDrawer::FillRectangularTriangle(buffer, TR1_x, TR1_y, TL1_x, TL1_y, TFT_CYAN);
}
else if (config->Slope_Top < 0)
{
EyeDrawer::FillRectangularTriangle(buffer, TR1_x, TR1_y, TL1_x, TL1_y, TFT_BLACK);
EyeDrawer::FillRectangularTriangle(buffer, TL1_x, TL1_y, TR1_x, TR1_y, TFT_CYAN);
}
if (config->Slope_Bottom > 0)
{
EyeDrawer::FillRectangularTriangle(buffer, BR1_x, BR1_y, BL1_x, BL1_y, TFT_BLACK);
EyeDrawer::FillRectangularTriangle(buffer, BL1_x, BL1_y, BR1_x, BR1_y, TFT_CYAN);
}
else if (config->Slope_Bottom < 0)
{
EyeDrawer::FillRectangularTriangle(buffer, BL1_x, BL1_y, BR1_x, BR1_y, TFT_BLACK);
EyeDrawer::FillRectangularTriangle(buffer, BR1_x, BR1_y, BL1_x, BL1_y, TFT_CYAN);
}
if (config->Radius_Top > 0)
{
EyeDrawer::FilleEllipseCorner(buffer, T_L, TLc_x, TLc_y, config->Radius_Top, config->Radius_Top, TFT_CYAN);
EyeDrawer::FilleEllipseCorner(buffer, T_R, TRc_x, TRc_y, config->Radius_Top, config->Radius_Top, TFT_CYAN);
}
if (config->Radius_Bottom > 0)
{
EyeDrawer::FilleEllipseCorner(buffer, B_L, BLc_x, BLc_y, config->Radius_Bottom, config->Radius_Bottom, TFT_CYAN);
EyeDrawer::FilleEllipseCorner(buffer, B_R, BRc_x, BRc_y, config->Radius_Bottom, config->Radius_Bottom, TFT_CYAN);
}
if (config->Inverse_Radius_Top > 0)
{
EyeDrawer::FillReactangle(buffer, min(iTLc_x, iTRc_x) - 1, iTRc_y, min(iTL0_x, iTR0_x) + 1, iTLc_y, TFT_BLACK);
if (config->Slope_Top > 0)
{
EyeDrawer::FillRectangularTriangle(buffer, iTL0_x, iTL0_y, iTR0_x, iTR0_y, TFT_BLACK);
}
else if (config->Slope_Top < 0)
{
EyeDrawer::FillRectangularTriangle(buffer, iTR0_x, iTR0_y, iTL0_x, iTL0_y, TFT_BLACK);
}
EyeDrawer::FilleEllipseCorner(buffer, B_L, iTLc_x - 1, iTLc_y, config->Inverse_Radius_Top, config->Inverse_Radius_Top, TFT_BLACK);
EyeDrawer::FilleEllipseCorner(buffer, B_R, iTRc_x - 1, iTRc_y, config->Inverse_Radius_Top, config->Inverse_Radius_Top, TFT_BLACK);
}
if (config->Inverse_Radius_Bottom > 0)
{
EyeDrawer::FilleEllipseCorner(buffer, T_L, iBLc_x + 1, iBLc_y, config->Inverse_Radius_Bottom, config->Inverse_Radius_Bottom, TFT_BLACK);
EyeDrawer::FilleEllipseCorner(buffer, T_R, iBRc_x + 1, iBRc_y, config->Inverse_Radius_Bottom, config->Inverse_Radius_Bottom, TFT_BLACK);
EyeDrawer::FillReactangle(buffer, max(iBL0_x, iBR0_x) + 1, iBRc_y - 1, max(iBLc_x, iBRc_x) + 1, iBLc_y + 1, TFT_BLACK);
if (config->Slope_Bottom > 0)
{
EyeDrawer::FillRectangularTriangle(buffer, iBR0_x, iBR0_y, iBL0_x, iBL0_y, TFT_BLACK);
}
else if (config->Slope_Bottom < 0)
{
EyeDrawer::FillRectangularTriangle(buffer, iBL0_x, iBL0_y, iBR0_x, iBR0_y, TFT_BLACK);
}
}
}
static void FilleEllipseCorner(TFT_eSprite& buffer, CornerType corner, int16_t x0, int16_t y0, int32_t rx, int32_t ry, uint16_t color)
{
if (rx < 2) return;
if (ry < 2) return;
int32_t x, y;
int32_t rx2 = rx * rx;
int32_t ry2 = ry * ry;
int32_t fx2 = 4 * rx2;
int32_t fy2 = 4 * ry2;
int32_t s;
if (corner == T_R)
{
for (x = 0, y = ry, s = 2 * ry2 + rx2 * (1 - 2 * ry); ry2 * x <= rx2 * y; x++) {
buffer.drawFastHLine(x0 - x, y0 - y, x, color);
if (s >= 0) {
s += fx2 * (1 - y);
y--;
}
s += ry2 * ((4 * x) + 6);
}
for (x = rx, y = 0, s = 2 * rx2 + ry2 * (1 - 2 * rx); rx2 * y <= ry2 * x; y++) {
buffer.drawFastHLine(x0 - x, y0 - y, x, color);
if (s >= 0) {
s += fy2 * (1 - x);
x--;
}
s += rx2 * ((4 * y) + 6);
}
}
else if (corner == B_R)
{
for (x = 0, y = ry, s = 2 * ry2 + rx2 * (1 - 2 * ry); ry2 * x <= rx2 * y; x++) {
buffer.drawFastHLine(x0, y0 - y, x, color);
if (s >= 0) {
s += fx2 * (1 - y);
y--;
}
s += ry2 * ((4 * x) + 6);
}
for (x = rx, y = 0, s = 2 * rx2 + ry2 * (1 - 2 * rx); rx2 * y <= ry2 * x; y++) {
buffer.drawFastHLine(x0, y0 - y, x, color);
if (s >= 0) {
s += fy2 * (1 - x);
x--;
}
s += rx2 * ((4 * y) + 6);
}
}
else if (corner == T_L)
{
for (x = 0, y = ry, s = 2 * ry2 + rx2 * (1 - 2 * ry); ry2 * x <= rx2 * y; x++) {
buffer.drawFastHLine(x0 - x, y0 + y - 1, x, color);
if (s >= 0) {
s += fx2 * (1 - y);
y--;
}
s += ry2 * ((4 * x) + 6);
}
for (x = rx, y = 0, s = 2 * rx2 + ry2 * (1 - 2 * rx); rx2 * y <= ry2 * x; y++) {
buffer.drawFastHLine(x0 - x, y0 + y - 1, x, color);
if (s >= 0) {
s += fy2 * (1 - x);
x--;
}
s += rx2 * ((4 * y) + 6);
}
}
else if (corner == B_L)
{
for (x = 0, y = ry, s = 2 * ry2 + rx2 * (1 - 2 * ry); ry2 * x <= rx2 * y; x++) {
buffer.drawFastHLine(x0, y0 + y - 1, x, color);
if (s >= 0) {
s += fx2 * (1 - y);
y--;
}
s += ry2 * ((4 * x) + 6);
}
for (x = rx, y = 0, s = 2 * rx2 + ry2 * (1 - 2 * rx); rx2 * y <= ry2 * x; y++) {
buffer.drawFastHLine(x0, y0 + y - 1, x, color);
if (s >= 0) {
s += fy2 * (1 - x);
x--;
}
s += rx2 * ((4 * y) + 6);
}
}
}
static void FillReactangle(TFT_eSprite& buffer, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t color)
{
buffer.fillRect(x0, y0, x1 - x0, y1 - y0, color);
}
static void FillRectangularTriangle(TFT_eSprite& buffer, int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t color)
{
buffer.fillTriangle(x0, y0, x1, y1, x1, y0, color);
}
};
#endif