Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
eazy to understand (pretty long though) solution in Clear category for Similar Triangles by 133905
from typing import List, Tuple
Coords = List[Tuple[int, int]]
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
A = coords_1[0]
B = coords_1[1]
c = coords_1[2]
D = coords_2[0]
E = coords_2[1]
f = coords_2[2]
AB = ((A[0] - B[0]) ** 2 + (A[1] - B[1]) ** 2)
DE = ((D[0] - E[0]) ** 2 + (D[1] - E[1]) ** 2)
Ac = ((A[0] - c[0]) ** 2 + (A[1] - c[1]) ** 2)
Df = ((D[0] - f[0]) ** 2 + (D[1] - f[1]) ** 2)
Bc = ((B[0] - c[0]) ** 2 + (B[1] - c[1]) ** 2)
Ef = ((E[0] - f[0]) ** 2 + (E[1] - f[1]) ** 2)
en = [AB, Ac, Bc]
ar = [DE, Df, Ef]
sorted1 = sorted(en)
sorted2 = sorted(ar)
if AB == DE and Ac == Df and Bc == Ef:
return True
if sorted1[0]/sorted2[0] == sorted1[1]/sorted2[1] == sorted1[2]/sorted2[2]:
return True
if AB/DE == Ac/Df == Bc/Ef:
return True
else:
return False
if __name__ == '__main__':
print("Example:")
print(similar_triangles([(1, 0), (1, 3), (2, 0)], [(3, 0), (5, 4), (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, 3), (2, 0)], [(3, 0), (5, 5), (5, 0)]) is False, 'different #2'
assert similar_triangles([(1, 0), (1, 2), (2, 0)], [(3, 0), (5, 4), (5, 0)]) is True, 'scaling and reflection'
print("Coding complete? Click 'Check' to earn cool rewards!")
June 28, 2021
Comments: