Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lv_area_is_on function bug #96

Closed
chenshijianworkgit opened this issue Jan 31, 2018 · 12 comments
Closed

lv_area_is_on function bug #96

chenshijianworkgit opened this issue Jan 31, 2018 · 12 comments
Labels

Comments

@chenshijianworkgit
Copy link
Contributor

Hi kisvegabor,

I found the lv_area_is_on function implementation seems a bit of a problem, the comment right-top but the implementation is p.x = a2_p-> x1 should be p.x = a2_p-> x2;

And There are problems with the last two judgments, Because they are always fake:

((a2_p->y1 <= a1_p->y1 && a2_p->y1 >= a1_p->y2) ||
(a2_p->y2 <= a1_p->y1 && a2_p->y2 >= a1_p->y2) ||
(a2_p->y1 <= a1_p->y1 && a2_p->y2 >= a1_p->y2))

((a2_p->x1 <= a1_p->x1 && a2_p->x1 >= a1_p->x2) ||
(a2_p->x2 <= a1_p->x1 && a2_p->x2 >= a1_p->x2) ||
(a2_p->x1 <= a1_p->x1 && a2_p->x2 >= a1_p->x2))

@kisvegabor
Copy link
Member

kisvegabor commented Jan 31, 2018

You are right. Thank you. I don't how could this remain hidden so long because it is an essential function.
May I ask how did you find this bug?

I think I need to re-think this function. Now it is too complicated. Have a good implemntation idea?
The goal is to give true if the 2 areas overlap each other.

@upbeat27
Copy link
Contributor

upbeat27 commented Jan 31, 2018

It looks like this is the simplest solution for checking if two rectangles overlap:

bool lv_area_is_on(const lv_area_t * a1_p, const lv_area_t * a2_p)
{
return (
(a1_p->x1 < a2_p->x2) && 
(a1_p->x2 > a2_p->x1) && 
(a1_p->y1 > a2_p->y2) && 
(a1_p->y2 < a2_p->y1));
}

Found here: https://stackoverflow.com/questions/306316/determine-if-two-rectangles-overlap-each-other

@chenshijianworkgit
Copy link
Contributor Author

Hi kisvegabor,

I inadvertently found in reading the code;

I'm searching online for how to judge the intersection of two rectangular areas with mathematical models and finding a better blog:

The core principle of judgment is that if the two rectangular areas intersect, then the distance between their centers should be less than half the width of the sum, and their longitudinal distance should be less than half the height of the sum:

Assumptions : A[x01,y01,x02,y02] B[x11,y11,x12,y12].

The distance between rectangle A and rectangle B Center point in the X direction is Lx: abs( (x01+x02)/2 – (x11+x12)/2)
The distance between rectangle A and rectangle B Center point in the Y direction is Ly: abs( (y01+y02)/2 – (y11+y12)/2)

The sides of the rectangle A and the rectangle B in the X direction are Sax: abs (x01-x02) Sbx: abs (x11-x12)
The sides of the rectangle A and the rectangle B in the Y direction are Say: abs (y01-y02) Sby: abs (y11-y12)

If AB intersects, the following relationship is satisfied:
Lx <= (Sax + Sbx)/2 && Ly <=(Say+ Sby)/2

`static bool is_rect_intersect(int x01, int x02, int y01, int y02,
int x11, int x12, int y11, int y12)
{
/1.Calculate the horizontal and vertical distances between the center points of two rectangular areas。
* (Since the two equations are divided by 2, there is no addition here)
/
int zx = abs((x01+x02)-(x11+x12));
int zy = abs((y01+y02)-(y11+y12));

/*2.Calculate the width and height of two rectangles
 *(Since the two equations are divided by 2, there is no addition here)*/
int x  = abs(x01-x02)+abs(x11-x12);
int y  = abs(y01-y02)+abs(y11-y12);

//3.Determine whether the two equations intersect
if(zx <= x && zy <= y)
	return true;
else
	return false;

}`

@chenshijianworkgit
Copy link
Contributor Author

image

If (zx == x && zy == y), then only one point intersects,Like Figure 1

@chenshijianworkgit
Copy link
Contributor Author

Reference program:

`#define ABS(x) ((x)>0?(x):-(x))

bool lv_area_is_on(const lv_area_t * a1_p, const lv_area_t * a2_p)
{
/1.Calculate the horizontal and vertical distances between the center points of two rectangular areas。
* (Since the two equations are divided by 2, there is no addition here)
/
int zx = ABS((a1_p->x1+a1_p->x2)-(a2_p->x1+a2_p->x2));
int zy = ABS((a1_p->y1+a1_p->y2)-(a2_p->y1+a2_p->y2));

/*2.Calculate the width and height of two rectangles
 *(Since the two equations are divided by 2, there is no addition here)*/
int x  = (a1_p->x2-a1_p->x1)+(a2_p->x2-a2_p->x1);
int y  = (a1_p->y2-a1_p->y1)+(a2_p->y2-a2_p->y1);

//3.Determine whether the two equations intersect
if(zx <= x && zy <= y)
	return true;
else
	return false;   /*no cover*/

}`

@kisvegabor
Copy link
Member

Thank you for the very deep work and explanation.
As this solution is much clearer then the current I will add this to the code!

@kisvegabor
Copy link
Member

@upbeat27
Thank you for the answer, but it doesn't give true if a1 is large and fully includes a2 (like Case 2 on the above image)

@upbeat27
Copy link
Contributor

upbeat27 commented Feb 1, 2018

@kisvegabor I forgot to adjust the y axis for increasing top to bottom in the above example.
This fixes that and also handles when only the edges overlap.
You can confirm it works for all cases - it is also more simple.

bool lv_area_is_on(const lv_area_t * a1_p, const lv_area_t * a2_p)
{
return (
    (a1_p->x1 <= a2_p->x2) && 
    (a1_p->x2 >= a2_p->x1) && 
    (a1_p->y1 <= a2_p->y2) && 
    (a1_p->y2 >= a2_p->y1));
}

@kisvegabor
Copy link
Member

@upbeat27
Looking again your suggestion it also seems correct. @chenshijianworkgit do you see any problem with that?

@chenshijianworkgit
Copy link
Contributor Author

Can not see the method upbeat27 mention any problems, and more simple

@kisvegabor
Copy link
Member

Then I will add @upbeat27 's solution.
Thank you for both of you!

@kisvegabor
Copy link
Member

Fixed in dev-5.1.

kisvegabor pushed a commit that referenced this issue Aug 19, 2021
* lib/fs_driver.py: add file system interface.

* examples/Dynamic_loading_font_example.py: add font loading demo.

* examples/font/*: add test font(Alibaba-PuHuiTi)

* Adjust font example to run on the unix port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants