Search code examples
pythonpython-3.xmath

How do I find the circumcenter of the triangle using python without external libraries?


I am trying to find the circumcenter of a triangle with python and no external libraries for a geometry calculator that I am making. The thing is I can't use equations such as y=mx+b because the computer thinks that I am trying to define a variable rather than doing algebra.

I have tried a lot of different things such as using sympy and shapely and none of them worked. So far I can find the midpoint and the slope. I am not really sure what to do. Please help. Thank you!

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

There is no output yet because if I print something it will not mean anything other than the slope.


Solution

  • You just should apply formulas from Wikipedia:

    The Cartesian coordinates of the circumcenter are:

    enter image description here

    with

    enter image description here

    So your code is:

    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)