Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Three Points Circle by macfreek
#!/usr/bin/env python3.3
# -*- coding: utf-8 -*-
from fractions import Fraction
from math import sqrt
def checkio(data):
"""
Given three points, calculate the circle through these points.
In other words, given the points A, B and C with coordinates (Ax,Ay),(Bx,By),(Cx,Cy),
find the variables x0,y0,d so that equation
(x-x0)^2 + (y-y0)^2 = d^2 is satisfied for each of A, B and C.
See e.g.:
https://en.wikipedia.org/wiki/Circumcircle
http://mathworld.wolfram.com/Circumcircle.html
http://www.mathportal.org/calculators/analytic-geometry/circle-through-three-points.php
"""
# For some reason, data input is a string. Convert to tuples.
# In real life, just eval() user input is a big No-No, but this is CheckIO,
# where the worst that can happen is having to comprehend Veky's code.
(Ax,Ay),(Bx,By),(Cx,Cy) = eval(data)
D = 2 * Ax * (By-Cy) + 2 * Bx* (Cy-Ay) + 2 * Cx * (Ay-By)
Ux = ( (Ax**2 + Ay**2) * (By-Cy) + (Bx**2 + By**2) * (Cy - Ay) + (Cx**2 + Cy**2) * (Ay - By) ) / D
Uy = ( (Ax**2 + Ay**2) * (Cx-Bx) + (Bx**2 + By**2) * (Ax - Cx) + (Cx**2 + Cy**2) * (Bx - Ax) ) / D
R = ( (Ax-Ux)**2 + (Ay-Uy)**2 ) ** 0.5
def f(num):
"""Peculiar formatting: round down to two decimal, and remove
extraneous zeros and all decimal points. This is different from
the normal str(round(num, 2)) as that yields "3.0" for num = 3.0024."""
num = str(round(num, 2))
if num.endswith(".0"):
return num[:-2]
return num
return "(x-{0})^2+(y-{1})^2={2}^2".format(f(Ux), f(Uy), f(R))
#These "asserts" using only for self-checking and not necessary for auto-testing
April 26, 2014
Comments: