Another approach you could consider, is creating a bit map of the safe area (and another of the warning zone, if you want). Using S for set safe bitmask bits, W for set warning and safe zone bitmask bits, and . for unset bits, this could look like
Code:
(0,0)
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . W W . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . W W W . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . W W W W . . . . . . . . . . . . . . . . . . . .
. . . . . . . . W W W W W . . . . . . . . . . . . . . . . . . .
. . . . . . . . W W W S W W W . . . . W W W . . . . . . . . . .
. . . . . . . . . W W S S W W W W W W W W W W . . . . . . . . .
. . . . . . . . . W W W S S W W W W W W S W W W . . . . . . . .
. . . . . . . . . . W W S S S S W W W S S S W W W . . . . . . .
. . . . . . . . . . W W S S S S S S S S S S S W W W W . . . . .
. . . . . . . . . . W W S S S S S S S S S S S S W W W W . . . .
. . . . . . . . . . W W S S S S S S S S S S S S S W W W W . . .
. . . . . . . . . W W W S S S S S S S S S S S S S S S W W W . .
. . . . . . . . . W W S S S S S S S S S S S S S S S W W W W W .
. . . . . . . . . W W S S S S S S S S S S S S S W W W W W . . .
. . . . . . . . W W S S S S S S S S S S S S W W W W W . . . . .
. . . . . . . . W W S S S S S S S S S S S W W W W . . . . . . .
. . . . . . . . W W S S S S S S S S S S S W W . . . . . . . . .
. . . . . . . . W W S S S S S S S S S S S W W . . . . . . . . .
. . . . . . . W W W S S S S S S S S S S W W W . . . . . . . . .
. . . . . . . W W W W S S S S S S S S S W W . . . . . . . . . .
. . . . . . . W W W W W W W S S S S S S W W . . . . . . . . . .
. . . . . . . . . . W W W W W W S S S W W W . . . . . . . . . .
. . . . . . . . . . . . . W W W W W W W W . . . . . . . . . . .
. . . . . . . . . . . . . . . W W W W W W . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . W W . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
(31,31)
The above uses your original coordinates. I think.
The above two bitmaps only need 1024 bits, or 128 bytes of storage per bitmap. In flash or ROM on a microcontroller, of course. If the warning zone is at the edge only, then you only need one bitmap; you can easily test if one of the four (or eight) neighboring bits is zero. Checking if there is a zero bit in a rectangular area around some coordinates is also easy.
For 64x64, you need 512 bytes per bitmap. For 128x128, 2048 bytes. For 256x256, 8192 bytes per bitmap. And so on: width × height / 8 bytes.
You could use
Code:
static const unsigned char safe_bitmap[HEIGHT][WIDTH/8] = { ... };
static inline int safe(const int x, const int y)
{
if (x >= 0 && y >= 0 && x < HEIGHT && y < WIDTH)
return !!(safe_bitmap[y][x / 8] & (1U << (x & 7)));
else
return 0; /* Not safe! */
}
static inline int warn(const int x, const int y)
{
return !(safe(x-1, y-1) && safe(x, y-1) && safe(x+1, y-1) &&
safe(x+1, y) && safe(x+1, y+1) && safe(x, y+1) &&
safe(x-1, y+1) && safe(x-1, y));
}
where !!(expression) is the not not operator. It just returns false/0 when (expression) is also false/zero, and true/1 otherwise. This way the function returns 1 if in a safe pixel, 0 otherwise. You can omit the !! if it bugs you; then the function returns nonzero when safe, 0 otherwise.
The warn() above is simplistic, returning true/1 if there is at least one unsafe pixel around the coordinates, and false/0 otherwise. There are more efficient ways of coding it, especially detecting unsafe areas from further (multiple pixels) away.
If the doggie is outside the safe area, (!safe(dog_x, dog_y)), ZAP!. Otherwise, if (warn(dog_x, dog_y)), BEEP!. Otherwise, GOOD DOG.