Reputation: 323
I'm trying to print out the x,y value for a line with a certain degree which intersects a circle with a specified radius.
Lets say for example that the line is pointing straight up at 90 degrees.
import math
degree = 90
radius = 10
x = radius * math.cos(degree)
y = radius * math.sin(degree)
print(x,y)
This prints out -4.480736161291701 8.939966636005579 but according to my calculator is supposed to print 0 10 on deg.
I have already tried adding math.radians and math.degrees before the degree var in the x = and y =, but it doesn't come out correctly any time I've tried. The link I found to the point where a line with degree intersects a circle is here, the sin/cos values are flipped 'tho for the x and y value in the solution.
Simply said, how would I make the 90 be in degrees instead of radians to get the correct x,y?
EDIT:
by adding math.radians:
x = radius * math.cos(math.radians(degree))
y = radius * math.sin(math.radians(degree))
it returned 6.123233995736766e-16 10.0
~~~
by adding math.degrees:
x = radius * math.cos(math.degrees(degree))
y = radius * math.sin(math.degrees(degree))
it returned -2.995153947555356 -9.540914674728182
Upvotes: 2
Views: 7902
Reputation: 1
If you're writing a somewhat longer code, you can make your code work in degrees by putting in the following lines in the beginning:
from math import sin, cos, tan, asin, acos, atan
from math import radians as rad, degrees as deg
def s(x):
return sin(rad(x))
def c(x):
return cos(rad(x))
def t(x):
return tan(rad(x))
def sa(x):
return deg(asin(x))
def ca(x):
return deg(acos(x))
def ta(x):
return deg(atan(x))
Then, throughout your code, instead of typing sin(90)
you would type s(90)
, or for arcsin(90)
you would type sa(90)
. (I couldn't make the code word as
for arcsine, since as
is already a Python word)
Now, you would just type up your code as though it were in degrees and everything should work out fine.
Upvotes: 0
Reputation: 25134
All angles in most of the math libraries are in radians. The input to math.cos
should be radians but you are passing in degrees.
import math
degree = 90
radius = 10
x = radius * math.cos(math.radians(degree))
y = radius * math.sin(math.radians(degree))
print x,y
>0, 10
math.radians
is nothing more than doing (pi * degree / 180 )
Upvotes: 1
Reputation: 22324
In Python, you can use math.radians
to convert from degrees to radians. Note that it is not just Python that defaults to radians, it is usually the standard in mathematics to talk about angles in radians.
Although, in general, you can always use the conversion formula
radians = pi * degrees / 180
Upvotes: 1
Reputation: 108
You can use math.radians(degree)
to convert to radians. All python's default trig functions work in radians. So your code becomes:
import math
degree = 90
radius = 10
x = radius * math.cos(math.radians(degree))
y = radius * math.sin(math.radians(degree))
print(x,y)
And this produces the correct result: 6.123233995736766e-16 10.0, with some odd floating point behavior you can fix with appropriate rounding.
Upvotes: 0