Last active
December 21, 2015 10:49
-
-
Save mildsunrise/6294440 to your computer and use it in GitHub Desktop.
Best way to check wether a square is "next to" another square.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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