Detect if square in grid is within a diamond shape

Posted by myrkos on Game Development See other posts from Game Development or by myrkos
Published on 2012-07-02T20:10:24Z Indexed on 2012/07/02 21:24 UTC
Read the original article Hit count: 270

Filed under:
|
|
|

So I have a game in which basically everything is a square inside a big grid. It's easy to check if a square is inside a box whose center is another square:

*** x
*o*      --> x is not in o's square
***

**x
*o*      --> x IS in o's square
***

This can be done by simply subtracting the coordinates of o and x, then taking the largest coordinate of that and comparing it with the half side length.

Now I want to do the same thing but check if x is in o's diamond, like so:

  *
 **x
**o**    --> x IS in o's diamond
 ***
  *

What would be the best way to check if a square is in another square's surrounding diamond-shaped area, given the diamond's half width/height?

© Game Development or respective owner

Related posts about c++

Related posts about vector