/**1* @file lv_area.c2*3*/45/*********************6* INCLUDES7*********************/8#include "lv_area.h"9#include "lv_math.h"1011/*********************12* DEFINES13*********************/1415/**********************16* TYPEDEFS17**********************/1819/**********************20* STATIC PROTOTYPES21**********************/2223/**********************24* STATIC VARIABLES25**********************/2627/**********************28* MACROS29**********************/3031/**********************32* GLOBAL FUNCTIONS33**********************/3435/**36* Initialize an area37* @param area_p pointer to an area38* @param x1 left coordinate of the area39* @param y1 top coordinate of the area40* @param x2 right coordinate of the area41* @param y2 bottom coordinate of the area42*/43void lv_area_set(lv_area_t * area_p, lv_coord_t x1, lv_coord_t y1, lv_coord_t x2, lv_coord_t y2)44{45area_p->x1 = x1;46area_p->y1 = y1;47area_p->x2 = x2;48area_p->y2 = y2;49}5051/**52* Set the width of an area53* @param area_p pointer to an area54* @param w the new width of the area (w == 1 makes x1 == x2)55*/56void lv_area_set_width(lv_area_t * area_p, lv_coord_t w)57{58area_p->x2 = area_p->x1 + w - 1;59}6061/**62* Set the height of an area63* @param area_p pointer to an area64* @param h the new height of the area (h == 1 makes y1 == y2)65*/66void lv_area_set_height(lv_area_t * area_p, lv_coord_t h)67{68area_p->y2 = area_p->y1 + h - 1;69}7071/**72* Set the position of an area (width and height will be kept)73* @param area_p pointer to an area74* @param x the new x coordinate of the area75* @param y the new y coordinate of the area76*/77void lv_area_set_pos(lv_area_t * area_p, lv_coord_t x, lv_coord_t y)78{79lv_coord_t w = lv_area_get_width(area_p);80lv_coord_t h = lv_area_get_height(area_p);81area_p->x1 = x;82area_p->y1 = y;83lv_area_set_width(area_p, w);84lv_area_set_height(area_p, h);85}8687/**88* Return with area of an area (x * y)89* @param area_p pointer to an area90* @return size of area91*/92uint32_t lv_area_get_size(const lv_area_t * area_p)93{94uint32_t size;9596size = (uint32_t)(area_p->x2 - area_p->x1 + 1) *97(area_p->y2 - area_p->y1 + 1);9899return size;100}101102/**103* Get the common parts of two areas104* @param res_p pointer to an area, the result will be stored here105* @param a1_p pointer to the first area106* @param a2_p pointer to the second area107* @return false: the two area has NO common parts, res_p is invalid108*/109bool lv_area_intersect(lv_area_t * res_p, const lv_area_t * a1_p, const lv_area_t * a2_p)110{111/* Get the smaller area from 'a1_p' and 'a2_p' */112res_p->x1 = LV_MATH_MAX(a1_p->x1, a2_p->x1);113res_p->y1 = LV_MATH_MAX(a1_p->y1, a2_p->y1);114res_p->x2 = LV_MATH_MIN(a1_p->x2, a2_p->x2);115res_p->y2 = LV_MATH_MIN(a1_p->y2, a2_p->y2);116117/*If x1 or y1 greater then x2 or y2 then the areas union is empty*/118bool union_ok = true;119if((res_p->x1 > res_p->x2) ||120(res_p->y1 > res_p->y2)) {121union_ok = false;122}123124return union_ok;125}126/**127* Join two areas into a third which involves the other two128* @param res_p pointer to an area, the result will be stored here129* @param a1_p pointer to the first area130* @param a2_p pointer to the second area131*/132void lv_area_join(lv_area_t * a_res_p, const lv_area_t * a1_p, const lv_area_t * a2_p)133{134a_res_p->x1 = LV_MATH_MIN(a1_p->x1, a2_p->x1);135a_res_p->y1 = LV_MATH_MIN(a1_p->y1, a2_p->y1);136a_res_p->x2 = LV_MATH_MAX(a1_p->x2, a2_p->x2);137a_res_p->y2 = LV_MATH_MAX(a1_p->y2, a2_p->y2);138}139140/**141* Check if a point is on an area142* @param a_p pointer to an area143* @param p_p pointer to a point144* @return false:the point is out of the area145*/146bool lv_area_is_point_on(const lv_area_t * a_p, const lv_point_t * p_p)147{148bool is_on = false;149150if((p_p->x >= a_p->x1 && p_p->x <= a_p->x2) &&151((p_p->y >= a_p->y1 && p_p->y <= a_p->y2))) {152is_on = true;153}154155return is_on;156}157158/**159* Check if two area has common parts160* @param a1_p pointer to an area.161* @param a2_p pointer to an other area162* @return false: a1_p and a2_p has no common parts163*/164bool lv_area_is_on(const lv_area_t * a1_p, const lv_area_t * a2_p)165{166if((a1_p->x1 <= a2_p->x2) &&167(a1_p->x2 >= a2_p->x1) &&168(a1_p->y1 <= a2_p->y2) &&169(a1_p->y2 >= a2_p->y1)) {170return true;171} else {172return false;173}174175}176177/**178* Check if an area is fully on an other179* @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* @return182*/183bool lv_area_is_in(const lv_area_t * ain_p, const lv_area_t * aholder_p)184{185bool is_in = false;186187if(ain_p->x1 >= aholder_p->x1 &&188ain_p->y1 >= aholder_p->y1 &&189ain_p->x2 <= aholder_p->x2 &&190ain_p->y2 <= aholder_p->y2) {191is_in = true;192}193194return is_in;195}196197/**********************198* STATIC FUNCTIONS199**********************/200201202