Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Three Points Circle by Bibiche
def points(s):
s = s.replace('(', '')
s = s.replace(')', '')
return list(map(int, s.split(',')))
def round2(t):
s = str(int(100 * (t + 0.005)) / 100)
if s.split('.')[1] == '0':
return str(int(t))
else:
return s
def mp(t):
if t >= 0:
return '-' + round2(t)
else:
return '+' + round2(-t)
def equation(center, radius):
return '(x' + mp(center[0]) + ')^2+(y' + mp(center[1]) + ')^2=' + round2(radius) + '^2'
# returns determinant of matrix 2x2 :[a,b\ c,d]
def det(a,b,c,d):
return a * d - c * b
def center_radius(data):
xa, ya, xb, yb, xc, yc = data
# solve linear system using Cramer formula
d = det(xb-xa, yb-ya, xb-xc, yb-yc)
dx = det(xb**2-xa**2+yb**2-ya**2, yb-ya, xb**2-xc**2+yb**2-yc**2, yb-yc) / 2
dy = det(xb-xa, xb**2-xa**2+yb**2-ya**2, xb-xc, xb**2-xc**2+yb**2-yc**2) / 2
#center coordinates
xc = dx / d
yc = dy / d
# radius
r = ((xc - xa)**2 + (yc - ya) **2)**0.5
# only two digits
return equation([xc, yc], r)
def checkio(data):
data = points(data)
return center_radius(data)
if __name__ == '__main__':
assert checkio("(2,2),(4,2),(2,4)") == "(x-4)^2+(y-4)^2=2.83^2"
assert checkio("(3,7),(6,9),(9,7)") == "(x-6)^2+(y-5.75)^2=3.25^2"
June 14, 2013
Comments: