已知三点坐标,求圆心坐标 (外接圆)python实现

问题描述:已知三点不共线坐标,做外接圆,求圆心坐标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)

 

    原文作者:欢乐的小猪
    原文地址: https://blog.csdn.net/hbu_pig/article/details/82458452
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