uchicago-mobi / MPCS51030-2019-Spring-Forum

This repository will contain code examples and discussion topics for the winter 2019 quarter.
1 stars 1 forks source link

An alternative to .intersects #27

Closed landonjmitchell closed 5 years ago

landonjmitchell commented 5 years ago

The assignment instructions state:

Use CGRect’s intersect method to determine if the piece is overlapping one of your nine views.

With the example:

frame1.intersects(frame2) // returns a boolean

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’s contains method to check whether the CGPoint 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

chelseatroy commented 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!