Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
23-liner: c-hull solution in Clear category for Convex Hull by przemyslaw.daniel
from math import pi, acos, hypot, degrees, fmod
def calc_angle_and_distance(a, b, last_angle):
(x1, y1), (x2, y2) = a, b
distance = hypot(x1-x2, y1-y2)
if not distance:
return 360, 0
angle = acos((y2-y1)/distance)
angle = degrees(2*pi-angle if x1 > x2 else angle)
return fmod(360+angle-last_angle, 360), distance
def checkio(data):
out, data, last_angle = [min(data)], data, 0
for _ in range(len(data)+1):
d = [calc_angle_and_distance(out[-1], x, last_angle)
for x in data]
next_point = data[d.index(min(d))]
out, last_angle = out+[next_point], min(d)[0]
if len({str(x) for x in out}) != len(out):
break
return [data.index(x) for x in out[:-1]]
Dec. 7, 2016