Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
CTCaer
GitHub Repository: CTCaer/hekate
Path: blob/master/bdk/libs/lvgl/lv_draw/lv_draw_arc.c
1476 views
1
/**
2
* @file lv_draw_arc.c
3
*
4
*/
5
6
/*********************
7
* INCLUDES
8
*********************/
9
#include "lv_draw_arc.h"
10
#include "../lv_misc/lv_math.h"
11
12
/*********************
13
* DEFINES
14
*********************/
15
16
/**********************
17
* TYPEDEFS
18
**********************/
19
20
/**********************
21
* STATIC PROTOTYPES
22
**********************/
23
static uint16_t fast_atan2(int x, int y);
24
static void ver_line(lv_coord_t x, lv_coord_t y, const lv_area_t * mask, lv_coord_t len, lv_color_t color, lv_opa_t opa);
25
static void hor_line(lv_coord_t x, lv_coord_t y, const lv_area_t * mask, lv_coord_t len, lv_color_t color, lv_opa_t opa);
26
static bool deg_test_norm(uint16_t deg, uint16_t start, uint16_t end);
27
static bool deg_test_inv(uint16_t deg, uint16_t start, uint16_t end);
28
29
/**********************
30
* STATIC VARIABLES
31
**********************/
32
33
/**********************
34
* MACROS
35
**********************/
36
37
/**********************
38
* GLOBAL FUNCTIONS
39
**********************/
40
41
/**
42
* Draw an arc. (Can draw pie too with great thickness.)
43
* @param center_x the x coordinate of the center of the arc
44
* @param center_y the y coordinate of the center of the arc
45
* @param radius the radius of the arc
46
* @param mask the arc will be drawn only in this mask
47
* @param start_angle the start angle of the arc (0 deg on the bottom, 90 deg on the right)
48
* @param end_angle the end angle of the arc
49
* @param style style of the arc (`body.thickness`, `body.main_color`, `body.opa` is used)
50
* @param opa_scale scale down all opacities by the factor
51
*/
52
void lv_draw_arc(lv_coord_t center_x, lv_coord_t center_y, uint16_t radius, const lv_area_t * mask,
53
uint16_t start_angle, uint16_t end_angle, const lv_style_t * style, lv_opa_t opa_scale)
54
{
55
lv_coord_t thickness = style->line.width;
56
if(thickness > radius) thickness = radius;
57
58
lv_coord_t r_out = radius;
59
lv_coord_t r_in = r_out - thickness;
60
int16_t deg_base;
61
int16_t deg;
62
lv_coord_t x_start[4];
63
lv_coord_t x_end[4];
64
65
lv_color_t color = style->line.color;
66
lv_opa_t opa = opa_scale == LV_OPA_COVER ? style->body.opa : (uint16_t)((uint16_t) style->body.opa * opa_scale) >> 8;
67
68
69
bool (*deg_test)(uint16_t, uint16_t, uint16_t);
70
if(start_angle <= end_angle) deg_test = deg_test_norm;
71
else deg_test = deg_test_inv;
72
73
if(deg_test(270, start_angle, end_angle)) hor_line(center_x - r_out + 1, center_y, mask, thickness - 1, color, opa); // Left Middle
74
if(deg_test(90, start_angle, end_angle)) hor_line(center_x + r_in, center_y, mask, thickness - 1, color, opa); // Right Middle
75
if(deg_test(180, start_angle, end_angle)) ver_line(center_x, center_y - r_out + 1, mask, thickness - 1, color, opa); // Top Middle
76
if(deg_test(0, start_angle, end_angle)) ver_line(center_x, center_y + r_in, mask, thickness - 1, color, opa); // Bottom middle
77
78
uint32_t r_out_sqr = r_out * r_out;
79
uint32_t r_in_sqr = r_in * r_in;
80
int16_t xi;
81
int16_t yi;
82
for(yi = -r_out; yi < 0; yi++) {
83
x_start[0] = LV_COORD_MIN;
84
x_start[1] = LV_COORD_MIN;
85
x_start[2] = LV_COORD_MIN;
86
x_start[3] = LV_COORD_MIN;
87
x_end[0] = LV_COORD_MIN;
88
x_end[1] = LV_COORD_MIN;
89
x_end[2] = LV_COORD_MIN;
90
x_end[3] = LV_COORD_MIN;
91
for(xi = -r_out; xi < 0; xi++) {
92
93
uint32_t r_act_sqr = xi * xi + yi * yi;
94
if(r_act_sqr > r_out_sqr) continue;
95
96
deg_base = fast_atan2(xi, yi) - 180;
97
98
deg = 180 + deg_base;
99
if(deg_test(deg, start_angle, end_angle)) {
100
if(x_start[0] == LV_COORD_MIN) x_start[0] = xi;
101
} else if(x_start[0] != LV_COORD_MIN && x_end[0] == LV_COORD_MIN) {
102
x_end[0] = xi - 1;
103
}
104
105
deg = 360 - deg_base;
106
if(deg_test(deg, start_angle, end_angle)) {
107
if(x_start[1] == LV_COORD_MIN) x_start[1] = xi;
108
} else if(x_start[1] != LV_COORD_MIN && x_end[1] == LV_COORD_MIN) {
109
x_end[1] = xi - 1;
110
}
111
112
deg = 180 - deg_base;
113
if(deg_test(deg, start_angle, end_angle)) {
114
if(x_start[2] == LV_COORD_MIN) x_start[2] = xi;
115
} else if(x_start[2] != LV_COORD_MIN && x_end[2] == LV_COORD_MIN) {
116
x_end[2] = xi - 1;
117
}
118
119
deg = deg_base;
120
if(deg_test(deg, start_angle, end_angle)) {
121
if(x_start[3] == LV_COORD_MIN) x_start[3] = xi;
122
} else if(x_start[3] != LV_COORD_MIN && x_end[3] == LV_COORD_MIN) {
123
x_end[3] = xi - 1;
124
}
125
126
if(r_act_sqr < r_in_sqr) break; /*No need to continue the iteration in x once we found the inner edge of the arc*/
127
}
128
129
130
if(x_start[0] != LV_COORD_MIN) {
131
if(x_end[0] == LV_COORD_MIN) x_end[0] = xi - 1;
132
hor_line(center_x + x_start[0], center_y + yi, mask, x_end[0] - x_start[0], color, opa);
133
}
134
135
if(x_start[1] != LV_COORD_MIN) {
136
if(x_end[1] == LV_COORD_MIN) x_end[1] = xi - 1;
137
hor_line(center_x + x_start[1], center_y - yi, mask, x_end[1] - x_start[1], color, opa);
138
}
139
140
if(x_start[2] != LV_COORD_MIN) {
141
if(x_end[2] == LV_COORD_MIN) x_end[2] = xi - 1;
142
hor_line(center_x - x_end[2], center_y + yi, mask, LV_MATH_ABS(x_end[2] - x_start[2]), color, opa);
143
}
144
145
if(x_start[3] != LV_COORD_MIN) {
146
if(x_end[3] == LV_COORD_MIN) x_end[3] = xi - 1;
147
hor_line(center_x - x_end[3], center_y - yi, mask, LV_MATH_ABS(x_end[3] - x_start[3]), color, opa);
148
}
149
150
151
#if LV_ANTIALIAS
152
/*TODO*/
153
154
#endif
155
156
}
157
}
158
159
static uint16_t fast_atan2(int x, int y)
160
{
161
// Fast XY vector to integer degree algorithm - Jan 2011 www.RomanBlack.com
162
// Converts any XY values including 0 to a degree value that should be
163
// within +/- 1 degree of the accurate value without needing
164
// large slow trig functions like ArcTan() or ArcCos().
165
// NOTE! at least one of the X or Y values must be non-zero!
166
// This is the full version, for all 4 quadrants and will generate
167
// the angle in integer degrees from 0-360.
168
// Any values of X and Y are usable including negative values provided
169
// they are between -1456 and 1456 so the 16bit multiply does not overflow.
170
171
unsigned char negflag;
172
unsigned char tempdegree;
173
unsigned char comp;
174
unsigned int degree; // this will hold the result
175
//signed int x; // these hold the XY vector at the start
176
//signed int y; // (and they will be destroyed)
177
unsigned int ux;
178
unsigned int uy;
179
180
// Save the sign flags then remove signs and get XY as unsigned ints
181
negflag = 0;
182
if(x < 0) {
183
negflag += 0x01; // x flag bit
184
x = (0 - x); // is now +
185
}
186
ux = x; // copy to unsigned var before multiply
187
if(y < 0) {
188
negflag += 0x02; // y flag bit
189
y = (0 - y); // is now +
190
}
191
uy = y; // copy to unsigned var before multiply
192
193
// 1. Calc the scaled "degrees"
194
if(ux > uy) {
195
degree = (uy * 45) / ux; // degree result will be 0-45 range
196
negflag += 0x10; // octant flag bit
197
} else {
198
degree = (ux * 45) / uy; // degree result will be 0-45 range
199
}
200
201
// 2. Compensate for the 4 degree error curve
202
comp = 0;
203
tempdegree = degree; // use an unsigned char for speed!
204
if(tempdegree > 22) { // if top half of range
205
if(tempdegree <= 44) comp++;
206
if(tempdegree <= 41) comp++;
207
if(tempdegree <= 37) comp++;
208
if(tempdegree <= 32) comp++; // max is 4 degrees compensated
209
} else { // else is lower half of range
210
if(tempdegree >= 2) comp++;
211
if(tempdegree >= 6) comp++;
212
if(tempdegree >= 10) comp++;
213
if(tempdegree >= 15) comp++; // max is 4 degrees compensated
214
}
215
degree += comp; // degree is now accurate to +/- 1 degree!
216
217
// Invert degree if it was X>Y octant, makes 0-45 into 90-45
218
if(negflag & 0x10) degree = (90 - degree);
219
220
// 3. Degree is now 0-90 range for this quadrant,
221
// need to invert it for whichever quadrant it was in
222
if(negflag & 0x02) { // if -Y
223
if(negflag & 0x01) // if -Y -X
224
degree = (180 + degree);
225
else // else is -Y +X
226
degree = (180 - degree);
227
} else { // else is +Y
228
if(negflag & 0x01) // if +Y -X
229
degree = (360 - degree);
230
}
231
return degree;
232
}
233
234
/**********************
235
* STATIC FUNCTIONS
236
**********************/
237
static void ver_line(lv_coord_t x, lv_coord_t y, const lv_area_t * mask, lv_coord_t len, lv_color_t color, lv_opa_t opa)
238
{
239
lv_area_t area;
240
lv_area_set(&area, x, y, x, y + len);
241
242
fill_fp(&area, mask, color, opa);
243
}
244
245
static void hor_line(lv_coord_t x, lv_coord_t y, const lv_area_t * mask, lv_coord_t len, lv_color_t color, lv_opa_t opa)
246
{
247
lv_area_t area;
248
lv_area_set(&area, x, y, x + len, y);
249
250
fill_fp(&area, mask, color, opa);
251
}
252
253
static bool deg_test_norm(uint16_t deg, uint16_t start, uint16_t end)
254
{
255
if(deg >= start && deg <= end) return true;
256
else return false;
257
}
258
259
static bool deg_test_inv(uint16_t deg, uint16_t start, uint16_t end)
260
{
261
if(deg >= start || deg <= end) {
262
return true;
263
} else return false;
264
}
265
266