Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Law of Cosines solution in Clear category for Similar Triangles by Johan_Bjorhn
import math
from typing import List, Tuple
Coords = List[Tuple[int, int]]
def get_angles(coords):
x = math.dist(coords[0], coords[1])
y = math.dist(coords[1], coords[2])
z = math.dist(coords[2], coords[0])
print(f"x = {x}, y = {y}, z = {z}")
a = math.degrees(math.acos((x**2 + z**2 - y**2)/(2*x*z)))
b = math.degrees(math.acos((x**2 + y**2 - z**2)/(2*x*y)))
c = math.degrees(math.acos((y**2 + z**2 - x**2)/(2*y*z)))
print(f"a = {a} b = {b} c = {c}")
return [a,b,c]
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
Tri_1 = get_angles(coords_1)
Tri_2 = get_angles(coords_2)
Tri_1.sort()
Tri_2.sort()
if Tri_1 == Tri_2:
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!")
Jan. 13, 2021