Skip to content

Instantly share code, notes, and snippets.



Last active Sep 11, 2020
What would you like to do?
sample triangle class
class C:
def __init__(self, x, y):
self.x = x
self.y = y
def __repr__(self):
return f"({self.x}, {self.y})"
def __sub__(self, other):
return ((self.x - other.x) ** 2 + (self.y - other.y) ** 2) ** 0.5
class Triangle:
def __init__(self, coords):
self.p1 = C(*coords[0])
self.p2 = C(*coords[1])
self.p3 = C(*coords[2])
l1 = property(lambda self: self.p2 - self.p1)
l2 = property(lambda self: self.p3 - self.p2)
l3 = property(lambda self: self.p1 - self.p3)
def calc_ratios(self):
sides = sorted([self.l1, self.l2, self.l3])
self.ratios = sides[1] / sides[0], sides[2] / sides[0]
def __eq__(self, other):
if not isinstance(other, Triangle):
return False
return all(math.isclose(self.ratios[i], other.ratios[i], rel_tol=0.001) for i in (0, 1))
def __repr__(self):
return f"Triangle ([{(self.p1, self.p2, self.p3)}])"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment