Here is an idea. Instead of creating each rectangle with (x, y, width, height) , create them using (x1, y1, x2, y2) or at least interpret these values ββwith respect to width and height.
That way, you can check which rectangles have the same x or y value, and make sure the corresponding rectangle has the same secondary value.
Example:
The indicated rectangles have the following meanings:
- Square 1: [0, 0, 8, 3]
- Square 3: [0, 4, 8, 6]
- Square 4: [9, 0, 10, 4]
First, compare Square 1 with Square 3 (no collision):
- Compare x values
- [0, 8] to [0, 8] This is exactly the same, so there is no crossover.
- Compare y values
- [0, 4] - [3, 6] None of these numbers are alike, so they are not a factor
Then we compare Square 3 with Square 4 (collision):
- Compare x values
- [0, 8] - [9, 10] None of these numbers are alike, so they are not a factor
- Compare y values
- [4, 6] - [0, 4]. Rectangles have a total of 4, but 0! = 6, so there is a collision
We know that we know that a collision will occur, so the method will end, but give an assessment of Square 1 and Square 4 for some additional clarity.
- Compare x values
- [0, 8] - [9, 10] None of these numbers are alike, so they are not a factor
- Compare y values
- [0, 3] - [0, 4]. The rectangles have a total number of 0, but 3! = 4, so there is a collision
Let me know if you need more details :)
Justian meyer
source share