-
Notifications
You must be signed in to change notification settings - Fork 1
/
pretty-poly-primitives.h
179 lines (149 loc) · 6.73 KB
/
pretty-poly-primitives.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
/*
Pretty Poly 🦜 - super-sampling polygon renderer for low resource platforms.
Jonathan Williamson, August 2022
Examples, source, and more: https://github.com/lowfatcode/pretty-poly
MIT License https://github.com/lowfatcode/pretty-poly/blob/main/LICENSE
An easy way to render high quality graphics in embedded applications running
on resource constrained microcontrollers such as the Cortex M0 and up.
- Renders polygons: concave, self-intersecting, multi contour, holes, etc.
- C11 header only library: simply copy the header file into your project
- Tile based renderer: low memory footprint, cache coherency
- Low memory usage: ~4kB of heap memory required
- High speed on low resource platforms: optionally no floating point
- Antialiasing modes: X1 (none), X4 and X16 super sampling
- Bounds clipping: all results clipped to supplied clip rectangle
- Pixel format agnostic: renders a "tile" to blend into your framebuffer
- Support for hardware interpolators on rp2040 (thanks @MichaelBell!)
Contributor bwaaaaaarks! 🦜
@MichaelBell - lots of bug fixes, performance boosts, and suggestions.
@gadgetoid - integrating into the PicoVector library and testing.
*/
#ifndef PPP_INCLUDE_H
#define PPP_INCLUDE_H
#include "pretty-poly.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
PP_COORD_TYPE x, y, w, h; // coordinates
PP_COORD_TYPE s; // stroke thickness (0 == filled)
PP_COORD_TYPE r1, r2, r3, r4; // corner radii (r1 = top left then clockwise)
} ppp_rect_def;
typedef struct {
PP_COORD_TYPE x, y; // coordinates
PP_COORD_TYPE r; // radius
int e; // edge count
PP_COORD_TYPE s; // stroke thickness (0 == filled)
} ppp_regular_def;
typedef struct {
PP_COORD_TYPE x, y; // coordinates
PP_COORD_TYPE r; // radius
PP_COORD_TYPE s; // stroke thickness (0 == filled)
} ppp_circle_def;
typedef struct {
PP_COORD_TYPE x, y; // coordinates
PP_COORD_TYPE r; // radius
PP_COORD_TYPE s; // stroke thickness (0 == filled)
PP_COORD_TYPE f, t; // angle from and to
} ppp_arc_def;
pp_poly_t* ppp_rect(ppp_rect_def d);
pp_poly_t* ppp_regular(ppp_regular_def d);
pp_poly_t* ppp_circle(ppp_circle_def d);
pp_poly_t* ppp_arc(ppp_arc_def d);
#ifdef __cplusplus
}
#endif
#ifdef PPP_IMPLEMENTATION
void _pp_round_rect_corner_points(pp_path_t *path, PP_COORD_TYPE cx, PP_COORD_TYPE cy, PP_COORD_TYPE r, int q) {
float quality = 5; // higher the number, lower the quality - selected by experiment
int steps = ceil(r / quality) + 2; // + 2 to include start and end
float delta = -(M_PI / 2) / steps;
float theta = (M_PI / 2) * q; // select start theta for this quadrant
for(int i = 0; i <= steps; i++) {
PP_COORD_TYPE xo = sin(theta) * r, yo = cos(theta) * r;
pp_path_add_point(path, (pp_point_t){cx + xo, cy + yo});
theta += delta;
}
}
void _ppp_rrect_corner(pp_path_t *path, PP_COORD_TYPE cx, PP_COORD_TYPE cy, PP_COORD_TYPE r, int q) {
float quality = 5; // higher the number, lower the quality - selected by experiment
int steps = ceil(r / quality) + 2; // + 2 to include start and end
float delta = -(M_PI / 2) / steps;
float theta = (M_PI / 2) * q; // select start theta for this quadrant
for(int i = 0; i <= steps; i++) {
PP_COORD_TYPE xo = sin(theta) * r, yo = cos(theta) * r;
pp_path_add_point(path, (pp_point_t){cx + xo, cy + yo});
theta += delta;
}
}
void _ppp_rrect_path(pp_path_t *path, ppp_rect_def d) {
d.r1 == 0 ? pp_path_add_point(path, (pp_point_t){d.x, d.y}) : _ppp_rrect_corner(path, d.x + d.r1, d.y + d.r1, d.r1, 3);
d.r2 == 0 ? pp_path_add_point(path, (pp_point_t){d.x + d.w, d.y}) : _ppp_rrect_corner(path, d.x + d.w - d.r2, d.y + d.r2, d.r2, 2);
d.r3 == 0 ? pp_path_add_point(path, (pp_point_t){d.x + d.w, d.y + d.h}) : _ppp_rrect_corner(path, d.x + d.w - d.r3, d.y + d.h - d.r3, d.r3, 1);
d.r4 == 0 ? pp_path_add_point(path, (pp_point_t){d.x, d.y}) : _ppp_rrect_corner(path, d.x + d.r4, d.y + d.h - d.r4, d.r4, 0);
}
pp_poly_t* ppp_rect(ppp_rect_def d) {
pp_poly_t *poly = pp_poly_new();
pp_path_t *path = pp_poly_add_path(poly);
if(d.r1 == 0.0f && d.r2 == 0.0f && d.r3 == 0.0f && d.r4 == 0.0f) { // non rounded rect
pp_path_add_points(path, (pp_point_t[]){{d.x, d.y}, {d.x + d.w, d.y}, {d.x + d.w, d.y + d.h}, {d.x, d.y + d.h}}, 4);
if(d.s != 0) { // stroked, not filled
d.x += d.s; d.y += d.s; d.w -= 2 * d.s; d.h -= 2 * d.s;
pp_path_t *inner = pp_poly_add_path(poly);
pp_path_add_points(inner, (pp_point_t[]){{d.x, d.y}, {d.x + d.w, d.y}, {d.x + d.w, d.y + d.h}, {d.x, d.y + d.h}}, 4);
}
}else{ // rounded rect
_ppp_rrect_path(path, d);
if(d.s != 0) { // stroked, not filled
d.x += d.s; d.y += d.s; d.w -= 2 * d.s; d.h -= 2 * d.s;
d.r1 = _pp_max(0, d.r1 - d.s);
d.r2 = _pp_max(0, d.r2 - d.s);
d.r3 = _pp_max(0, d.r3 - d.s);
d.r4 = _pp_max(0, d.r4 - d.s);
pp_path_t *inner = pp_poly_add_path(poly);
_ppp_rrect_path(inner, d);
}
}
return poly;
}
pp_poly_t* ppp_regular(ppp_regular_def d) {
pp_poly_t *poly = pp_poly_new();
pp_path_t *path = pp_poly_add_path(poly);
pp_path_t *inner = d.s != 0.0f ? pp_poly_add_path(poly) : NULL;
for(int i = 0; i < d.e; i++) {
float theta = ((M_PI * 2.0f) / (float)d.e) * (float)i;
pp_path_add_point(path, (pp_point_t){sin(theta) * d.r + d.x, cos(theta) * d.r + d.y});
if(inner) {
pp_path_add_point(inner, (pp_point_t){sin(theta) * (d.r - d.s) + d.x, cos(theta) * (d.r - d.s) + d.y});
}
}
return poly;
}
pp_poly_t* ppp_circle(ppp_circle_def d) {
int e = _pp_max(8, d.r); // edge count
ppp_regular_def r = {d.x, d.y, d.r, e, d.s};
return ppp_regular(r);
}
pp_poly_t* ppp_arc(ppp_arc_def d) {
pp_poly_t *poly = pp_poly_new();
pp_path_t *path = pp_poly_add_path(poly);
pp_path_t *inner = d.s == 0.0f ? NULL : calloc(1, sizeof(pp_path_t));
// no thickness, so add centre point to make pie shape
if(!inner) pp_path_add_point(path, (pp_point_t){d.x, d.y});
d.f = d.f * (M_PI / 180.0f); d.t = d.t * (M_PI / 180.0f); // to radians
int s = _pp_max(8, d.r); float astep = (d.t - d.f) / s; float a = d.f;
for(int i = 0; i <= s; i++) {
pp_path_add_point(path, (pp_point_t){sin(a) * d.r + d.x, cos(a) * d.r + d.y});
if(inner) {
pp_path_add_point(inner, (pp_point_t){sin(d.t - (a - d.f)) * (d.r - d.s) + d.x, cos(d.t - (a - d.f)) * (d.r - d.s) + d.y});
}
a += astep;
}
if(inner) { // append the inner path
pp_path_add_points(path, inner->points, inner->count);
free(inner->points); free(inner);
}
return poly;
}
#endif // PPP_IMPLEMENTATION
#endif // PPP_INCLUDE_H