Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
cmath solution in Clear category for Black Holes by DiZ
from itertools import combinations as c
from cmath import pi, acos, sqrt
def checkio(data):
space = list(map(list, data))
def distance(u, v):
return abs(complex(*u[:2]) - complex(*v[:2])) + 1e-9
def intersection(d, r1, r2):
""" http://mathworld.wolfram.com/Circle-CircleIntersection.html """
a = r1**2 * acos((d**2 + r1**2 - r2**2) / (2 * r1 * d)) + \
r2**2 * acos((d**2 + r2**2 - r1**2) / (2 * r2 * d)) - \
sqrt((r1 + r2 - d) * (r1 + d - r2) * (r2 + d - r1) * (d + r1 + r2)) / 2
return a.real
while 1:
for d, a, b in sorted((distance(u, v), u, v) for u, v in c(space, 2)):
(rs, s), (rb, b) = sorted((n[2], n) for n in (a, b))
if rb**2 / 1.2 >= rs**2 <= intersection(d, rs, rb) / pi / .55:
b[2] = abs(rs + 1j*rb)
space.remove(s)
break
else:
return space
Oct. 21, 2015