Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Similar Triangles by aleks040271
from typing import List, Tuple
from math import sqrt
from itertools import permutations
Coords = List[Tuple[int, int]]
def edge(a, b):
return sqrt((b[0] - a[0])**2 + (b[1] - a[1])**2)
def count_eges(coords):
return (edge(coords[0], coords[1]), edge(coords[1], coords[2]), edge(coords[2], coords[0]))
def similar_triangles(coords_1, coords_2) -> bool:
tre_1 = count_eges(coords_1)
tre_2 = count_eges(coords_2)
all_tre_2 = list(permutations(tre_2))
for tre in all_tre_2:
if len(set((round(tre[i] / tre_1[i], 10) for i in range(3)))) == 1:
return True
return False
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!")
May 12, 2021