Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Similar Triangles by 2dfsdfl
from typing import List, Tuple
Coords = List[Tuple[int, int]]
def get_lines(coords):
L01 = pow( coords[0][0]-coords[1][0],2) + pow(coords[0][1]-coords[1][1],2)
L02 = pow( coords[0][0]-coords[2][0],2) + pow(coords[0][1]-coords[2][1],2)
L12 = pow( coords[1][0]-coords[2][0],2) + pow(coords[1][1]-coords[2][1],2)
return [ L01,L02,L12 ]
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
L1 = get_lines(coords_1)
L2 = get_lines(coords_2)
L1.sort()
L2.sort()
return L1[0]/L2[0]==L1[1]/L2[1]==L1[2]/L2[2]
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!")
June 3, 2021
Comments: