Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
triangle side ratio equals solution in Clear category for Similar Triangles by pakhomovegor
from typing import List, Tuple
Coords = List[Tuple[int, int]]
def line_dist(x1,y1,x2,y2): #distance between two points
return ((x1-x2)**2+(y1-y2)**2)**0.5
def longsides_to_small(coords: Coords): #returns tuple (b/a,c/a) where a,b,c - triangle sides ordered by distance
x,y,z=coords
a=line_dist(*x,*y)
b=line_dist(*x,*z)
c=line_dist(*z,*y)
out=sorted([a,b,c])
return (out[1]/out[0],out[2]/out[0])
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
return longsides_to_small(coords_1)==longsides_to_small(coords_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 30, 2021