Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First numpy solution in 3rd party category for Similar Triangles by alterGNU
import numpy as np
from typing import List, Tuple
Coords = List[Tuple[int, int]]
def angle_between(pt1, pt2, pt3):
u = (pt2[0]-pt1[0],pt2[1]-pt1[1])
v = (pt3[0]-pt2[0],pt3[1]-pt2[1])
prod_scal= u[0] * v[0] + u[1] * v[1]
norm_u= np.sqrt(u[0]**2 + u[1]**2)
norm_v= np.sqrt(v[0]**2 + v[1]**2)
return round(np.arccos( prod_scal / (norm_u * norm_v) ) * 180 / np.pi,10)
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
angle1 = [ angle_between(coords_1[0], coords_1[1], coords_1[2]),angle_between(coords_1[1], coords_1[2], coords_1[0]),angle_between(coords_1[2], coords_1[0], coords_1[1])]
angle2 = [ angle_between(coords_2[0], coords_2[1], coords_2[2]),angle_between(coords_2[1], coords_2[2], coords_2[0]),angle_between(coords_2[2], coords_2[0], coords_2[1])]
angle1.sort()
angle2.sort()
if angle1 != angle2:
return False
return True
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 4, 2021
Comments: