checkio -- Three Points Circle

用三点确定一个圆

import math

def checkio(s):
    s=s.replace("("," ")
    s=s.replace(","," ")
    s=s.replace(")"," ")
    l=s.split(' ')
    n=[]
    for i in l:
        if len(i) != 0:
            n.append(int(i))
    a=2*(n[2]-n[0])
    b=2*(n[3]-n[1])
    c=n[2]*n[2]+n[3]*n[3]-n[0]*n[0]-n[1]*n[1]
    d=2*(n[4]-n[2])
    e=2*(n[5]-n[3])
    f=n[4]*n[4]+n[5]*n[5]-n[2]*n[2]-n[3]*n[3]
    x=(b*f-e*c)/float((b*d-e*a))
    y=(d*c-a*f)/float((b*d-e*a))
    r=math.sqrt((x-n[0])*(x-n[0])+(y-n[1])*(y-n[1]))
    x=round(x,2)
    if x==int(x):
        x=int(x)
    y=round(y,2)
    if y==int(y):
        y=int(y)
    r=round(r,2)
    if r==int(r):
        r=int(r)
    return "(x-"+str(x)+")^2+(y-"+str(y)+")^2="+str(r)+"^2"

你可能感兴趣的:(checkio -- Three Points Circle)