Search code examples
pythonmathgeometryangle

Calculate angle of triangle Python


I'm trying to find out the angle of the triangle in the following, I know it should be 90 degrees, however I don't know how to actually calculate it in the following:

enter image description here

Here's what I've tried:

angle = math.cos(7/9.899)
angleToDegrees = math.degrees(angle)

returns: 43.XX

What am I doing wrong?


Solution

  • It's a little more compicated than that. You need to use the law of cosines

    >>> A = 7
    >>> B = 7
    >>> C = 9.899
    >>> from math import acos, degrees
    >>> degrees(acos((A * A + B * B - C * C)/(2.0 * A * B)))
    89.99594878743945
    

    This is accurate to 4 significant figures. If you provide a more precise value of C, you get a more accurate result.

    >>> C=9.899494936611665
    >>> degrees(acos((A * A + B * B - C * C)/(2.0 * A * B)))
    90.0