Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Similar Triangles by pokryshkin
from typing import List, Tuple
Coords = List[Tuple[int, int]]
from math import sqrt
def similar_triangles(coords_1: Coords, coords_2: Coords) -> bool:
def distance(point1, point2):
# distance between 2 points
x1, y1 = point1
x2, y2 = point2
return round(sqrt(pow((x2-x1),2)+pow((y2-y1),2)),2)
triangle1 = (distance(coords_1[0],coords_1[1]), distance(coords_1[1],coords_1[2]), distance(coords_1[2],coords_1[0])) # to calculate the perimeter of the rectangle1
triangle2 = (distance(coords_2[0],coords_2[1]), distance(coords_2[1],coords_2[2]), distance(coords_2[2],coords_2[0])) # to calculate the perimeter of the rectangle2
k = sum(triangle1)/sum(triangle2) # to calculate the ratio of similitude of the triangles by the ratio of their perimeters
answer = set(triangle1) == set([round(x*k,2) for x in triangle2]) # to check whether all the corresponding sides have lengths in the same ratio or not
return answer
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'
print("Coding complete? Click 'Check' to earn cool rewards!")
April 3, 2021
Comments: