Skip to content

Instantly share code, notes, and snippets.

@binki
Created October 25, 2018 21:27
Show Gist options
  • Save binki/b079cf296c3aed62e7775abb317b1440 to your computer and use it in GitHub Desktop.
Save binki/b079cf296c3aed62e7775abb317b1440 to your computer and use it in GitHub Desktop.
Collide rectangles in Python (touching considered not a collision) with tests
#!/usr/bin/env python
class Rectangle:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
# Alternative way of thinking about things
self.left = x
self.top = y
self.right = x + width
self.bottom = y + height
def collidesWith(self, other):
return self.left < other.right and self.right > other.left and self.top < other.bottom and self.bottom > other.top
def __str__(self):
return 'Rectangle(' + str(self.x) + ', ' + str(self.y) + ', ' + str(self.width) + ', ' + str(self.height) + ')'
if __name__ == '__main__':
def pairFromTuples(info):
return [Rectangle(x[0], x[1], x[2], x[3]) for x in info]
collidingPairs = [pairFromTuples(x) for x in [
# Identical
((0, 0, 1, 1), (0, 0, 1, 1)),
# Overlapping
((0, 0, 1, 1), (0.5, 0.5, 1, 1)),
((0, 0, 1, 1), (-0.5, -0.5, 1, 1)),
# Nested
((0, 0, 1, 1), (0.5, 0.5, 0.25, 0.25)),
]]
nonCollidingPairs = [pairFromTuples(x) for x in [
# No overlap
((0, 0, 1, 1), (2, 2, 1, 1)),
# Overlap in horizontal
((0, 0, 1, 1), (0.5, 2, 1, 1)),
((0, 0, 1, 1), (-0.5, 2, 1, 1)),
((0, 0, 1, 1), (0.5, -2, 1, 1)),
((0, 0, 1, 1), (-0.5, 2, 1, 1)),
# Overlap in vertical
((0, 0, 1, 1), (2, 0.5, 1, 1)),
((0, 0, 1, 1), (2, -0.5, 1, 1)),
((0, 0, 1, 1), (-2, 0.5, 1, 1)),
((0, 0, 1, 1), (-2, -0.5, 1, 1)),
]]
def reportError(message, a, b):
raise Exception(message + ' a=' + str(a) + ', b=' + str(b))
for a, b in collidingPairs:
if not a.collidesWith(b):
reportError('should collide', a, b)
if not b.collidesWith(a):
reportError('should collide', b, a)
for a, b in nonCollidingPairs:
if a.collidesWith(b):
reportError('shouldn’t collide', a, b)
if b.collidesWith(a):
reportError('shouldn’t collide', b, a)
print('All tests pass')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment