Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
simple solution in Clear category for Similar Triangles by Lemur21
from typing import List, Tuple
Coords = List[Tuple[int, int]]
from collections import namedtuple as NT
Point = NT("Point",'x y')
import math
def sorted_sides(co):
a = Point(co[0][0], co[0][1])
b = Point(co[1][0], co[1][1])
c = Point(co[2][0], co[2][1])
ab = (a.x - b.x)**2 + (a.y - b.y)**2
ac = (a.x - c.x)**2 + (a.y - c.y)**2
bc = (b.x - c.x)**2 + (b.y - c.y)**2
return sorted([ab, bc, ac])
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
a1, a2, a3 = sorted_sides(coords_1)
b1, b2, b3 = sorted_sides(coords_2)
return (a1*b2 == b1*a2 and a2*b3 == b2*a3)
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!")
Dec. 9, 2020
Comments: