Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Check solution in Uncategorized category for Similar Triangles by andromedk
from typing import List, Tuple
Coords = List[Tuple[int, int]]
import math
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
angles = []
for a, b, c in (coords_1, coords_2):
ab = round(math.hypot(abs(b[0] - a[0]), abs(b[1] - a[1])),3)
bc = round(math.hypot(abs(c[0] - b[0]), abs(c[1] - b[1])),3)
ac = round(math.hypot(abs(c[0] - a[0]), abs(c[1] - a[1])),3)
cos_a = round((ab ** 2 + ac ** 2 - bc ** 2) / (2 * ab * ac),2)
cos_b = round((ab**2 + bc**2 - ac**2) / (2 * ab * bc),2)
cos_y = round((bc**2 + ac**2 - ab**2) / (2 * bc * ac),2)
angles.append([cos_a, cos_b, cos_y])
return sorted(angles[0]) == sorted(angles[1])
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. 10, 2021