Python degrees defaulting to radians - python

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

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

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.

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 )

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
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.

Related

How to calculate sinus (or any other trigonometric function) in degrees python math module? [duplicate]

In the math module, I could only find math.cos(x), with cos/sin/tan/acos/asin/atan. This returns the answer in radians. How can I get the answer in degrees?
Here's my code:
import math
x = math.cos(1)
y = x * 180 / math.pi
print(y)
30.9570417874
My calculator, on deg, gives me:
cos(1)
0.9998476...
Python includes two functions in the math package; radians converts degrees to radians, and degrees converts radians to degrees.
To match the output of your calculator you need:
>>> math.cos(math.radians(1))
0.9998476951563913
Note that 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.
Python convert radians to degrees or degrees to radians:
What are Radians and what problem does it solve?:
Radians and degrees are two separate units of measure that help people express and communicate precise changes in direction. Wikipedia has some great intuition with their infographics on how one Radian is defined relative to degrees:
https://en.wikipedia.org/wiki/Radian
Python examples using libraries calculating degrees from radians:
>>> import math
>>> math.degrees(0) #0 radians == 0 degrees
0.0
>>> math.degrees(math.pi/2) #pi/2 radians is 90 degrees
90.0
>>> math.degrees(math.pi) #pi radians is 180 degrees
180.0
>>> math.degrees(math.pi+(math.pi/2)) #pi+pi/2 radians is 270 degrees
270.0
>>> math.degrees(math.pi+math.pi) #2*pi radians is 360 degrees
360.0
Python examples using libraries calculating radians from degrees:
>>> import math
>>> math.radians(0) #0 degrees == 0 radians
0.0
>>> math.radians(90) #90 degrees is pi/2 radians
1.5707963267948966
>>> math.radians(180) #180 degrees is pi radians
3.141592653589793
>>> math.radians(270) #270 degrees is pi+(pi/2) radians
4.71238898038469
>>> math.radians(360) #360 degrees is 2*pi radians
6.283185307179586
Source: https://docs.python.org/3/library/math.html#angular-conversion
The mathematical notation:
You can do degree/radian conversion without python libraries:
If you roll your own degree/radian converter, you have to write your own code to handle edge cases.
Mistakes here are easy to make, and will hurt just like it hurt the developers of the 1999 mars orbiter, who sunk $125m dollars crashing it into Mars because of non intuitive edge case here.
>>> 0 * 180.0 / math.pi #0 radians is 0 degrees
0.0
>>> (math.pi/2) * 180.0 / math.pi #pi/2 radians is 90 degrees
90.0
>>> (math.pi) * 180.0 / math.pi #pi radians is 180 degrees
180.0
>>> (math.pi+(math.pi/2)) * 180.0 / math.pi #pi+(pi/2) radians is 270 degrees
270.0
>>> (2 * math.pi) * 180.0 / math.pi #2*pi radians is 360 degrees
360.0
Degrees to radians:
>>> 0 * math.pi / 180.0 #0 degrees in radians
0.0
>>> 90 * math.pi / 180.0 #90 degrees in radians
1.5707963267948966
>>> 180 * math.pi / 180.0 #180 degrees in radians
3.141592653589793
>>> 270 * math.pi / 180.0 #270 degrees in radians
4.71238898038469
>>> 360 * math.pi / 180.0 #360 degrees in radians
6.283185307179586
Expressing multiple rotations with degrees and radians
Single rotation valid radian values are between 0 and 2*pi. Single rotation degree values are between 0 and 360. However if you want to express multiple rotations, valid radian and degree values are between 0 and infinity.
>>> import math
>>> math.radians(360) #one complete rotation
6.283185307179586
>>> math.radians(360+360) #two rotations
12.566370614359172
>>> math.degrees(12.566370614359172) #math.degrees and math.radians preserve the
720.0 #number of rotations
Collapsing multiple rotations:
You can collapse multiple degree/radian rotations into a single rotation by modding against the value of one rotation. For degrees you mod by 360, for radians you modulus by 2*pi.
>>> import math
>>> math.radians(720+90) #2 whole rotations plus 90 is 14.14 radians
14.137166941154069
>>> math.radians((720+90)%360) #14.1 radians brings you to
1.5707963267948966 #the end point as 1.57 radians.
>>> math.degrees((2*math.pi)+(math.pi/2)) #one rotation plus a quarter
450.0 #rotation is 450 degrees.
>>> math.degrees(((2*math.pi)+(math.pi/2))%(2*math.pi)) #one rotation plus a quarter
90.0 #rotation brings you to 90.
Fundamental education on Radians and Degrees
5 minute refresher using Trigonometry and expression of rotation to convert radians to degrees and back: https://youtu.be/ovLbCvq7FNA?t=31
Khan academy refresher on trigonometry, unit circle, angular mathematics to use sin,cos,tan to describe rotation and changes in rotation. https://www.khanacademy.org/math/algebra2/x2ec2f6f830c9fb89:trig/x2ec2f6f830c9fb89:unit-circle/v/unit-circle-definition-of-trig-functions-1
You can simply convert your radian result to degree by using
math.degrees and rounding appropriately to the required decimal places
for example
>>> round(math.degrees(math.asin(0.5)),2)
30.0
>>>
radian can also be converted to degree by using numpy
print(np.rad2deg(1))
57.29577951308232
if needed to roundoff ( I did with 6 digits after decimal below), then
print(np.round(np.rad2deg(1), 6)
57.29578
I also like to define my own functions that take and return arguments in degrees rather than radians. I am sure there some capitalization purest who don't like my names, but I just use a capital first letter for my custom functions. The definitions and testing code are below.
#Definitions for trig functions using degrees.
def Cos(a):
return cos(radians(a))
def Sin(a):
return sin(radians(a))
def Tan(a):
return tan(radians(a))
def ArcTan(a):
return degrees(arctan(a))
def ArcSin(a):
return degrees(arcsin(a))
def ArcCos(a):
return degrees(arccos(a))
#Testing Code
print(Cos(90))
print(Sin(90))
print(Tan(45))
print(ArcTan(1))
print(ArcSin(1))
print(ArcCos(0))
Note that I have imported math (or numpy) into the namespace with
from math import *
Also note, that my functions are in the namespace in which they were defined. For instance,
math.Cos(45)
does not exist.
-fix-
because you want to change from radians to degrees, it is actually
rad=deg * math.pi /180
and not deg*180/math.pi
import math
x=1 # in deg
x = x*math.pi/180 # convert to rad
y = math.cos(x) # calculate in rad
print y
in 1 line it can be like this
y=math.cos(1*math.pi/180)
I like this method,use sind(x) or cosd(x)
import math
def sind(x):
return math.sin(math.radians(x))
def cosd(x):
return math.cos(math.radians(x))

Trigonometry: sin(x) getting negative value

I am trying to solve a homework: I am required to write a program which will calculate the length of a ladder based on two inputs, that is the desired height to be reached and the angle created by leaning the ladder toward the wall.
I used the following formula to convert degrees to radians :
radians = (math.pi / 180) * x # x is the given angle by the user.
I imported the math library as well to use its functions.
def main():
import math
print("this program calculates the length of a ladder after you give the height and the angle")
h = eval(input("enter the height you want to reach using the ladder"))
x = eval(input("enter the angle which will be created be leaning the ladder to the wall"))
radians = ( math.pi / 180 ) * x
length = h / math.sin(x)
print("the length is:", length)
main()
What exactly am I doing wrong?
I know the code is missing something and would appreciate it if someone could help me fill the gap.
You never used radians after you calculate it.
i.e. length = h / math.sin(radians)
To make crickt_007's right answer absolutely clear: radians which you did not use after you calculate it should be the argument of the sine:
length = h / math.sin(radians)
you calculate radians,thats ok,but problem is you never used that radians value. i think your code must be changed as follows :)
def main():
import math
print("this program calculates the length of a ladder after you give the height and the angle")
h = eval(input("enter the height you want to reach using the ladder"))
x = eval(input("enter the angle which will be created be leaning the ladder to the wall"))
radians = ( math.pi / 180 ) * x
length = h / math.sin(radians)
print("the length is:", length)
main()
if your both input will be 5,output is the length is: 57.36856622834928

