9

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?

user1118321
  • 25,567
  • 4
  • 55
  • 86
Shannon Hochkins
  • 11,763
  • 15
  • 62
  • 95
  • This gives the angle between A and C. Look into the law of cosines. – Teepeemm Sep 03 '13 at 02:34
  • If I were to use `tan` I get closer... `x = math.tan(7/7)` and then `math.degrees(x)` returns `89.2328896038`? why isn't this an even 90? – Shannon Hochkins Sep 03 '13 at 02:37
  • A single trigonometric function will only work if you already have a right angle in the triangle, and are trying to find a different angle. `math.tan` takes radians and gives a ratio. `math.degrees` takes radians and gives degrees. They giving you almost 90 is coincidental. – Teepeemm Sep 03 '13 at 03:26
  • all of the trig functions convert between an angle and the ratio of two sides of a triangle. cos, sin, and tan take an angle in radians as input and return the ratio; acos, asin, and atan take a ratio as input and return an angle in radians. You only convert the angles, never the ratios. – Neo Ravi Apr 14 '18 at 11:56
  • As @Teepeemm explains, the ratio leg1/hypotenuse gives you the cosine of the angle adjacent to leg1 that is NOT the right angle. In this case, because the triangle is isosceles, 45 degrees. – InverniE Mar 10 '19 at 13:31

5 Answers5

23

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
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
3

You can use this also.

print(str(int(round(math.degrees(math.atan2(x,y)))))+'°')

This accepts two inputs as two heights of the triangle and you can get the output angle in proper degree format.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
himanshu
  • 41
  • 2
0

i think you're looking for math.acos not math.cos, you want to return the angle whose value is the ratio of those two sides. not take its cosine.

yoshi
  • 403
  • 1
  • 6
  • 11
0

Trig functions will convert an angle into a length of a certain leg of a certain triangle. In particular, the tangent is the ratio of the opposite side to the adjacent side. math.tan(7/7) is the length of the right triangle opposite an angle of 1(=7/7) radian. This length (~1.557) just happens to be near to the number of radians which is 90 degrees (pi/2 ~ 1.571).

As noted, you're looking for an inverse trig function to convert a length back into an angle.

colcarroll
  • 3,632
  • 17
  • 25
0

use this:

import math
AB = float(input())
BC = float(input())

print(str(int(round(math.degrees(math.atan2(AB, BC)))))+'°')
HasanShovon
  • 59
  • 1
  • 1
  • 5
  • 1
    This code assumes that OP's marked angle is 90degrees, and then gives the acute angle next to input "BC". – Teepeemm Mar 10 '19 at 22:17