# Detect if square in grid is within a diamond shape

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?

• Ah, Manhattan distance! That's it! So I have: (x - o).abs().sum() <= half where abs() returns the coordinates' absolute values and sum() returns the sum of the coordinates. – slartibartfast Jul 3 '12 at 2:25