Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Similar Triangles by sawako.oono
from typing import List, Tuple
import math
import itertools
from decimal import *
getcontext().prec = 16
Coords = List[Tuple[int, int]]
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
x1,x2,x3=coords_1[0][0],coords_1[1][0],coords_1[2][0]
y1,y2,y3=coords_1[0][1],coords_1[1][1],coords_1[2][1]
X1,X2,X3=coords_2[0][0],coords_2[1][0],coords_2[2][0]
Y1,Y2,Y3=coords_2[0][1],coords_2[1][1],coords_2[2][1]
l1 = math.sqrt((x2-x1)**2+(y2-y1)**2)
l2 = math.sqrt((x3-x2)**2+(y3-y2)**2)
l3 = math.sqrt((x1-x3)**2+(y1-y3)**2)
L1 = math.sqrt((X2-X1)**2+(Y2-Y1)**2)
L2 = math.sqrt((X3-X2)**2+(Y3-Y2)**2)
L3 = math.sqrt((X1-X3)**2+(Y1-Y3)**2)
lcomb = itertools.permutations([l1,l2,l3])
Lcomb = itertools.permutations([L1,L2,L3])
for l in lcomb:
for L in Lcomb:
A1 = Decimal(str(L[0]/l[0])).quantize(Decimal('.0000000001'), rounding=ROUND_HALF_EVEN)
A2 = Decimal(str(L[1]/l[1])).quantize(Decimal('.0000000001'), rounding=ROUND_HALF_EVEN)
A3 = Decimal(str(L[2]/l[2])).quantize(Decimal('.0000000001'), rounding=ROUND_HALF_EVEN)
if A1 == A2 == A3:
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!")
May 29, 2021
Comments: