from typing import List, Tuple
import math
import numpy as np
Coords = List[Tuple[int, int]]
def similar_triangles(coords_1: Coords, coords_2: Coords):
coords_x = coords_1
coords_y = coords_2
a = math.sqrt((coords_x[0][0]-coords_x[1][0])**2 + (coords_x[0][1]-coords_x[1][1])**2)
b = math.sqrt((coords_x[0][0]-coords_x[2][0])**2 + (coords_x[0][1]-coords_x[2][1])**2)
c = math.sqrt((coords_x[1][0]-coords_x[2][0])**2 + (coords_x[1][1]-coords_x[2][1])**2)
A = math.degrees(math.acos((a*a-b*b-c*c)/(-2*b*c)))
B = math.degrees(math.acos((b*b-a*a-c*c)/(-2*a*c)))
C = math.degrees(math.acos((c*c-a*a-b*b)/(-2*a*b)))
o = math.sqrt((coords_y[0][0]-coords_y[1][0])**2 + (coords_y[0][1]-coords_y[1][1])**2)
p = math.sqrt((coords_y[0][0]-coords_y[2][0])**2 + (coords_y[0][1]-coords_y[2][1])**2)
q = math.sqrt((coords_y[1][0]-coords_y[2][0])**2 + (coords_y[1][1]-coords_y[2][1])**2)
O = math.degrees(math.acos((o*o-p*p-q*q)/(-2*p*q)))
P = math.degrees(math.acos((p*p-o*o-q*q)/(-2*o*q)))
Q = math.degrees(math.acos((q*q-o*o-p*p)/(-2*o*p)))
if ( A in (O,P,Q) and B in (O,P,Q) and C in (O,P,Q) ):
return True
else:
return False
# return True? 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
assert similar_triangles([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 3), (5, 0)]) is False
assert similar_triangles([(0, 0), (1, 2), (2, 0)], [(2, 0), (4, 4), (6, 0)]) is True
assert similar_triangles([(0, 0), (0, 3), (2, 0)], [(3, 0), (5, 3), (5, 0)]) is True
assert similar_triangles([(1, 0), (1, 2), (2, 0)], [(3, 0), (5, 4), (5, 0)]) is True
assert similar_triangles([(1, 0), (1, 3), (2, 0)], [(3, 0), (5, 5), (5, 0)]) is False
# similar_triangles([(0, 0), (1, 2), (2, 0)], [(3, 0), (4, 2), (5, 0)])
文章评论