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