This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgfx_2d.h
1180 lines (1012 loc) · 30.7 KB
/
gfx_2d.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef P3DT_GFX_2D_H
#define P3DT_GFX_2D_H
#ifdef OSW_EMULATOR
#include <iostream>
#include "FakeArduino.h"
#else
#include <Arduino.h>
#endif
#include "gfx_util.h"
#include "math_angles.h"
enum CIRC_OPT { DRAW_UPPER_RIGHT, DRAW_UPPER_LEFT, DRAW_LOWER_RIGHT, DRAW_LOWER_LEFT, DRAW_ALL };
class DrawPixel {
public:
DrawPixel() {}
virtual void drawPixel(int32_t x, int32_t y, uint16_t color){};
};
class Graphics2D {
public:
Graphics2D(uint16_t w_, uint16_t h_, uint8_t chunkHeight_, bool isRound_ = false, bool allocatePsram_ = false)
: width(w_), height(h_), chunkHeight(chunkHeight_), isRound(isRound_), allocatePsram(allocatePsram_) {
enableBuffer();
maskEnabled = false;
maskColor = rgb565(0, 0, 0);
alphaEnabled = false;
}
void enableBuffer() {
drawPixelCallback = NULL;
uint16_t numChunks = height / chunkHeight;
buffer = new uint16_t*[numChunks];
if (isRound) {
missingPixelColor = rgb565(128, 128, 128);
chunkXOffsets = new uint16_t[numChunks];
chunkWidths = new uint16_t[numChunks];
for (uint16_t i = 0; i < numChunks; i++) {
uint16_t y = i * chunkHeight;
float y1 = (y + (y < height / 2 ? chunkHeight : 0)) - height / 2.0;
float d = sqrt(120 * 120 - y1 * y1);
uint16_t xOffset = 120 - d;
uint16_t chunkWidth = ceil(d * 2);
chunkXOffsets[i] = xOffset;
chunkWidths[i] = chunkWidth;
// Serial.print("Chunk: ");
// Serial.println(i);
// Serial.print(" Width: ");
// Serial.println(chunkWidth);
// Serial.print(" chunkHeight: ");
// Serial.println(chunkHeight);
// Serial.print(" Size: ");
// Serial.println(chunkWidth * chunkHeight);
// buffer[i] = new uint16_t[chunkWidth * chunkHeight];
if (allocatePsram) {
#if defined(GPS_EDITION) || defined(GPS_EDITION_ROTATED)
buffer[i] = (uint16_t*)ps_malloc(width * chunkHeight * sizeof(uint16_t));
#else
buffer[i] = new uint16_t[width * chunkHeight]();
#endif
} else {
buffer[i] = new uint16_t[width * chunkHeight]();
}
}
} else {
for (uint16_t i = 0; i < numChunks; i++) {
// buffer[i] = new uint16_t[width * chunkHeight];
// (uint8_t*) malloc( BufferSize * sizeof(uint8_t) )
if (allocatePsram) {
#if defined(GPS_EDITION) || defined(GPS_EDITION_ROTATED)
buffer[i] = (uint16_t*)ps_malloc(width * chunkHeight * sizeof(uint16_t));
#else
buffer[i] = (uint16_t*)malloc(width * chunkHeight * sizeof(uint16_t));
#endif
} else {
buffer[i] = (uint16_t*)malloc(width * chunkHeight * sizeof(uint16_t));
}
}
}
}
bool hasBuffer() { return drawPixelCallback == NULL; }
void disableBuffer(DrawPixel* callback) { //
delay(1000);
drawPixelCallback = callback;
uint16_t numChunks = height / chunkHeight;
for (uint16_t i = 0; i < numChunks; i++) {
delete[] buffer[i];
buffer[i] = NULL;
}
delete[] buffer;
buffer = NULL;
// delete[] chunkXOffsets;
// chunkXOffsets = NULL;
// delete[] chunkWidths;
// chunkWidths = NULL;
}
~Graphics2D() {
uint16_t numChunks = height / chunkHeight;
for (uint16_t i = 0; i < numChunks; i++) {
delete[] buffer[i];
buffer[i] = NULL;
}
delete[] buffer;
buffer = NULL;
// delete[] chunkXOffsets;
// chunkXOffsets = NULL;
// delete[] chunkWidths;
// chunkWidths = NULL;
}
uint16_t numChunks() { return height / chunkHeight; }
uint16_t* getChunk(uint8_t chunkId) { return buffer[chunkId]; }
uint8_t getChunkHeight() { return chunkHeight; }
uint16_t getChunkOffset(uint8_t chunkId) { return isRound ? chunkXOffsets[chunkId] : 0; }
uint16_t getChunkWidth(uint8_t chunkId) { return isRound ? chunkWidths[chunkId] : width; }
uint16_t getHeight() { return height; }
uint16_t getWidth() { return width; }
bool isMaskEnabled() { return maskEnabled; }
void enableMask(uint16_t color) {
maskEnabled = true;
maskColor = color;
}
void disableMask() { maskColor = false; }
void enableAlpha(float a) {
alpha = a;
alphaEnabled = true;
}
void disableAplha() { alphaEnabled = false; }
void setMissingPixelColor(uint16_t color) { missingPixelColor = color; }
uint16_t getMissingPixelColor(void) { return missingPixelColor; }
// no other functions should be allowed to access the buffer in write mode due to the chunk mapping
/**
* @brief Draw a pixel of color 'color' a x-y position.
*
* @param x x axis coordinate
* @param y y axis coordinate
* @param color color code of the pixel
*/
void drawPixel(int32_t x, int32_t y, uint16_t color) { drawPixelClipped(x, y, color); }
void drawPixelClipped(int32_t x, int32_t y, uint16_t color) {
if (x >= width || y >= height || x < 0 || y < 0) {
return;
}
if (maskEnabled && color == maskColor) {
return;
}
// if we have a pixel callback, there is now buffer
// draw with the callback and return..
if (drawPixelCallback != NULL) {
drawPixelCallback->drawPixel(x, y, color);
return;
}
uint8_t chunkId = y / chunkHeight;
int16_t chunkY = y - chunkId * chunkHeight;
//
if (alphaEnabled) {
if (isRound && isInsideChunk(x, y)) {
int16_t chunkX = x - chunkXOffsets[chunkId];
color = blend(buffer[chunkId][chunkX + chunkY * chunkWidths[chunkId]], color, alpha);
} else {
color = blend(buffer[chunkId][x + chunkY * width], color, alpha);
}
}
if (isRound && isInsideChunk(x, y)) {
int16_t chunkX = x - chunkXOffsets[chunkId];
buffer[chunkId][chunkX + chunkY * chunkWidths[chunkId]] = color;
} else if (!isRound) { // fix for round module
buffer[chunkId][x + chunkY * width] = color;
}
}
uint16_t getPixel(uint16_t x, uint16_t y) {
if (x >= width || y >= height) {
return 0;
}
uint8_t chunkId = y / chunkHeight;
uint16_t chunkY = y - chunkId * chunkHeight;
// printf("chunkid %d, offetY %d for y=%d and chunkHeight=%d\n", chunkId, chunkY, y, chunkHeight);
if (isRound) {
// TODO: check if inside chunk
if (isInsideChunk(x, y)) {
uint16_t chunkX = x - chunkXOffsets[chunkId];
return buffer[chunkId][chunkX + chunkY * chunkWidths[chunkId]];
} else {
return missingPixelColor;
}
} else {
return buffer[chunkId][x + chunkY * width];
}
}
bool isInsideChunk(uint16_t x, uint16_t y) {
uint8_t chunkId = y / chunkHeight;
// uint16_t chunkY = y - chunkId * chunkHeight;
uint16_t chunkOffset = chunkXOffsets[chunkId];
uint16_t chunkWidth = chunkWidths[chunkId];
bool xFit = chunkOffset < x && x < chunkOffset + chunkWidth;
// y always fits, because we chunk in rows
return xFit;
}
/**
* @brief Draw an horizontal line from the point (x,y) to an other horizontal point at h pixels
*
* @param x x-axis of the start point
* @param y y-axis of the start point
* @param w width of the horizontal line
* @param color color code of the line
*/
void drawHLine(int32_t x, int32_t y, uint16_t w, uint16_t color) {
for (uint16_t i = 0; i < w; i++) {
drawPixel(x + i, y, color);
}
}
/**
* @brief Draw a vertical line from the bottom point (x,y) to an other vertical point at h pixels
*
* @param x x-axis of the start point
* @param y y-axis of the start point
* @param h height of the vertical line
* @param color color code of the line
*/
void drawVLine(int32_t x, int32_t y, uint16_t h, uint16_t color) {
for (uint16_t i = 0; i < h; i++) {
drawPixel(x, y + i, color);
}
}
void drawFrame(int32_t x, int32_t y, uint16_t w, uint16_t h, uint16_t color) {
drawHLine(x, y, w, color);
drawHLine(x, y + h, w, color);
drawVLine(x, y, h, color);
drawVLine(x + w, y, h, color);
}
void fillFrame(int32_t x0, int32_t y0, uint16_t w, uint16_t h, uint16_t color) {
for (uint16_t y = y0; y < y0 + h; y++) {
drawHLine(x0, y, w, color);
}
}
/**
* Draw line from (x1,y1) to (x2,y2) point with color
*
* @param x1
* @param y1
* @param x2
* @param y2
* @param color
*/
void drawLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint16_t color) { // see p3dt_gfx_2d_license.txt
// printf("\ndrawLine(%d, %d, %d, %d)",x1,y1,x2,y2);
// see p3dt_gfx_2d_license.txt
int32_t tmp;
int32_t x, y;
int32_t dx, dy;
int32_t err;
int32_t ystep;
uint8_t swapxy = 0;
/* no intersection check at the moment, should be added... */
if (x1 > x2)
dx = x1 - x2;
else
dx = x2 - x1;
if (y1 > y2)
dy = y1 - y2;
else
dy = y2 - y1;
if (dy > dx) {
swapxy = 1;
tmp = dx;
dx = dy;
dy = tmp;
tmp = x1;
x1 = y1;
y1 = tmp;
tmp = x2;
x2 = y2;
y2 = tmp;
}
if (x1 > x2) {
tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
}
err = dx >> 1;
if (y2 > y1)
ystep = 1;
else
ystep = -1;
y = y1;
if (x2 == 0xffff) x2--;
for (x = x1; x <= x2; x++) {
if (swapxy == 0)
drawPixel(x, y, color);
else
drawPixel(y, x, color);
err -= abs(dy);
if (err < 0) {
y += ystep;
err += dx;
}
}
}
/**
* @brief Draw a line between (x1,y1) and (x2,y2) with a thick of radius and with specific color
*
* Radius is a multiple of 4 pixels.
*
* @param x1 x-axis of the start point
* @param y1 y-axis of the start point
* @param x2 x-axis of the end point
* @param y2 y-axis of the end point
* @param radius radius of the line. Example : radius = 1 give a line of 4 px of diameter, radius 2 -> 8px, etc....
* @param color color code use to draw the line.
* @param highQuality
*/
void drawThickLine(int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint8_t radius, uint16_t color,
bool highQuality = false) { // see p3dt_gfx_2d_license.txt
// see p3dt_gfx_2d_license.txt
int32_t tmp;
int32_t x, y;
int32_t dx, dy;
int32_t err;
int32_t ystep;
uint8_t swapxy = 0;
/* no intersection check at the moment, should be added... */
if (x1 > x2)
dx = x1 - x2;
else
dx = x2 - x1;
if (y1 > y2)
dy = y1 - y2;
else
dy = y2 - y1;
if (dy > dx) {
swapxy = 1;
tmp = dx;
dx = dy;
dy = tmp;
tmp = x1;
x1 = y1;
y1 = tmp;
tmp = x2;
x2 = y2;
y2 = tmp;
}
if (x1 > x2) {
tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
}
err = dx >> 1;
if (y2 > y1)
ystep = 1;
else
ystep = -1;
y = y1;
if (x2 == 0xffff) x2--;
for (x = x1; x <= x2; x++) {
if (swapxy == 0) {
if (highQuality) {
fillCircle(x, y, radius, color);
} else {
drawCircle(x, y, radius, color);
if (radius > 2) {
drawCircle(x, y, radius - 1, color);
}
if (radius > 3) {
drawCircle(x, y, radius - 2, color);
}
}
} else {
if (highQuality) {
fillCircle(y, x, radius, color);
} else {
drawCircle(y, x, radius, color);
if (radius > 2) {
drawCircle(y, x, radius - 1, color);
}
if (radius > 3) {
drawCircle(y, x, radius - 2, color);
}
}
}
err -= (uint8_t)dy;
if (err < 0) {
y += (uint16_t)ystep;
err += (uint16_t)dx;
}
}
}
void drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color) {
drawLine(x0, y0, x1, y1, color);
drawLine(x1, y1, x2, y2, color);
drawLine(x2, y2, x0, y0, color);
}
/*
* "Complex" Stuff:
*/
void _drawCircleSection(uint16_t x, uint16_t y, uint16_t x0, uint16_t y0, uint16_t color,
CIRC_OPT option) { // see p3dt_gfx_2d_license.txt
if (option == DRAW_UPPER_RIGHT || option == DRAW_ALL) {
drawPixel(x0 + x, y0 - y, color);
drawPixel(x0 + y, y0 - x, color);
}
if (option == DRAW_UPPER_LEFT || option == DRAW_ALL) {
drawPixel(x0 - x, y0 - y, color);
drawPixel(x0 - y, y0 - x, color);
}
if (option == DRAW_LOWER_RIGHT || option == DRAW_ALL) {
drawPixel(x0 + x, y0 + y, color);
drawPixel(x0 + y, y0 + x, color);
}
if (option == DRAW_LOWER_LEFT || option == DRAW_ALL) {
drawPixel(x0 - x, y0 + y, color);
drawPixel(x0 - y, y0 + x, color);
}
}
/**
* @brief Draw a circle
*
* @param x0 x-axis of the center of the circle
* @param y0 y-axis of the center of the circle
* @param rad radius of the circle
* @param color color code of the circle
* @param option
*/
void drawCircle(uint16_t x0, uint16_t y0, uint16_t rad, uint16_t color,
CIRC_OPT option = DRAW_ALL) { // see p3dt_gfx_2d_license.txt
float f;
float ddFx;
float ddFy;
float x;
float y;
f = 1;
f -= rad;
ddFx = 1;
ddFy = 0;
ddFy -= rad;
ddFy *= 2;
x = 0;
y = rad;
_drawCircleSection(x, y, x0, y0, color, option);
while (x < y) {
if (f >= 0) {
y--;
ddFy += 2;
f += ddFy;
}
x++;
ddFx += 2;
f += ddFx;
_drawCircleSection(x, y, x0, y0, color, option);
}
}
void _fillCircleSection(uint16_t x, uint16_t y, uint16_t x0, uint16_t y0, uint16_t color,
CIRC_OPT option) { // see p3dt_gfx_2d_license.txt
if (option == DRAW_UPPER_RIGHT || option == DRAW_ALL) {
drawVLine(x0 + x, y0 - y, y + 1, color);
drawVLine(x0 + y, y0 - x, x + 1, color);
}
if (option == DRAW_UPPER_LEFT || option == DRAW_ALL) {
drawVLine(x0 - x, y0 - y, y + 1, color);
drawVLine(x0 - y, y0 - x, x + 1, color);
}
if (option == DRAW_LOWER_RIGHT || option == DRAW_ALL) {
drawVLine(x0 + x, y0, y + 1, color);
drawVLine(x0 + y, y0, x + 1, color);
}
if (option == DRAW_LOWER_LEFT || option == DRAW_ALL) {
drawVLine(x0 - x, y0, y + 1, color);
drawVLine(x0 - y, y0, x + 1, color);
}
}
void fillCircle(uint16_t x0, uint16_t y0, uint16_t rad, uint16_t color,
CIRC_OPT option = DRAW_ALL) { // see p3dt_gfx_2d_license.txt
float f;
float ddFx;
float ddFy;
float x;
float y;
f = 1;
f -= rad;
ddFx = 1;
ddFy = 0;
ddFy -= rad;
ddFy *= 2;
x = 0;
y = rad;
_fillCircleSection(x, y, x0, y0, color, option);
while (x < y) {
if (f >= 0) {
y--;
ddFy += 2;
f += ddFy;
}
x++;
ddFx += 2;
f += ddFx;
_fillCircleSection(x, y, x0, y0, color, option);
}
}
void _drawEllipseSection(uint16_t x, uint16_t y, uint16_t x0, uint16_t y0, uint16_t color,
CIRC_OPT option = DRAW_ALL) { // see p3dt_gfx_2d_license.txt
/* upper right */
if (option == DRAW_UPPER_RIGHT || option == DRAW_ALL) {
drawPixel(x0 + x, y0 - y, color);
}
/* upper left */
if (option == DRAW_UPPER_LEFT || option == DRAW_ALL) {
drawPixel(x0 - x, y0 - y, color);
}
/* lower right */
if (option == DRAW_LOWER_RIGHT || option == DRAW_ALL) {
drawPixel(x0 + x, y0 + y, color);
}
/* lower left */
if (option == DRAW_LOWER_LEFT || option == DRAW_ALL) {
drawPixel(x0 - x, y0 + y, color);
}
}
void drawEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, uint16_t color,
CIRC_OPT option = DRAW_ALL) { // see p3dt_gfx_2d_license.txt
float x;
float y;
float xchg;
float ychg;
float err;
float rxrx2;
float ryry2;
float stopx;
float stopy;
rxrx2 = rx;
rxrx2 *= rx;
rxrx2 *= 2;
ryry2 = ry;
ryry2 *= ry;
ryry2 *= 2;
x = rx;
y = 0;
xchg = 1;
xchg -= rx;
xchg -= rx;
xchg *= ry;
xchg *= ry;
ychg = rx;
ychg *= rx;
err = 0;
stopx = ryry2;
stopx *= rx;
stopy = 0;
while (stopx >= stopy) {
_drawEllipseSection(x, y, x0, y0, color, option);
y++;
stopy += rxrx2;
err += ychg;
ychg += rxrx2;
if (2 * err + xchg > 0) {
x--;
stopx -= ryry2;
err += xchg;
xchg += ryry2;
}
}
x = 0;
y = ry;
xchg = ry;
xchg *= ry;
ychg = 1;
ychg -= ry;
ychg -= ry;
ychg *= rx;
ychg *= rx;
err = 0;
stopx = 0;
stopy = rxrx2;
stopy *= ry;
while (stopx <= stopy) {
_drawEllipseSection(x, y, x0, y0, color, option);
x++;
stopx += ryry2;
err += xchg;
xchg += ryry2;
if (2 * err + ychg > 0) {
y--;
stopy -= rxrx2;
err += ychg;
ychg += rxrx2;
}
}
}
void _fillEllipseSection(uint16_t x, uint16_t y, uint16_t x0, uint16_t y0, uint16_t color,
CIRC_OPT option = DRAW_ALL) { // see p3dt_gfx_2d_license.txt
/* upper right */
if (option == DRAW_UPPER_RIGHT || option == DRAW_ALL) {
drawVLine(x0 + x, y0 - y, y + 1, color);
}
/* upper left */
if (option == DRAW_UPPER_LEFT || option == DRAW_ALL) {
drawVLine(x0 - x, y0 - y, y + 1, color);
}
/* lower right */
if (option == DRAW_LOWER_RIGHT || option == DRAW_ALL) {
drawVLine(x0 + x, y0, y + 1, color);
}
/* lower left */
if (option == DRAW_LOWER_LEFT || option == DRAW_ALL) {
drawVLine(x0 - x, y0, y + 1, color);
}
}
void fillEllipse(uint16_t x0, uint16_t y0, uint16_t rx, uint16_t ry, uint16_t color,
CIRC_OPT option = DRAW_ALL) { // see p3dt_gfx_2d_license.txt
float x;
float y;
float xchg;
float ychg;
float err;
float rxrx2;
float ryry2;
float stopx;
float stopy;
rxrx2 = rx;
rxrx2 *= rx;
rxrx2 *= 2;
ryry2 = ry;
ryry2 *= ry;
ryry2 *= 2;
x = rx;
y = 0;
xchg = 1;
xchg -= rx;
xchg -= rx;
xchg *= ry;
xchg *= ry;
ychg = rx;
ychg *= rx;
err = 0;
stopx = ryry2;
stopx *= rx;
stopy = 0;
while (stopx >= stopy) {
_fillEllipseSection(x, y, x0, y0, color, option);
y++;
stopy += rxrx2;
err += ychg;
ychg += rxrx2;
if (2 * err + xchg > 0) {
x--;
stopx -= ryry2;
err += xchg;
xchg += ryry2;
}
}
x = 0;
y = ry;
xchg = ry;
xchg *= ry;
ychg = 1;
ychg -= ry;
ychg -= ry;
ychg *= rx;
ychg *= rx;
err = 0;
stopx = 0;
stopy = rxrx2;
stopy *= ry;
while (stopx <= stopy) {
_fillEllipseSection(x, y, x0, y0, color, option);
x++;
stopx += ryry2;
err += xchg;
xchg += ryry2;
if (2 * err + ychg > 0) {
y--;
stopy -= rxrx2;
err += ychg;
ychg += rxrx2;
}
}
}
void drawRFrame(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t r,
uint16_t color) { // see p3dt_gfx_2d_license.txt
uint16_t xl;
uint16_t yu;
xl = x;
xl += r;
yu = y;
yu += r;
{
uint16_t yl;
uint16_t xr;
xr = x;
xr += w;
xr -= r;
xr -= 1;
yl = y;
yl += h;
yl -= r;
yl -= 1;
drawCircle(xl, yu, r, color, DRAW_UPPER_LEFT);
drawCircle(xr, yu, r, color, DRAW_UPPER_RIGHT);
drawCircle(xl, yl, r, color, DRAW_LOWER_LEFT);
drawCircle(xr, yl, r, color, DRAW_LOWER_RIGHT);
}
{
uint16_t ww;
uint16_t hh;
ww = w;
ww -= r;
ww -= r;
hh = h;
hh -= r;
hh -= r;
xl++;
yu++;
if (ww >= 3) {
ww -= 2;
h--;
drawHLine(xl, y, ww, color);
drawHLine(xl, y + h, ww, color);
}
if (hh >= 3) {
hh -= 2;
w--;
drawVLine(x, yu, hh, color);
drawVLine(x + w, yu, hh, color);
}
}
}
void fillRFrame(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t r,
uint16_t color) { // see p3dt_gfx_2d_license.txt
if(h < 2 * r)
//Prevent infinite looping
return;
uint16_t xl;
uint16_t yu;
uint16_t yl;
uint16_t xr;
xl = x;
xl += r;
yu = y;
yu += r;
xr = x;
xr += w;
xr -= r;
xr -= 1;
yl = y;
yl += h;
yl -= r;
yl -= 1;
fillCircle(xl, yu, r, color, DRAW_UPPER_LEFT);
fillCircle(xr, yu, r, color, DRAW_UPPER_RIGHT);
fillCircle(xl, yl, r, color, DRAW_LOWER_LEFT);
fillCircle(xr, yl, r, color, DRAW_LOWER_RIGHT);
{
uint16_t ww;
uint16_t hh;
ww = w;
ww -= r;
ww -= r;
xl++;
yu++;
if (ww >= 3) {
ww -= 2;
fillFrame(xl, y, ww, r + 1, color);
fillFrame(xl, yl, ww, r + 1, color);
}
hh = h;
hh -= r;
hh -= r;
// h--;
if (hh >= 3) {
hh -= 2;
fillFrame(x, yu, w, hh, color);
}
}
}
void drawTick(uint8_t cx, uint8_t cy, uint8_t r1, uint8_t r2, float angle, uint16_t color) {
drawLine(rpx(cx, r1, angle), rpy(cy, r1, angle), rpx(cx, r2, angle), rpy(cy, r2, angle), color);
}
void drawThickTick(uint8_t cx, uint8_t cy, uint8_t r1, uint8_t r2, float angle, uint8_t radius, uint16_t color,
bool highQuality = false) {
drawThickLine(rpx(cx, r1, angle), rpy(cy, r1, angle), rpx(cx, r2, angle), rpy(cy, r2, angle), radius, color,
highQuality);
}
/**
* @brief Draw N ticks around the clock to visualize the hours.
*
* @param cx center x axis
* @param cy center y axis
* @param r1 radius from the begin of the tick.
* @param r2 radius from the end of the tick.
* @param nTicks number of ticks to draw
* @param color color code
*/
void drawNTicks(uint8_t cx, uint8_t cy, uint8_t r1, uint8_t r2, uint8_t nTicks, uint16_t color) {
const float deltaAngle = 360.0 / nTicks;
for (uint16_t h = 0; h < nTicks; h++) {
drawTick(cx, cy, r1, r2, h * deltaAngle, color);
}
}
/**
* @brief Draw 12 ticks around the clock to visualize the hours.
*
* @param cx center x axis
* @param cy center y axis
* @param r1 radius from the begin of the tick.
* @param r2 radius from the end of the tick.
* @param color color code
*/
void drawHourTicks(uint8_t cx, uint8_t cy, uint8_t r1, uint8_t r2, uint16_t color) {
drawNTicks(cx, cy, r1, r2, 12, color);
}
/**
* @brief Draw the ticks around the clock to visualize the minutes.
*
* 60 ticks will be drawn around the clock.
*
* @param cx center x axis
* @param cy center y axis
* @param r1 rayon
* @param r2
* @param color color code
*/
void drawMinuteTicks(uint8_t cx, uint8_t cy, uint8_t r1, uint8_t r2, uint16_t color) {
drawNTicks(cx, cy, r1, r2, 60, color);
}
void drawArc(int16_t x, int16_t y, int16_t r1, int16_t r2, float start, float end, uint16_t color) {
this->drawArc(x, y, start, end, 1, r1, r2-r1, color); // This _should_ be the equivalent call...
}
/**
* Draw an arc
*
* @param cx Arc X center coordinates
* @param cy Arc Y center coordinates
* @param start Beginning angle of the arc (in deg). O° is equivalent to 12AM
* @param stop End angle of the arc (in deg).
* @param steps Number of lines that will compose the arc.
* @param radius Radius of the arc from the cx/cy center
* @param lineRadius Radius of the line. Example : radius = 1 give a line of 4 px of diameter, radius 2 -> 8px,
* etc....
* @param color Color code of the arc
* @param highQuality
*/
void drawArc(int32_t cx, int32_t cy, float start, float stop, uint16_t steps, uint16_t radius, uint8_t lineRadius,
uint16_t color, bool highQuality = false) {
int32_t x1 = rpx(cx, radius, start);
int32_t y1 = rpy(cy, radius, start);
// printf("\ndraw from %f,%f in %d steps", start, stop, steps);
float arcLength = stop - start;
for (uint16_t i = 1; i <= steps; i++) {
float segmentLength = i * (arcLength / steps);
// printf("\n rpx(%d, %d, %f + %f)", cx, radius, start, segmentLength);
int32_t x2 = rpx(cx, radius, start + segmentLength);
int32_t y2 = rpy(cy, radius, start + segmentLength);
// printf("\n gfx2d.drawLine(%d, %d, %d, %d, color);", x1, y1, x2, y2);
drawThickLine(x1, y1, x2, y2, lineRadius, color, highQuality);