Convert X, Y Rotation coordinates from Radians to Degrees

I have 2 values that are in radians, c[1] and c[3]. I need to turn the radians into degrees and I haven't the faintest idea what to do to these numbers to get degrees out of them. I have been searching the internet far and wide and I cant find anything that I can actually understand. I have tried devising my own way to do it but I'm sure I'm not even close. I have tried the following:
z = (((c[1] * 180) + 180) + ((c[3] * 180) + 180))
z = (((c[1] * math.pi) / 180) + ((c[3] * math.pi) / 180) / 2)
z = (c[1] * (90/math.pi) - (c[3] * (90/math.pi)))
z = math.atan2(c[3], c[1])
z = (math.degrees(c[1]) + math.degrees(c[3])) * 2
z = c[1]
z = (math.asin(c[3]) / math.acos(c[1]))
How do I get a value in degrees from 2 radians?
After going through your comment, I don't think that you are getting two angles in radians for c[1] and c[3]. Rather, you are getting direction cosines. If you were getting angles in radians, the value would range from -pi to pi. Rather, the value goes from -1 to 1 (i.e. cos(-pi) to cos(pi)).
You can change the value first to an angle in radians and then to degrees if that is what you want. Just as a caveat, the cosine of angles is symmetric ...
So for:
In [12]: zip(angles, (cos(angles)))
Out[12]:
[(-3.1415926535897931, -1.0),
(-2.8108986900540254, -0.94581724170063464),
(-2.4802047265182576, -0.78914050939639346),
(-2.1495107629824899, -0.5469481581224267),
(-1.8188167994467224, -0.24548548714079912),
(-1.4881228359109546, 0.082579345472332394),
(-1.1574288723751871, 0.40169542465296937),
(-0.82673490883941936, 0.67728157162574099),
(-0.49604094530365161, 0.87947375120648907),
(-0.16534698176788387, 0.98636130340272232),
(0.16534698176788387, 0.98636130340272232),
(0.49604094530365161, 0.87947375120648907),
(0.82673490883941891, 0.67728157162574132),
(1.1574288723751867, 0.40169542465296976),
(1.4881228359109544, 0.082579345472332616),
(1.8188167994467221, -0.2454854871407989),
(2.1495107629824899, -0.5469481581224267),
(2.4802047265182576, -0.78914050939639346),
(2.8108986900540254, -0.94581724170063464),
(3.1415926535897931, -1.0)]
But,
In [11]: zip(angles, arccos(cos(angles)))
Out[11]:
[(-3.1415926535897931, 3.1415926535897931),
(-2.8108986900540254, 2.8108986900540254),
(-2.4802047265182576, 2.4802047265182576),
(-2.1495107629824899, 2.1495107629824899),
(-1.8188167994467224, 1.8188167994467224),
(-1.4881228359109546, 1.4881228359109546),
(-1.1574288723751871, 1.1574288723751871),
(-0.82673490883941936, 0.82673490883941936),
(-0.49604094530365161, 0.49604094530365156),
(-0.16534698176788387, 0.16534698176788418),
(0.16534698176788387, 0.16534698176788418),
(0.49604094530365161, 0.49604094530365156),
(0.82673490883941891, 0.82673490883941891),
(1.1574288723751867, 1.1574288723751867),
(1.4881228359109544, 1.4881228359109544),
(1.8188167994467221, 1.8188167994467221),
(2.1495107629824899, 2.1495107629824899),
(2.4802047265182576, 2.4802047265182576),
(2.8108986900540254, 2.8108986900540254),
(3.1415926535897931, 3.1415926535897931)]
Which means that getting your angles from your direction cosines, you will need to do:
In [13]: def toAng(a): return sign(a)*arccos(a)
which will give you your correct angles:
In [19]: zip(angles, toAng(cos(angles)))
Out[19]:
[(-3.1415926535897931, -3.1415926535897931),
(-2.8108986900540254, -2.8108986900540254),
(-2.4802047265182576, -2.4802047265182576),
(-2.1495107629824899, -2.1495107629824899),
(-1.8188167994467224, -1.8188167994467224),
(-1.4881228359109546, 1.4881228359109546),
(-1.1574288723751871, 1.1574288723751871),
(-0.82673490883941936, 0.82673490883941936),
(-0.49604094530365161, 0.49604094530365156),
(-0.16534698176788387, 0.16534698176788418),
(0.16534698176788387, 0.16534698176788418),
(0.49604094530365161, 0.49604094530365156),
(0.82673490883941891, 0.82673490883941891),
(1.1574288723751867, 1.1574288723751867),
(1.4881228359109544, 1.4881228359109544),
(1.8188167994467221, -1.8188167994467221),
(2.1495107629824899, -2.1495107629824899),
(2.4802047265182576, -2.4802047265182576),
(2.8108986900540254, -2.8108986900540254),
(3.1415926535897931, -3.1415926535897931)]
Finally, if you need to convert it to degrees, you can just do:
In [20]: def toAng(a): return 180*sign(a)*arccos(a)/pi
In [21]: zip(angles, toAng(cos(angles)))
Out[21]:
[(-3.1415926535897931, -180.0),
(-2.8108986900540254, -161.05263157894737),
(-2.4802047265182576, -142.10526315789474),
(-2.1495107629824899, -123.1578947368421),
(-1.8188167994467224, -104.21052631578948),
(-1.4881228359109546, 85.263157894736835),
(-1.1574288723751871, 66.31578947368422),
(-0.82673490883941936, 47.368421052631582),
(-0.49604094530365161, 28.421052631578949),
(-0.16534698176788387, 9.4736842105263346),
(0.16534698176788387, 9.4736842105263346),
(0.49604094530365161, 28.421052631578949),
(0.82673490883941891, 47.368421052631554),
(1.1574288723751867, 66.315789473684191),
(1.4881228359109544, 85.263157894736835),
(1.8188167994467221, -104.21052631578947),
(2.1495107629824899, -123.1578947368421),
(2.4802047265182576, -142.10526315789474),
(2.8108986900540254, -161.05263157894737),
(3.1415926535897931, -180.0)]
Which gives you the right angles in degrees ...
Note I am using an environment where sign, pi etc are numpy objects. In your program, you might have ti import them separately.
degree to radian conversions are done with the equation (n deg)*(pi/180 deg).
z = (c[1]*(math.pi/180.0) + (c[1]*(math.pi/180)
if it's something you need to do regularly make a function.
def DegtoRad(deg):
return (deg)*(math.pi/180)
or as a lambda
DegtoRad = lambda x: x*(math.pi/180)
remember though if you havent imported math/math.pi none of this will work.
probably better to define pi with an actual literal variable up to your needs of precision.
This was not so very hard to find online? - was it?
http://www.mathwarehouse.com/trigonometry/radians/convert-degee-to-radians.php
The formula is the opposite of #user2913685 's formula: num*180/pi
(an easy mistake)
here is an example in python:
pi = 3.14159265
rad_val = 7
deg_val = rad_val*180/pi
print(deg_val)
which gives the output:
401.07045704986604
This uses pi as 3.14159265, and doesn't use the module math. Obviously you can do the same as in the other answer, but after changing the formula.

Python: find a x,y coordinate for a given point B using the distance from the point A (x0, y0) and the angle

Is there a function already implemented in Python to find the X, Y of a point B from a point A (x0, Y0) using a given angle expressed in degrees (°)?
from math import cos, sin
def point_position(x0, y0, dist, theta):
return dist*sin(theta), dist*cos(theta)
where x0 = x coordinate of point A, y0 = y coordinate of point A, dist = distance between A and B, theta = angle (°) of the point B respect the North (0°) measured by a compass
You just need a function that converts degrees to radians. Then your function simply becomes:
from math import sin, cos, radians, pi
def point_pos(x0, y0, d, theta):
theta_rad = pi/2 - radians(theta)
return x0 + d*cos(theta_rad), y0 + d*sin(theta_rad)
(as you can see you mixed up sine and cosine in your original function)
(also note the linear angle transformation because angles on compass are clockwise and mathematical angles are counter-clockwise. Also there is an offset between the respective zeroes)
You can also use complex numbers to represent points, which is somewhat nicer than a tuple of coordinates (although a dedicated Point class would be even more appropriate):
import cmath
def point_pos(p, d, theta):
return p + cmath.rect(d, pi/2-radians(theta))

homework help? for making a spirograph

SO over break week our teacher gave us a little project to do requiring a Spirograph, here is the code he helped us write before
from graphics import *
from math import *
def ar(a):
return a*3.141592654/180
def main():
x0 = 100
y0 = 100
startangle = 60
stepangle = 120
radius = 50
win = GraphWin()
p1 = Point(x0 + radius * cos(ar(startangle)), y0 + radius * sin(ar(startangle)))
for i in range(stepangle+startangle,360+stepangle+startangle,stepangle):
p2 = Point(x0 + radius * cos(ar(i)), y0 + radius * sin(ar(i)))
Line(p1,p2).draw(win)
p1 = p2
input("<ENTER> to quit...")
win.close()
main()
he then wants us to develop the program that consecutively draws 12 equilateral triangles (rotating the triangle each time by 30 degrees through a full 360 circle). This can be achieved by “stepping” the STARTANGLE parameter. My question I am stuck on where to go from here, what does he mean by "stepping?" I assume making some sort of loop, is it possible someone can give me a push in the right step?
This is a solution using matplotlib. The general procedure would be the same. But you will have to modify it for using the libraries you're allowed to use.
from math import radians, sin, cos
import matplotlib.pyplot as plt
startAngle = 0
stepAngle = 30
origin = (0,0)
points = []
points.append(origin)
points.append((cos(radians(startAngle)), sin(radians(startAngle))))
for i in range(startAngle + stepAngle, 360 + stepAngle, stepAngle):
x = cos(radians(i))
y = sin(radians(i))
points.append((x,y))
points.append(origin)
points.append((x,y))
x,y = zip(*points) #separate the tupples into x and y coordinates.
plt.plot(x,y) #plots the points, drawing lines between each point
plt.show()
plt.plot draw lines between each point in the list. We're adding inn the origin points so we get triangles instead of just a polygon around the center.

Categories