问题描述:已知三点不共线坐标,做外接圆,求圆心坐标x0,y0,半径R
算法思想:根据三点到圆心的距离为R*R,联立求出
python实现:
class Point():
def __init__(self, x, y):
self.x = x
self.y = y
def getCircle(p1, p2, p3):
x21 = p2.x - p1.x
y21 = p2.y - p1.y
x32 = p3.x - p2.x
y32 = p3.y - p2.y
# three colinear
if (x21 * y32 - x32 * y21 == 0):
return None
xy21 = p2.x * p2.x - p1.x * p1.x + p2.y * p2.y - p1.y * p1.y
xy32 = p3.x * p3.x - p2.x * p2.x + p3.y * p3.y - p2.y * p2.y
y0 = (x32 * xy21 - x21 * xy32) / 2 * (y21 * x32 - y32 * x21)
x0 = (xy21 - 2 * y0 * y21) / (2.0 * x21)
R = ((p1.x - x0) ** 2 + (p1.y - y0) ** 2) ** 0.5
return x0, y0, R
p1, p2, p3 = Point(0, 0), Point(4, 0), Point(2, 2)
print(getCircle(p1, p2, p3))
输出
(2.0, 0, 2.0)