Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Complex numbers solution in Clear category for Inscribe a Contour by Leonix
import itertools
def inscribe(contour):
""" Use complex numbers as coordinates because they are trivial to rotate.
Rotate the figure all possible ways: try each pair of vertices to form a horizontal edge.
Project rotated figure to reference axes and thus calculate rectangular area.
"""
contour = [complex(x, y) for x, y in contour]
return min(area(rotated_contour(p1, p2, contour))
for p1, p2 in itertools.combinations(contour, 2))
def area(contour):
min_x = min(p.real for p in contour)
max_x = max(p.real for p in contour)
min_y = min(p.imag for p in contour)
max_y = max(p.imag for p in contour)
return (max_x - min_x) * (max_y - min_y)
def rotated_contour(p1, p2, contour):
return [(p - p1) * abs(p2 - p1) / (p2 - p1) for p in contour]
June 9, 2021