Skip to content

Instantly share code, notes, and snippets.

@henriquebastos
Created June 30, 2020 22:06
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save henriquebastos/5bb246853b9d7cc26317fb9ab35e4a28 to your computer and use it in GitHub Desktop.
Save henriquebastos/5bb246853b9d7cc26317fb9ab35e4a28 to your computer and use it in GitHub Desktop.
"""Geometry"""
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def __eq__(self, other):
return self.x == other.x and self.y == other.y
def __add__(self, other):
return self.__class__(self.x + other.x, self.y + other.y)
def __truediv__(self, scalar):
return self.__class__(self.x / scalar, self.y / scalar)
def __repr__(self):
return f'{self.__class__.__name__}({self.x}, {self.y})'
class Rect:
def __init__(self, topLeft, botRight):
self.topLeft = topLeft
self.botRight = botRight
def center(self):
return (self.topLeft + self.botRight) / 2
def __repr__(self):
return f'{self.__class__.__name__}({self.topLeft!r}, {self.botRight!r})'
def test_rect():
r = Rect(Point(1, 1), Point(3, 3))
assert isinstance(r, Rect)
assert r.topLeft == Point(1, 1)
assert r.botRight == Point(3, 3)
assert r.center() == Point(2, 2)
assert repr(r) == 'Rect(Point(1, 1), Point(3, 3))'
def test_point():
p = Point(2, 4)
assert isinstance(p, Point)
assert p.x == 2
assert p.y == 4
[pytest]
python_files = *.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment