1 /**
2  * @file lv_area.c
3  *
4  */
5 
6 /*********************
7  *      INCLUDES
8  *********************/
9 #ifdef LV_CONF_INCLUDE_SIMPLE
10 #include "lv_conf.h"
11 #else
12 #include "../../lv_conf.h"
13 #endif
14 
15 #include "lv_area.h"
16 #include "lv_math.h"
17 
18 /*********************
19  *      DEFINES
20  *********************/
21 
22 /**********************
23  *      TYPEDEFS
24  **********************/
25 
26 /**********************
27  *  STATIC PROTOTYPES
28  **********************/
29 
30 /**********************
31  *  STATIC VARIABLES
32  **********************/
33 
34 /**********************
35  *      MACROS
36  **********************/
37 
38 /**********************
39  *   GLOBAL FUNCTIONS
40  **********************/
41 
42 /**
43  * Initialize an area
44  * @param area_p pointer to an area
45  * @param x1 left coordinate of the area
46  * @param y1 top coordinate of the area
47  * @param x2 right coordinate of the area
48  * @param y2 bottom coordinate of the area
49  */
lv_area_set(lv_area_t * area_p,lv_coord_t x1,lv_coord_t y1,lv_coord_t x2,lv_coord_t y2)50 void lv_area_set(lv_area_t * area_p, lv_coord_t x1, lv_coord_t y1, lv_coord_t x2, lv_coord_t y2)
51 {
52     area_p->x1 = x1;
53     area_p->y1 = y1;
54     area_p->x2 = x2;
55     area_p->y2 = y2;
56 }
57 
58 /**
59  * Set the width of an area
60  * @param area_p pointer to an area
61  * @param w the new width of the area (w == 1 makes x1 == x2)
62  */
lv_area_set_width(lv_area_t * area_p,lv_coord_t w)63 void lv_area_set_width(lv_area_t * area_p, lv_coord_t w)
64 {
65     area_p->x2 = area_p->x1 + w - 1;
66 }
67 
68 /**
69  * Set the height of an area
70  * @param area_p pointer to an area
71  * @param h the new height of the area (h == 1 makes y1 == y2)
72  */
lv_area_set_height(lv_area_t * area_p,lv_coord_t h)73 void lv_area_set_height(lv_area_t * area_p, lv_coord_t h)
74 {
75     area_p->y2 = area_p->y1 + h - 1;
76 }
77 
78 /**
79  * Set the position of an area (width and height will be kept)
80  * @param area_p pointer to an area
81  * @param x the new x coordinate of the area
82  * @param y the new y coordinate of the area
83  */
lv_area_set_pos(lv_area_t * area_p,lv_coord_t x,lv_coord_t y)84 void lv_area_set_pos(lv_area_t * area_p, lv_coord_t x, lv_coord_t y)
85 {
86     lv_coord_t w = lv_area_get_width(area_p);
87     lv_coord_t h = lv_area_get_height(area_p);
88     area_p->x1   = x;
89     area_p->y1   = y;
90     lv_area_set_width(area_p, w);
91     lv_area_set_height(area_p, h);
92 }
93 
94 /**
95  * Return with area of an area (x * y)
96  * @param area_p pointer to an area
97  * @return size of area
98  */
lv_area_get_size(const lv_area_t * area_p)99 uint32_t lv_area_get_size(const lv_area_t * area_p)
100 {
101     uint32_t size;
102 
103     size = (uint32_t)(area_p->x2 - area_p->x1 + 1) * (area_p->y2 - area_p->y1 + 1);
104 
105     return size;
106 }
107 
108 /**
109  * Get the common parts of two areas
110  * @param res_p pointer to an area, the result will be stored here
111  * @param a1_p pointer to the first area
112  * @param a2_p pointer to the second area
113  * @return false: the two area has NO common parts, res_p is invalid
114  */
lv_area_intersect(lv_area_t * res_p,const lv_area_t * a1_p,const lv_area_t * a2_p)115 bool lv_area_intersect(lv_area_t * res_p, const lv_area_t * a1_p, const lv_area_t * a2_p)
116 {
117     /* Get the smaller area from 'a1_p' and 'a2_p' */
118     res_p->x1 = LV_MATH_MAX(a1_p->x1, a2_p->x1);
119     res_p->y1 = LV_MATH_MAX(a1_p->y1, a2_p->y1);
120     res_p->x2 = LV_MATH_MIN(a1_p->x2, a2_p->x2);
121     res_p->y2 = LV_MATH_MIN(a1_p->y2, a2_p->y2);
122 
123     /*If x1 or y1 greater then x2 or y2 then the areas union is empty*/
124     bool union_ok = true;
125     if((res_p->x1 > res_p->x2) || (res_p->y1 > res_p->y2)) {
126         union_ok = false;
127     }
128 
129     return union_ok;
130 }
131 /**
132  * Join two areas into a third which involves the other two
133  * @param res_p pointer to an area, the result will be stored here
134  * @param a1_p pointer to the first area
135  * @param a2_p pointer to the second area
136  */
lv_area_join(lv_area_t * a_res_p,const lv_area_t * a1_p,const lv_area_t * a2_p)137 void lv_area_join(lv_area_t * a_res_p, const lv_area_t * a1_p, const lv_area_t * a2_p)
138 {
139     a_res_p->x1 = LV_MATH_MIN(a1_p->x1, a2_p->x1);
140     a_res_p->y1 = LV_MATH_MIN(a1_p->y1, a2_p->y1);
141     a_res_p->x2 = LV_MATH_MAX(a1_p->x2, a2_p->x2);
142     a_res_p->y2 = LV_MATH_MAX(a1_p->y2, a2_p->y2);
143 }
144 
145 /**
146  * Check if a point is on an area
147  * @param a_p pointer to an area
148  * @param p_p pointer to a point
149  * @return false:the point is out of the area
150  */
lv_area_is_point_on(const lv_area_t * a_p,const lv_point_t * p_p)151 bool lv_area_is_point_on(const lv_area_t * a_p, const lv_point_t * p_p)
152 {
153     bool is_on = false;
154 
155     if((p_p->x >= a_p->x1 && p_p->x <= a_p->x2) && ((p_p->y >= a_p->y1 && p_p->y <= a_p->y2))) {
156         is_on = true;
157     }
158 
159     return is_on;
160 }
161 
162 /**
163  * Check if two area has common parts
164  * @param a1_p pointer to an area.
165  * @param a2_p pointer to an other area
166  * @return false: a1_p and a2_p has no common parts
167  */
lv_area_is_on(const lv_area_t * a1_p,const lv_area_t * a2_p)168 bool lv_area_is_on(const lv_area_t * a1_p, const lv_area_t * a2_p)
169 {
170     if((a1_p->x1 <= a2_p->x2) && (a1_p->x2 >= a2_p->x1) && (a1_p->y1 <= a2_p->y2) && (a1_p->y2 >= a2_p->y1)) {
171         return true;
172     } else {
173         return false;
174     }
175 }
176 
177 /**
178  * Check if an area is fully on an other
179  * @param ain_p pointer to an area which could be in 'aholder_p'
180  * @param aholder pointer to an area which could involve 'ain_p'
181  * @return
182  */
lv_area_is_in(const lv_area_t * ain_p,const lv_area_t * aholder_p)183 bool lv_area_is_in(const lv_area_t * ain_p, const lv_area_t * aholder_p)
184 {
185     bool is_in = false;
186 
187     if(ain_p->x1 >= aholder_p->x1 && ain_p->y1 >= aholder_p->y1 && ain_p->x2 <= aholder_p->x2 &&
188        ain_p->y2 <= aholder_p->y2) {
189         is_in = true;
190     }
191 
192     return is_in;
193 }
194 
195 /**********************
196  *   STATIC FUNCTIONS
197  **********************/
198