Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Algorithm to check if a rectangle is overlapping with another rectangle
- Invisible_Factory
-
500+ posts
Algorithm to check if a rectangle is overlapping with another rectangle
I have a 2D grid where each square can be represented by (x,y) coordinates and I can place 1x1 blocks in the grid, and I am storing the coordinates of each block in a list. I am currently also trying to code it so that I can place blocks of any size, such as 1x2, 3x1, 6x5, etc. which I am going to call rectangles. Each rectangle is represented by (x,y,xEnd,yEnd) where x and y are coordinates of the bottomleft 1x1 block of the rectangle, and xEnd,yEnd are for the topright block. (I have also removed the functionality for 1x1 blocks and represent 1x1 blocks as 1x1 rectangles, which are effectively the same thing).
However, while placing a rectangle, I dont want to cover any grid squares occupied by another rectangle - aka, i dont want overlap. In particular, i will split this problem into 4 cases.
1. Checking if a 1x1 is overlapping a 1x1; this is easy, just see if the x and y coordinates are equal.
2. Checking if a 1x1 is overlapping a bigger rectangle. The solution is to see if the coordinates of the 1x1 is within the bounds of the rectangle; example:
If the 1x1 is (4,3,4,3) and the bigger rectangle is (2,1,6,5), then there is overlap because x = 4 lies within the bounds of 2 and 6. which is saying 2≤4≤6. And for y 1≤3≤5.
3. Checking if a bigger rectangle overlaps a 1x1, is a similar solution to the previous.
Now my actual question;
4. What if a rectangle overlaps a rectangle, if neither is a 1x1?
Btw, i dont want to find every 1x1 square in a rectangle then check if each square is inside a rectangle (reduction to case 2). It is technically a solution but a bad solution, I want to see if it can be done using the raw numbers given as arguments.
Also i returned to scratch for a bit after 5 years so sorry if im not following posting guidelines or scratchblock rules.
However, while placing a rectangle, I dont want to cover any grid squares occupied by another rectangle - aka, i dont want overlap. In particular, i will split this problem into 4 cases.
1. Checking if a 1x1 is overlapping a 1x1; this is easy, just see if the x and y coordinates are equal.
2. Checking if a 1x1 is overlapping a bigger rectangle. The solution is to see if the coordinates of the 1x1 is within the bounds of the rectangle; example:
If the 1x1 is (4,3,4,3) and the bigger rectangle is (2,1,6,5), then there is overlap because x = 4 lies within the bounds of 2 and 6. which is saying 2≤4≤6. And for y 1≤3≤5.
3. Checking if a bigger rectangle overlaps a 1x1, is a similar solution to the previous.
Now my actual question;
4. What if a rectangle overlaps a rectangle, if neither is a 1x1?
Btw, i dont want to find every 1x1 square in a rectangle then check if each square is inside a rectangle (reduction to case 2). It is technically a solution but a bad solution, I want to see if it can be done using the raw numbers given as arguments.
Also i returned to scratch for a bit after 5 years so sorry if im not following posting guidelines or scratchblock rules.
- kingjaw2
-
91 posts
Algorithm to check if a rectangle is overlapping with another rectangle
Since you have the top right and bottom left points of each rectangle, you can check each corner individually on one of the rectangles. For example, for the bottom left corner you could check if x1 is less than or equal to xEnd2 and y1 is less than or equal to yEnd2. You could repeat this for each rectangle and connect them in a big “or” statement.
- Invisible_Factory
-
500+ posts
Algorithm to check if a rectangle is overlapping with another rectangle
x1 ≤ xEnd2 isnt really giving any information about overlaps because if there is no overlap then either rectangle could be to the left or the right. Since you have the top right and bottom left points of each rectangle, you can check each corner individually on one of the rectangles. For example, for the bottom left corner you could check if x1 is less than or equal to xEnd2 and y1 is less than or equal to yEnd2. You could repeat this for each rectangle and connect them in a big “or” statement.
However, you might be right that checking the corners can be faster than checking the entire rectangle, if i can find the right comparisons
- Invisible_Factory
-
500+ posts
Algorithm to check if a rectangle is overlapping with another rectangle
Btw, I have solved the problem.
- Discussion Forums
- » Help with Scripts
-
» Algorithm to check if a rectangle is overlapping with another rectangle