Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mildsunrise
Last active December 21, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mildsunrise/6294440 to your computer and use it in GitHub Desktop.
Save mildsunrise/6294440 to your computer and use it in GitHub Desktop.
Best way to check wether a square is "next to" another square.
# Returns `True` if the square at `a` is in one of the
# **eight surrounding squares** of `b`; `False` otherwise.
def nextTo(a, b):
x = a[0] - b[0]
y = a[1] - b[1]
d = x*x + y*y
return 0 < d < 3
a = [4, -7]
b = [3, -8]
if nextTo(a, b):
print "Yes, they're next!"
else:
print "No, sorry."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment