The simplest Boolean expression of image intersection?

2014-04-20 2:30 pm
Given two image, A and B.
The top-left corner of A: (x0, y0)
The bottom-right corner of A: (x1, y1)
The top-left corner of B: (x2, y2)
The bottom-right corner of B: (x3, y3)

I am coding a game using MIPS which involves a bomb bombing an object. To check its intersection, what I could thought of is just EIGHT independent cases, that is, a particular corner of an image is inside another image and vice versa. The Boolean expression is so long.
(x1 <= x3 && x3 <= x2 && y1 <= y3 && y3 <= y2) ||
(x1 <= x4 && x4 <= x2 && y1 <= y3 && y3 <= y2) ||
...... (six more)

So, I want to know the simplest Boolean expression (maybe in C program format) which return true iff the two images intersects with each other.

Thanks!

回答 (1)

2014-04-20 3:07 pm
✔ 最佳答案
You need to check four cases only.

The rectangles do not overlap if any of the following is true:
x0 > x3 (A outside and to the right of B)
x1 < x2 (A outside and to the left of B)
y0 > y3 (A outside and below B)
y1 < y2 (A outside and above B)

To get the reverse (overlap), invert them and connect them with AND instead of OR:

bool overlap = (x0 <= x3 && x1 >= x2 && y0 <= y3 && y1 >= y2);


收錄日期: 2021-04-13 21:18:10
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20140420063050AAiSq6h

檢視 Wayback Machine 備份