Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
return s1[0]/s2[0] == s1[1]/s2[1] == s1[2]/s2[2] solution in Clear category for Similar Triangles by Igroc88
from typing import List, Tuple
Coords = List[Tuple[int, int]]
def side_length(x: Coords):
fx = lambda x,y: ((x[0]-y[0])**2+(x[1]-y[1])**2)
s1 = fx(x[0], x[1])
s2 = fx(x[0], x[2])
s3 = fx(x[2], x[1])
return sorted([s1,s2,s3])
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
s1 = side_length(coords_1)
s2 = side_length(coords_2)
return s1[0]/s2[0] == s1[1]/s2[1] == s1[2]/s2[2]
if __name__ == '__main__':
print("Example:")
print(similar_triangles([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 2), (5, 0)]))
# These "asserts" are used for self-checking and not for an auto-testing
assert similar_triangles([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 2), (5, 0)]) is True, 'basic'
assert similar_triangles([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 3), (5, 0)]) is False, 'different #1'
assert similar_triangles([(0, 0), (1, 2), (2, 0)], [(2, 0), (4, 4), (6, 0)]) is True, 'scaling'
assert similar_triangles([(0, 0), (0, 3), (2, 0)], [(3, 0), (5, 3), (5, 0)]) is True, 'reflection'
assert similar_triangles([(1, 0), (1, 2), (2, 0)], [(3, 0), (5, 4), (5, 0)]) is True, 'scaling and reflection'
assert similar_triangles([(1, 0), (1, 3), (2, 0)], [(3, 0), (5, 5), (5, 0)]) is False, 'different #2'
print("Coding complete? Click 'Check' to earn cool rewards!")
Feb. 7, 2021