我如何在没有外部库的情况下使用python找到三角形的外心?

时间:2019-05-20 16:26:43

标签: python python-3.x math

我正在尝试使用python查找三角形的外接点,并且没有用于我制作的几何计算器的外部库。问题是我不能使用诸如y = mx + b之类的方程式,因为计算机认为我正在尝试定义变量而不是代数。

我尝试了很多不同的方法,例如使用sympy和shapely,但没有一个起作用。到目前为止,我可以找到中点和斜率。我不太确定该怎么办。请帮忙。谢谢!

def circumcenter():
    c1 = float(input('What is x of point 1?'))
    c2 = float(input('What is y of point 1?'))
    c3 = float(input('What is x of point 2?'))
    c4 = float(input('What is y of point 2?'))
    c5 = float(input('What is x of point 3?'))
    c6 = float(input('What is y of point 3?'))
    m1 = c1 + c3
    m2 = c2 + c4
    m3 = m1 / 2
    m4 = m2 / 2
    s1a = c3 - c1
    s1b = c4 - c2
    s1c = s1a / s1b
    s2a = c5 - c3
    s2b = c6 - c4
    s2c = s2a / s2b
    s1 = -1 / s1c
    s2 = -1 / s2c

目前还没有输出,因为如果我打印某些内容,除了倾斜度以外,其他任何内容都不会。

1 个答案:

答案 0 :(得分:2)

您只应应用Wikipedia中的公式:

  

外接点的笛卡尔坐标为:

enter image description here

  

使用

enter image description here

  

所以您的代码是:

def circumcenter():
    ax = float(input('What is x of point 1?'))
    ay = float(input('What is y of point 1?'))
    bx = float(input('What is x of point 2?'))
    by = float(input('What is y of point 2?'))
    cx = float(input('What is x of point 3?'))
    cy = float(input('What is y of point 3?'))
    d = 2 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by))
    ux = ((ax * ax + ay * ay) * (by - cy) + (bx * bx + by * by) * (cy - ay) + (cx * cx + cy * cy) * (ay - by)) / d
    uy = ((ax * ax + ay * ay) * (cx - bx) + (bx * bx + by * by) * (ax - cx) + (cx * cx + cy * cy) * (bx - ax)) / d
    return (ux, uy)