作为一个论坛提问,程序代码量挺大的
需要用到一定的算法
写了一下,但是没有写任何容错措施。运行时,一定要按照你所说的格式进行输入import math
def get_center(point1,point2,point3):#求三点的外接圆的圆心坐标
a = point2[0] - point1[0]
b = point2[1] - point1[1]
c = point3[0] - point2[0]
d = point3[1] - point2[1]
e = point2[0]**2 + point2[1]**2 - point1[0]**2 - point1[1]**2
f = point3[0]**2 + point3[1]**2 - point2[0]**2 - point2[1]**2
x = (f*b-e*d)/(c*b-a*d)/2;
y = (a*f-e*c)/(a*d-b*c)/2;
return (x,y)
def get_result(points):#求多点的最小覆盖圆的半径和圆心坐标
center = points[0]
rad = 0
def d(point1,point2):
return math.sqrt((point1[0]-point2[0])**2+(point1[1]-point2[1])**2)
for i in range(len(points)):
if d(points[i],center)>rad:
center = points[i]
rad = 0
for j in range(i):
if d(points[j],center)>rad:
center = ((points[i][0]+points[j][0])/2,(points[i][1]+points[j][1])/2)
rad = d(points[i],points[j])/2
for k in range(j):
if d(points[k],center)>rad:
center = get_center(points[i],points[j],points[k])
rad = d(points[i],center)
return (center,rad)
result=[]
while True:#循环获取输入,并计算
n = int(input("请输入坐标点数(输入0退出):"))
if n == 0:
break
points = []
des = []
max_d = (0,0,0)
for i in range(n):#输入n个坐标点
x , y = (eval(k) for k in input("输入第%d个点的坐标"%(i+1)).split())
points.append((x,y))
center,rad=get_result(points)#计算n个点的最小覆盖圆
result.append((int(center[0]*100),int(center[1]*100),int(rad*100)))#将结果扩大100倍并取整
for each in result:#循环输出结果
print(each[0],each[1],each[2],sep=' ')
|