Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Similar Triangles by helebed
import math
from typing import List, Tuple
Coords = List[Tuple[int, int]]
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
triangle = []
a, b, c = coords_1
d, e, f = coords_2
def angle(a,b,c):
ang = math.degrees(math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0]))
res = abs(ang)
if res > 180:
res = 360 - res
triangle.append(res)
angle(a,b,c)
angle(b,c,a)
angle(c,a,b)
angle(d,e,f)
angle(e,f,d)
angle(f,d,e)
triangle = list(map(lambda x: round(x), triangle))
return sorted(triangle[:3]) == sorted(triangle[3:])
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 27, 2021
Comments: