Closed landonjmitchell closed 5 years ago
Hi Landon!
You're right about the complication of intersects
possibly being true for multiple squares. Historically we've accepted several of the heuristic solutions you've listed.
That said, checking that your piece contains the center of the grid square is also a legible way to automatically get the square with the most overlap. If the game squares are at least the same size as the game piece (which they should be), plus the width of the grid lines, it'll only be possible for the game piece to contain one grid square's center at a time. I'm a fan of this solution. Go for it!
The assignment instructions state:
With the example:
I found using this meant the game piece often intersected more than one of the nine squares at once.
There are some heuristic solutions you could use to solve this (e.g. using the first one found, calculating the closest, very thick grid lines, etc). But the simplest solution I found was to instead use
CGRect
’scontains
method to check whether theCGPoint
center of my game piece is within one of the nine squares.It looks like it can also be used with another
CGRect
as a parameter to check if one frame is entirely contained within another (if your game piece is real small maybe?).Docs here Examples (some possibly outdated) here
@chelseatroy please let me know if there's any reason we shouldn't use this