Given a grid of squares, I need to figure out how, when a player places an object with a square footprint, to calculate what percentage of that footprint is in each grid square. Put more simply, how do I calculate the area of intersection of two squares.
I know how elementary this seems, but I don’t want to have to write a bunch of conditional statements that seem to be necessary given that the math changes somewhat based on the position of one square relative to another.
For example, assuming no rotation of either square, if the upper left quadrant of square B intersects square A in A’s lower right quadrant, then, to get the area, it would be (A.xMax-B.xMin)*(A.yMax-B.yMin). But, if the intersetion occurs in the upper left quadrant of B and the lower right quad of A, the math changes somewhat.
I’m looking for a solution that works regardless of where one square intersects the other. Furthermore, I need one that will handle the eventuality that square B will be rotated such that the respective sides of the two squares are not parallel.