Skip to content

Instantly share code, notes, and snippets.

@NotTheEconomist
Created June 2, 2015 23:16
Show Gist options
  • Save NotTheEconomist/28c23c2cc1943b49190b to your computer and use it in GitHub Desktop.
Save NotTheEconomist/28c23c2cc1943b49190b to your computer and use it in GitHub Desktop.
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __sub__(self, other):
"""Distance between two points
Point(1, 1) - Point(1, 0) == 1
"""
if not isinstance(other, Point):
raise NotImplementedError("Must subtract Points from Points")
return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
# Magically growing bicycle!!
center_of_mass = Point(0, 0) # flat area
rear_tire = Point(-2, 0)
front_tire = Point(2, 0)
bike_length = front_tire - rear_tire # 4.0
center_of_mass = Point(10, 0) # very bumpy right here!
rear_tire = Point(8, -4) # -2 pixels twice
front_tire = Point(12, 4) # +2 pixels twice
bike_length = front_tire - rear_tire # 8.94427190999916 OVER TWICE THE LENGTH!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment