Reputation: 572
okay, i have a circle and i want get the point on circle in 90 degree of origin.
def point_on_circle():
'''
Finding the x,y coordinates on circle, based on given angle
'''
from math import cos, sin
#center of circle, angle in degree and radius of circle
center = [0,0]
angle = 90
radius = 100
#x = offsetX + radius * Cosine(Degree)
x = center[0] + (radius * cos(angle))
#y = offsetY + radius * Sine(Degree)
y = center[1] + (radius * sin(angle))
return x,y
>>> print point_on_circle()
[-44.8073616129 , 89.3996663601]
since pi start from 3 o'clock, i expected to get x=0
and y=100
but i have no idea why i'm getting that.
what am i doing wrong?
Edit: even i convert to radians, still i get weird result.
def point_on_circle():
'''
Finding the x,y coordinates on circle, based on given angle
'''
from math import cos, sin, radians
#center of circle, angle in degree and radius of circle
center = [0,0]
angle = radians(90)
radius = 100
#x = offsetX + radius * Cosine(radians)
x = center[0] + (radius * cos(angle))
#y = offsetY + radius * Sine(radians)
y = center[1] + (radius * sin(angle))
return x,y
>>> print point_on_circle()
[6.12323399574e-15 , 100.0]
any idea how to get accurate number?
Upvotes: 1
Views: 7568
Reputation: 11
Two things need to be changed.
range = 90
to range = radians(90)
which means you need to import radians.range = 90
to range = radians(360 - 90)
and have imported radians.Then if you want to stop your answer from having floating-point, you have return int(x), int(y)
instead of return x,y
at the end of your function. I made these changes and it worked.
Upvotes: 1
Reputation: 4771
math.cos
and math.sin
expect radians, not degrees. Simply replace 90
by pi/2
:
def point_on_circle():
'''
Finding the x,y coordinates on circle, based on given angle
'''
from math import cos, sin, pi
#center of circle, angle in degree and radius of circle
center = [0,0]
angle = pi / 2
radius = 100
x = center[0] + (radius * cos(angle))
y = center[1] + (radius * sin(angle))
return x,y
You'll get (6.123233995736766e-15, 100.0)
which is close to (0, 100)
.
If you want better precision, you can try SymPy online before installing it yourself:
>>> from sympy import pi, mpmath
>>> mpmath.cos(pi/2)
6.12323399573677e−17
We're getting closer, but this is still using floating-point. However, mpmath.cospi gets you the correct result:
>>> mpmath.cospi(1/2)
0.0
Upvotes: 8
Reputation: 34
The sin() & cos() expect radians use:
x = center[0] + (radius * cos(angle*pi/180));
Upvotes: 1