@BaronAWC This isn’t really important but I thought it was a cool discovery
Something I think is interesting that I discovered
I usually do rectangle intersection detection by getting the x/y position of an edge (only either the x or y depending on which edge it is) and do a comparison to the position of the opposite edge in the other rectangle. So like this:
aRight > bLeft &&
aLeft < bRight &&
aBottom > bTop &&
aTop < bBottom
If the position of rectangles were are its top-left corner, that would become
ax + aw > bx &&
ax < bx + bw &&
ay + ah > by &&
ay < by + bh
But since in Wick Editor, they’re instead at its center, So I have to change that equation to
ax + aw / 2 > bx - bw / 2 &&
ax - aw / 2 < bx + bw / 2 &&
ay + ah / 2 > by - bh / 2 &&
ay - ah / 2 < by + bh / 2
You do it like this:
wd = (aw + bw) / 2
hd = (ah + bh) / 2
ax < bx + wd &&
ax + wd > bx &&
ay < by + hd &&
ay + hd > by
These two ways of doing it look different, and they are because you came up with it a different way than I did. You look at the differences between the sizes and the distances between the rectangles, while I see if an edge is past another edge.
In my equation, I want it to be more simpler and shorter because it’s long. I was thinking of using variables but defining a variable for each side of both rectangles would make it longer in height. So I thought, maybe there’s a way to simplify this equation except not using 8 variables and instead a few. I supposed that if I changed the equations in some way using algebra, they would all show up a re-occuring operation that I could then turn into a variable. Both sides of the equations do both look similar enough.
boring math algebra
Statements | Reasons |
---|---|
ax + aw / 2 > bx - bw / 2 | Given |
ax + aw / 2 + bw / 2 > bx - bw / 2 + bw / 2 | Addition Property of Equality |
ax + aw / 2 + bw / 2 > bx | Addition |
ax + (aw + bw) / 2 > bx | Distributive Property of Equality |
ax - aw / 2 < bx + bw / 2 | Given |
ax - aw / 2 + aw / 2 < bx + bw / 2 + aw / 2 | Addition Property of Equality |
ax < bx + bw / 2 + aw / 2 | Addition |
ax < bx + (aw + bw) / 2 | Distributive Property of Equality |
So as you can see from what I highlighted in bold in the boring math algebra, from both the left-side and right-side equations I get (aw + bw) / 2
. The same thing happens for the top-side and bottom-side equation except it’s the height. So when I make that into variables wd
and hd
the equations turn into this:
ax + wd > bx &&
ax - wd < bx &&
ax + hd > by &&
ay - hd < by
If I take your way of doing it, move some variables to the other side of the equations and changing where some of the equations are located, getting this:
wd = (aw + bw) / 2
hd = (ah + bh) / 2
ax + wd > bx &&
ax - wd < bx &&
ay + hd > by &&
ay - hd < by &&
You can see it’s exactly the same thing as the equation I came up with. Which means that, technically, that way of doing it is exactly the same as my way of doing it. Which makes sense because they’re both just checking if two rectangles intersect, and there’s probably only one way of doing that.