Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Brute force solution in Creative category for Three Points Circle by Talim42
STEPS = [(.4, .4), (-.4, -.4), (-.4, .4), (.4, -.4), (0, .5), (.5, 0), (0, -.5), (-.5, 0)]
def checkio(input):
def dist_e(p0, p1):
#Euclidean distance
return ((p0[0]-p1[0])**2 + (p0[1]-p1[1])**2)**.5
def dist_m(p012, c):
#Measures error of estimation
dists = sorted([dist_e(p, (c[0], c[1])) for p in p012])
return dists[2] - dists[0]
p_list = map(lambda x: x.split(","), input.replace("),(", ";")[1:-1].split(";"))
p012 = [(int(p[0]), int(p[1])) for p in p_list]
#express input in convinient form
cx = (p012[0][0] + p012[1][0] + p012[2][0]) / 3.0
cy = (p012[0][1] + p012[1][1] + p012[2][1]) / 3.0
#starting point of seeking algo
dist0 = dist_m(p012, (cx, cy))
depth = 1
while dist0 > .00000001:
moving_closer = True
while moving_closer:
moving_closer = False
for dx, dy in STEPS:
dx /= depth; dy /= depth
dist1 = dist_m(p012, (cx + dx, cy + dy))
while dist1 < dist0:
#moving center of the circle to the better point
cx += dx; cy += dy
dist0 = dist1
dist1 = dist_m(p012, (cx + dx, cy + dy))
moving_closer = True
depth *= 2 # let's try with smaller steps
#if depth >1000000 : break # uh oh, smth went wrong!
cr = round(dist_e(p012[0], (cx, cy)), 2)
cx, cy = round(cx, 2), round(cy, 2)
return "(x-{:g})^2+(y-{:g})^2={:g}^2".format(cx, cy, cr)
April 24, 2014