Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Through the darkness solution in Clear category for Three Points Circle by Wojtas1411
import math
def checkio(data):
cord=data.split(',')
x=[]
y=[]
for i in range(len(cord)):
cord[i]=cord[i].strip('()')
#print(cord[i])
for i in range(0, len(cord), 2):
x.append(float(cord[i]))
y.append(float(cord[i+1]))
for i in range(3):
print(x[i], y[i])
if x[0]==x[1] or y[0]==y[1]:
a2 = (y[1]-y[2])/(x[1]-x[2])
a2 = (-1)/a2
w2x = x[1]-x[2]
w2x/=2
w2y = y[1]-y[2]
w2y/=2
xp2 = x[1]-w2x
yp2 = y[1]-w2y
b2 = yp2-(a2*xp2)
if x[0]==x[1]:
xp1=x[0]
yp1=y[0] + (y[1]-y[0])/2
ys=yp1
xs=(ys-b2)/a2
#print (xs,ys)
elif y[0]==y[1]:
yp1=y[0]
xp1=x[0] + (x[1]-x[0])/2
xs=xp1
ys=a2*xs+b2
print (xs,ys)
elif x[1]==x[2] or y[1]==y[2]:
a1 = (y[0]-y[1])/(x[0]-x[1])
a1 = (-1)/a1
w1x = x[0]-x[1]
w1x/=2
w1y = y[0]-y[1]
w1y/=2
xp1 = x[0]-w1x
yp1 = y[0]-w1y
b1 = yp1-(a1*xp1)
if x[1]==x[2]:
xp2=x[1]
yp2=y[1] + (y[2]-y[1])/2
ys=yp2
xs=(ys-b1)/a1
#print (xs,ys)
elif y[1]==y[2]:
yp1=y[1]
xp1=x[1] + (x[2]-x[1])/2
xs=xp1
ys=a1*xs+b1
print (xs,ys)
else:
a1 = (y[0]-y[1])/(x[0]-x[1])
a2 = (y[1]-y[2])/(x[1]-x[2])
#print (a1,a2)
if a1 != 0:
a1 = (-1)/a1
if a2 != 0:
a2 = (-1)/a2
#print (a1, a2)
w1x = x[0]-x[1]
w1x/=2
w1y = y[0]-y[1]
w1y/=2
w2x = x[1]-x[2]
w2x/=2
w2y = y[1]-y[2]
w2y/=2
#print (w1x, w1y, w2x, w2y)
xp1 = x[0]-w1x
yp1 = y[0]-w1y
xp2 = x[1]-w2x
yp2 = y[1]-w2y
#print (xp1, yp1, xp1, yp2)
b1 = yp1-(a1*xp1)
b2 = yp2-(a2*xp2)
xs=(b2-b1)/(a1-a2)
ys=a1*xs +b1
#print (xs, ys)
r=math.sqrt((xs-x[0])**2+(ys-y[0])**2)
print(r)
if xs.is_integer()==True:
xs=int(xs)
else:
xs=float("{0:.2f}".format(xs))
if ys.is_integer()==True:
ys=int(ys)
else:
ys=float("{0:.2f}".format(ys))
if r.is_integer()==True:
r=int(r)
else:
r=float("{0:.2f}".format(r))
if r.is_integer()==True:
r=int(r)
print(xs,ys,r)
res="(x"
if xs<0:
res+="+"
res+=str(abs(xs))
else:
res+="-"
res+=str(xs)
res+=")^2+(y"
if ys<0:
res+="+"
res+=str(abs(ys))
else:
res+="-"
res+=str(ys)
res+=")^2="
res+=str(r)
res+="^2"
print(res)
return res
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
assert checkio("(2,2),(6,2),(2,6)") == "(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"
Oct. 23, 2016