1 /**
2  * @file lv_draw_arc.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #include "lv_draw_arc.h"
10 #include "lv_draw_rect.h"
11 #include "lv_draw_mask.h"
12 #include "../lv_misc/lv_math.h"
13 
14 /*********************
15  *      DEFINES
16  *********************/
17 #define SPLIT_RADIUS_LIMIT 10  /*With radius greater then this the arc will drawn in quarters. A quarter is drawn only if there is arc in it */
18 #define SPLIT_ANGLE_GAP_LIMIT 60  /*With small gaps in the arc don't bother with splitting because there is nothing to skip.*/
19 
20 /**********************
21  *      TYPEDEFS
22  **********************/
23 typedef struct {
24     lv_coord_t center_x;
25     lv_coord_t center_y;
26     lv_coord_t radius;
27     uint16_t start_angle;
28     uint16_t end_angle;
29     uint16_t start_quarter;
30     uint16_t end_quarter;
31     lv_coord_t width;
32     lv_draw_rect_dsc_t * draw_dsc;
33     const lv_area_t * draw_area;
34     const lv_area_t * clip_area;
35 } quarter_draw_dsc_t;
36 
37 
38 /**********************
39  *  STATIC PROTOTYPES
40  **********************/
41 static void draw_quarter_0(quarter_draw_dsc_t * q);
42 static void draw_quarter_1(quarter_draw_dsc_t * q);
43 static void draw_quarter_2(quarter_draw_dsc_t * q);
44 static void draw_quarter_3(quarter_draw_dsc_t * q);
45 static void get_rounded_area(int16_t angle, lv_coord_t radius, uint8_t tickness, lv_area_t * res_area);
46 
47 
48 /**********************
49  *  STATIC VARIABLES
50  **********************/
51 
52 /**********************
53  *      MACROS
54  **********************/
55 
56 /**********************
57  *   GLOBAL FUNCTIONS
58  **********************/
59 
60 /**
61  * Draw an arc. (Can draw pie too with great thickness.)
62  * @param center_x the x coordinate of the center of the arc
63  * @param center_y the y coordinate of the center of the arc
64  * @param radius the radius of the arc
65  * @param mask the arc will be drawn only in this mask
66  * @param start_angle the start angle of the arc (0 deg on the bottom, 90 deg on the right)
67  * @param end_angle the end angle of the arc
68  * @param clip_area the arc will be drawn only in this area
69  * @param dsc pointer to an initialized `lv_draw_line_dsc_t` variable
70  */
lv_draw_arc(lv_coord_t center_x,lv_coord_t center_y,uint16_t radius,uint16_t start_angle,uint16_t end_angle,const lv_area_t * clip_area,const lv_draw_line_dsc_t * dsc)71 void lv_draw_arc(lv_coord_t center_x, lv_coord_t center_y, uint16_t radius,  uint16_t start_angle, uint16_t end_angle,
72                  const lv_area_t * clip_area, const lv_draw_line_dsc_t * dsc)
73 {
74     if(dsc->opa <= LV_OPA_MIN) return;
75     if(dsc->width == 0) return;
76     if(start_angle == end_angle) return;
77 
78     lv_style_int_t width = dsc->width;
79     if(width > radius) width = radius;
80 
81     lv_draw_rect_dsc_t cir_dsc;
82     lv_draw_rect_dsc_init(&cir_dsc);
83     cir_dsc.radius = LV_RADIUS_CIRCLE;
84     cir_dsc.bg_opa = LV_OPA_TRANSP;
85     cir_dsc.border_opa = dsc->opa;
86     cir_dsc.border_color = dsc->color;
87     cir_dsc.border_width = width;
88     cir_dsc.border_blend_mode = dsc->blend_mode;
89 
90     lv_area_t area;
91     area.x1 = center_x - radius;
92     area.y1 = center_y - radius;
93     area.x2 = center_x + radius - 1;  /*-1 because the center already belongs to the left/bottom part*/
94     area.y2 = center_y + radius - 1;
95 
96     /*Draw a full ring*/
97     if(start_angle + 360 == end_angle || start_angle == end_angle + 360) {
98         lv_draw_rect(&area, clip_area, &cir_dsc);
99         return;
100     }
101 
102     if(start_angle >= 360) start_angle -= 360;
103     if(end_angle >= 360) end_angle -= 360;
104 
105     lv_draw_mask_angle_param_t mask_angle_param;
106     lv_draw_mask_angle_init(&mask_angle_param, center_x, center_y, start_angle, end_angle);
107 
108     int16_t mask_angle_id = lv_draw_mask_add(&mask_angle_param, NULL);
109 
110     int32_t angle_gap;
111     if(end_angle > start_angle) {
112         angle_gap = 360 - (end_angle - start_angle);
113     }
114     else {
115         angle_gap = start_angle - end_angle;
116     }
117     if(angle_gap > SPLIT_ANGLE_GAP_LIMIT && radius > SPLIT_RADIUS_LIMIT) {
118         /*Handle each quarter individually and skip which is empty*/
119         quarter_draw_dsc_t q_dsc;
120         q_dsc.center_x = center_x;
121         q_dsc.center_y = center_y;
122         q_dsc.radius = radius;
123         q_dsc.start_angle = start_angle;
124         q_dsc.end_angle = end_angle;
125         q_dsc.start_quarter = (start_angle / 90) & 0x3;
126         q_dsc.end_quarter = (end_angle / 90) & 0x3;
127         q_dsc.width = width;
128         q_dsc.draw_dsc =  &cir_dsc;
129         q_dsc.draw_area = &area;
130         q_dsc.clip_area = clip_area;
131 
132         draw_quarter_0(&q_dsc);
133         draw_quarter_1(&q_dsc);
134         draw_quarter_2(&q_dsc);
135         draw_quarter_3(&q_dsc);
136     }
137     else {
138         lv_draw_rect(&area, clip_area, &cir_dsc);
139     }
140     lv_draw_mask_remove_id(mask_angle_id);
141 
142     if(dsc->round_start || dsc->round_end) {
143         cir_dsc.bg_color        = dsc->color;
144         cir_dsc.bg_opa        = dsc->opa;
145         cir_dsc.bg_blend_mode = dsc->blend_mode;
146         cir_dsc.border_width = 0;
147 
148         lv_area_t round_area;
149         if(dsc->round_start) {
150             get_rounded_area(start_angle, radius, width, &round_area);
151             round_area.x1 += center_x;
152             round_area.x2 += center_x;
153             round_area.y1 += center_y;
154             round_area.y2 += center_y;
155 
156             lv_draw_rect(&round_area, clip_area, &cir_dsc);
157         }
158 
159         if(dsc->round_end) {
160             get_rounded_area(end_angle, radius, width, &round_area);
161             round_area.x1 += center_x;
162             round_area.x2 += center_x;
163             round_area.y1 += center_y;
164             round_area.y2 += center_y;
165 
166             lv_draw_rect(&round_area, clip_area, &cir_dsc);
167         }
168     }
169 }
170 
171 /**********************
172  *   STATIC FUNCTIONS
173  **********************/
174 
draw_quarter_0(quarter_draw_dsc_t * q)175 static void draw_quarter_0(quarter_draw_dsc_t * q)
176 {
177     lv_area_t quarter_area;
178 
179     if(q->start_quarter == 0 && q->end_quarter == 0 && q->start_angle < q->end_angle) {
180         /*Small arc here*/
181         quarter_area.y1 = q->center_y + ((_lv_trigo_sin(q->start_angle) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
182         quarter_area.x2 = q->center_x + ((_lv_trigo_sin(q->start_angle + 90) * (q->radius)) >> LV_TRIGO_SHIFT);
183 
184         quarter_area.y2 = q->center_y + ((_lv_trigo_sin(q->end_angle) * q->radius) >> LV_TRIGO_SHIFT);
185         quarter_area.x1 = q->center_x + ((_lv_trigo_sin(q->end_angle + 90) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
186 
187         bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
188         if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
189     }
190     else if(q->start_quarter == 0 || q->end_quarter == 0) {
191         /*Start and/or end arcs here*/
192         if(q->start_quarter == 0) {
193             quarter_area.x1 = q->center_x;
194             quarter_area.y2 = q->center_y + q->radius;
195 
196             quarter_area.y1 = q->center_y + ((_lv_trigo_sin(q->start_angle) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
197             quarter_area.x2 = q->center_x + ((_lv_trigo_sin(q->start_angle + 90) * (q->radius)) >> LV_TRIGO_SHIFT);
198 
199             bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
200             if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
201         }
202         if(q->end_quarter == 0) {
203             quarter_area.x2 = q->center_x + q->radius;
204             quarter_area.y1 = q->center_y;
205 
206             quarter_area.y2 = q->center_y + ((_lv_trigo_sin(q->end_angle) * q->radius) >> LV_TRIGO_SHIFT);
207             quarter_area.x1 = q->center_x + ((_lv_trigo_sin(q->end_angle + 90) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
208 
209             bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
210             if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
211         }
212     }
213     else if((q->start_quarter == q->end_quarter && q->start_quarter != 0 && q->end_angle < q->start_angle) ||
214             (q->start_quarter == 2 && q->end_quarter == 1) ||
215             (q->start_quarter == 3 && q->end_quarter == 2) ||
216             (q->start_quarter == 3 && q->end_quarter == 1)) {
217         /*Arc crosses here*/
218         quarter_area.x1 = q->center_x;
219         quarter_area.y1 = q->center_y;
220         quarter_area.x2 = q->center_x + q->radius;
221         quarter_area.y2 = q->center_y + q->radius;
222 
223         bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
224         if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
225     }
226 }
227 
draw_quarter_1(quarter_draw_dsc_t * q)228 static void draw_quarter_1(quarter_draw_dsc_t * q)
229 {
230     lv_area_t quarter_area;
231 
232     if(q->start_quarter == 1 && q->end_quarter == 1 && q->start_angle < q->end_angle) {
233         /*Small arc here*/
234         quarter_area.y2 = q->center_y + ((_lv_trigo_sin(q->start_angle) * (q->radius)) >> LV_TRIGO_SHIFT);
235         quarter_area.x2 = q->center_x + ((_lv_trigo_sin(q->start_angle + 90) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
236 
237         quarter_area.y1 = q->center_y + ((_lv_trigo_sin(q->end_angle) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
238         quarter_area.x1 = q->center_x + ((_lv_trigo_sin(q->end_angle + 90) * (q->radius)) >> LV_TRIGO_SHIFT);
239 
240         bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
241         if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
242     }
243     else if(q->start_quarter == 1 || q->end_quarter == 1) {
244         /*Start and/or end arcs here*/
245         if(q->start_quarter == 1) {
246             quarter_area.x1 = q->center_x - q->radius;
247             quarter_area.y1 = q->center_y;
248 
249             quarter_area.y2 = q->center_y + ((_lv_trigo_sin(q->start_angle) * (q->radius)) >> LV_TRIGO_SHIFT);
250             quarter_area.x2 = q->center_x + ((_lv_trigo_sin(q->start_angle + 90) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
251 
252             bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
253             if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
254         }
255         if(q->end_quarter == 1) {
256             quarter_area.x2 = q->center_x - 1;
257             quarter_area.y2 = q->center_y + q->radius;
258 
259             quarter_area.y1 = q->center_y + ((_lv_trigo_sin(q->end_angle) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
260             quarter_area.x1 = q->center_x + ((_lv_trigo_sin(q->end_angle + 90) * (q->radius)) >> LV_TRIGO_SHIFT);
261 
262             bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
263             if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
264         }
265     }
266     else if((q->start_quarter == q->end_quarter && q->start_quarter != 1 && q->end_angle < q->start_angle) ||
267             (q->start_quarter == 0 && q->end_quarter == 2) ||
268             (q->start_quarter == 0 && q->end_quarter == 3) ||
269             (q->start_quarter == 3 && q->end_quarter == 2)) {
270         /*Arc crosses here*/
271         quarter_area.x1 = q->center_x - q->radius;
272         quarter_area.y1 = q->center_y;
273         quarter_area.x2 = q->center_x - 1;
274         quarter_area.y2 = q->center_y + q->radius;
275 
276         bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
277         if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
278     }
279 }
280 
draw_quarter_2(quarter_draw_dsc_t * q)281 static void draw_quarter_2(quarter_draw_dsc_t * q)
282 {
283     lv_area_t quarter_area;
284 
285     if(q->start_quarter == 2 && q->end_quarter == 2 && q->start_angle < q->end_angle) {
286         /*Small arc here*/
287         quarter_area.x1 = q->center_x + ((_lv_trigo_sin(q->start_angle + 90) * (q->radius)) >> LV_TRIGO_SHIFT);
288         quarter_area.y2 = q->center_y + ((_lv_trigo_sin(q->start_angle) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
289 
290         quarter_area.y1 = q->center_y + ((_lv_trigo_sin(q->end_angle) * q->radius) >> LV_TRIGO_SHIFT);
291         quarter_area.x2 = q->center_x + ((_lv_trigo_sin(q->end_angle + 90) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
292 
293         bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
294         if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
295     }
296     else if(q->start_quarter == 2 || q->end_quarter == 2) {
297         /*Start and/or end arcs here*/
298         if(q->start_quarter == 2) {
299             quarter_area.x2 = q->center_x - 1;
300             quarter_area.y1 = q->center_y - q->radius;
301 
302             quarter_area.x1 = q->center_x + ((_lv_trigo_sin(q->start_angle + 90) * (q->radius)) >> LV_TRIGO_SHIFT);
303             quarter_area.y2 = q->center_y + ((_lv_trigo_sin(q->start_angle) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
304 
305             bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
306             if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
307         }
308         if(q->end_quarter == 2) {
309             quarter_area.x1 = q->center_x - q->radius;
310             quarter_area.y2 = q->center_y - 1;
311 
312             quarter_area.x2 = q->center_x + ((_lv_trigo_sin(q->end_angle + 90) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
313             quarter_area.y1 = q->center_y + ((_lv_trigo_sin(q->end_angle) * (q->radius)) >> LV_TRIGO_SHIFT);
314 
315             bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
316             if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
317         }
318     }
319     else if((q->start_quarter == q->end_quarter && q->start_quarter != 2 && q->end_angle < q->start_angle) ||
320             (q->start_quarter == 0 && q->end_quarter == 3) ||
321             (q->start_quarter == 1 && q->end_quarter == 3) ||
322             (q->start_quarter == 1 && q->end_quarter == 0)) {
323         /*Arc crosses here*/
324         quarter_area.x1 = q->center_x - q->radius;
325         quarter_area.y1 = q->center_y - q->radius;
326         quarter_area.x2 = q->center_x - 1;
327         quarter_area.y2 = q->center_y - 1;
328 
329         bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
330         if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
331     }
332 }
333 
334 
draw_quarter_3(quarter_draw_dsc_t * q)335 static void draw_quarter_3(quarter_draw_dsc_t * q)
336 {
337     lv_area_t quarter_area;
338 
339     if(q->start_quarter == 3 && q->end_quarter == 3 && q->start_angle < q->end_angle) {
340         /*Small arc here*/
341         quarter_area.x1 = q->center_x + ((_lv_trigo_sin(q->start_angle + 90) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
342         quarter_area.y1 = q->center_y + ((_lv_trigo_sin(q->start_angle) * (q->radius)) >> LV_TRIGO_SHIFT);
343 
344         quarter_area.x2 = q->center_x + ((_lv_trigo_sin(q->end_angle + 90) * (q->radius)) >> LV_TRIGO_SHIFT);
345         quarter_area.y2 = q->center_y + ((_lv_trigo_sin(q->end_angle) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
346 
347         bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
348         if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
349     }
350     else if(q->start_quarter == 3 || q->end_quarter == 3) {
351         /*Start and/or end arcs here*/
352         if(q->start_quarter == 3) {
353             quarter_area.x2 = q->center_x + q->radius;
354             quarter_area.y2 = q->center_y - 1;
355 
356             quarter_area.x1 = q->center_x + ((_lv_trigo_sin(q->start_angle + 90) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
357             quarter_area.y1 = q->center_y + ((_lv_trigo_sin(q->start_angle) * (q->radius)) >> LV_TRIGO_SHIFT);
358 
359             bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
360             if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
361         }
362         if(q->end_quarter == 3) {
363             quarter_area.x1 = q->center_x;
364             quarter_area.y1 = q->center_y - q->radius;
365 
366             quarter_area.x2 = q->center_x + ((_lv_trigo_sin(q->end_angle + 90) * (q->radius)) >> LV_TRIGO_SHIFT);
367             quarter_area.y2 = q->center_y + ((_lv_trigo_sin(q->end_angle) * (q->radius - q->width)) >> LV_TRIGO_SHIFT);
368 
369             bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
370             if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
371         }
372     }
373     else if((q->start_quarter == q->end_quarter && q->start_quarter != 3 && q->end_angle < q->start_angle) ||
374             (q->start_quarter == 2 && q->end_quarter == 0) ||
375             (q->start_quarter == 1 && q->end_quarter == 0) ||
376             (q->start_quarter == 2 && q->end_quarter == 1)) {
377         /*Arc crosses here*/
378         quarter_area.x1 = q->center_x;
379         quarter_area.y1 = q->center_y - q->radius;
380         quarter_area.x2 = q->center_x + q->radius;
381         quarter_area.y2 = q->center_y - 1;
382 
383         bool ok = _lv_area_intersect(&quarter_area, &quarter_area, q->clip_area);
384         if(ok) lv_draw_rect(q->draw_area, &quarter_area, q->draw_dsc);
385     }
386 }
387 
388 
get_rounded_area(int16_t angle,lv_coord_t radius,uint8_t tickness,lv_area_t * res_area)389 static void get_rounded_area(int16_t angle, lv_coord_t radius, uint8_t tickness, lv_area_t * res_area)
390 {
391     const uint8_t ps = 8;
392     const uint8_t pa = 127;
393 
394     int32_t thick_half = tickness / 2;
395     uint8_t thick_corr = (tickness & 0x01) ? 0 : 1;
396 
397     int32_t rx_corr;
398     int32_t ry_corr;
399 
400     if(angle > 90 && angle < 270) rx_corr = 0;
401     else  rx_corr = 0;
402 
403     if(angle > 0 && angle < 180) ry_corr = 0;
404     else  ry_corr = 0;
405 
406     int32_t cir_x;
407     int32_t cir_y;
408 
409     cir_x = ((radius - rx_corr - thick_half) * _lv_trigo_sin(90 - angle)) >> (LV_TRIGO_SHIFT - ps);
410     cir_y = ((radius - ry_corr - thick_half) * _lv_trigo_sin(angle)) >> (LV_TRIGO_SHIFT - ps);
411 
412     /* Actually the center of the pixel need to be calculated so apply 1/2 px offset*/
413     if(cir_x > 0) {
414         cir_x = (cir_x - pa) >> ps;
415         res_area->x1 = cir_x - thick_half + thick_corr;
416         res_area->x2 = cir_x + thick_half;
417     }
418     else {
419         cir_x = (cir_x + pa) >> ps;
420         res_area->x1 = cir_x - thick_half;
421         res_area->x2 = cir_x + thick_half - thick_corr;
422     }
423 
424     if(cir_y > 0) {
425         cir_y = (cir_y - pa) >> ps;
426         res_area->y1 = cir_y - thick_half + thick_corr;
427         res_area->y2 = cir_y + thick_half;
428     }
429     else {
430         cir_y = (cir_y + pa) >> ps;
431         res_area->y1 = cir_y - thick_half;
432         res_area->y2 = cir_y + thick_half - thick_corr;
433     }
434 }
435 
436