Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
native_similar_triangles solution in Clear category for Similar Triangles by Jon_Red
def similar_triangles(coords_1,coords_2)->bool:
distance=lambda a,b:((b[0]-a[0])**2+(b[1]-a[1])**2)**.5
edges=[sorted([distance(c[x],c[x-1])for x in range(len(c))])
for c in(coords_1,coords_2)]
return len({round(a/b,2)for a,b in zip(*edges)})==1
if __name__=='__main__':
# self-checks
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'
assert similar_triangles(
[(0,2),(5,1),(-1,5)],[(-7,0),(-10,9),(8,-3)]
)is True,'round'
Aug. 21, 2020
Comments: