I'm trying to create a program that calculates the adjacent and opposite of a triangle using the hypotenuse and the angle between the hypotenuse and the adjacent with python
Asked
Active
Viewed 640 times
-2
-
Don't repost questions, just because you didn't get the answer the first time. – gre_gor Dec 04 '21 at 03:38
-
And reposting a bad question (this one seem to be about math, not programming) just leads faster to [a question ban](https://stackoverflow.com/help/question-bans). – gre_gor Dec 04 '21 at 03:41
2 Answers
0
Just remember SohCahToa. We have the hypotenuse (h) so for the opposite, we set up the equation sin(angle) = opposite/hypotenuse. To find the opposite we multiply both sides by the hypotenuse to get hypotenuse * sin(angle) = opposite. The do a similar thing with cos(angle) = adjacent / hypotenuse. Then you have your opposite and adjacent sides. In terms of code it would look something like this:
import math # we need this specifically to use trig functions
angle = (some angle here)
hypotenuse = (some number here)
opposite = hypotenuse * math.sin(math.radians(angle))
# we have to multiply the angle by PI/180 because the python sin function uses radians and we do this by using math.radians() is how to convert from degrees to radians.
adjacent = hypotenuse * math.cos(angle * (math.pi/180))

Jerry Spice
- 63
- 1
- 8
-
FYI, you can use [`math.radians`](https://docs.python.org/3/library/math.html#math.radians) instead of manually converting between degrees and radians. – Brian61354270 Dec 04 '21 at 02:45
-
Really? I had no idea of that, I've just been using the same thing my trig teacher taught me, thanks for the tip! – Jerry Spice Dec 04 '21 at 02:47
0
the best way I found was to do:
import math
hype = 10
angle = 30
adj = 0
opp = 0
adj = hype*math.cos(math.radians(angle))
opp = hype*math.sin(math.radians(angle))
opp
print(str(opp) + ' is the opposite side value')
print(str(adj) + ' is the adjacent side value')
print(str(hype) + ' is the hypotenuse')
print(str(angle) + ' is the angle')
the math package in python allows you to work with sine and cosine, which you will need when doing trig like this.