Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
ELECTRONIC STATION - "Similar Triangles" solution in Clear category for Similar Triangles by jsg-inet
from typing import List, Tuple
Coords = List[Tuple[int, int]]
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
def segment_length(K: tuple, L: tuple)->float:
#print(pow(pow((L[0]-K[0]),2)+pow((L[1]-K[1]),2), 1/2))
return pow(pow((L[0]-K[0]),2)+pow((L[1]-K[1]),2), 1/2)
#print('TRIANGLE 1')
# TRIANGLE 1 (ABC):
# VERTEX ABC
A=coords_1[0]
B=coords_1[1]
C=coords_1[2]
#SEGMENT LENGTHS ABC
AB=segment_length(A,B)
BC=segment_length(B,C)
CA=segment_length(C,A)
#LENGHT LIST ORDERED ABC
lengths_ABC = [AB, BC, CA]
lengths_ABC.sort()
#print(lengths_ABC)
#print('TRIANGLE 2')
# TRIANGLE 2 (XYZ):
# VERTEX XYZ
X=coords_2[0]
Y=coords_2[1]
Z=coords_2[2]
#SEGMENT LENGTHS XYZ
XY=segment_length(X,Y)
YZ=segment_length(Y,Z)
ZX=segment_length(Z,X)
#LENGHT LIST ORDERED XYZ
lengths_XYZ = [XY, YZ, ZX]
lengths_XYZ.sort()
#print(lengths_XYZ)
# SIMILAR TRIANGLES COMPARISON
ratios = set()
for i in range(3): ratios.add(lengths_ABC[i]/lengths_XYZ[i])
#print(ratios)
#print(len(ratios))
if len(ratios)==1:
return True
else:
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!")
July 2, 2021