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