Search code examples
python

Baffled by Python math geometric output


In Python I want to move an object away from a second object by calculating the length of the hypotenuse, adding to its length and then calculatin the x and y co-ordinates of the new position. To do this I'm using the Python math module.

From my school days I remember 'soh car toa' where a is the adjacent side of the angle, h is the hypotenuse and o is the opposite side to the angle.

As a test my code is:

import math

a = 30
o = 40
h = math.sqrt(o*o+a*a)                  #  should = 50
ta = math.tan(o/a) * 180 / math.pi      #  toa
ca = math.cos(a/h) * 180 / math.pi      #  cah
sa = math.sin(o/h) * 180 / math.pi      #  soh
print(" : hypotenuse = ",h,"\n : tan angle = ",int(ta),"\n : cos angle = ",int(ca),"\n : sine angle = ",int(sa))

I would have expected the answers to all come back with the same angle, but they don't. These are the answers I get:

 : hypotenuse =  50.0 
 : tan angle =  236 
 : cos angle =  47 
 : sine angle =  41

Why are the answers so different and/or what am I doing wrong?


Solution

  • I noticed you weren't using inverse trig functions, I have updated the functions to inverses where you take the inverse of the ratio of the side:

    import math
    
    a = 30
    o = 40
    h = math.sqrt(o*o+a*a)                  #  should = 50
    ta = math.atan(o/a) * 180 / math.pi      #  math.tan --> math.atan
    ca = math.acos(a/h) * 180 / math.pi      #  math.cos --> math.acos
    sa = math.asin(o/h) * 180 / math.pi      #  math.sin --> math.asin
    print(" : hypotenuse = ",h,"\n : tan angle = ",int(ta),"\n : cos angle = ",int(ca),"\n : sine angle = ",int(sa))
    

    The input of a regular trig function is the angle which will then result in the ratio of the sides. Using the inverse trig functions you can get the angle from the ratio of the sides.