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

问题描述 投票:0回答:1

我正在尝试用 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

还没有输出,因为如果我打印一些东西,它除了坡度之外不会意味着任何东西。

python python-3.x math
1个回答
7
投票

您只需应用维基百科中的公式:

外心的笛卡尔坐标为:

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)
© www.soinside.com 2019 - 2024. All rights reserved.