Skip to content

Instantly share code, notes, and snippets.

@azengard
Forked from henriquebastos/geometry.py
Created July 2, 2020 04:37
Show Gist options
  • Save azengard/2b88dfc3231f16207e3a0f6d2040777a to your computer and use it in GitHub Desktop.
Save azengard/2b88dfc3231f16207e3a0f6d2040777a 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