"Point on Circle" 基于给定的角度
"Point on Circle" base on given angle
好的,我有一个圆,我想在原点 90 度的圆上得到点。
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]
因为 pi 从 3 点钟开始,我希望得到 x=0
和 y=100
但我不知道为什么我会得到那个。
我做错了什么?
编辑:即使我转换为弧度,我仍然得到奇怪的结果。
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]
知道如何获得准确的数字吗?
math.cos
和 math.sin
需要弧度,而不是度数。只需将 90
替换为 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
您将得到 (6.123233995736766e-15, 100.0)
,接近 (0, 100)
。
如果你想要更好的精度,你可以try SymPy online在自己安装之前:
>>> from sympy import pi, mpmath
>>> mpmath.cos(pi/2)
6.12323399573677e−17
我们越来越接近了,但这仍然使用浮点数。然而,mpmath.cospi 得到正确的结果:
>>> mpmath.cospi(1/2)
0.0
sin() 和 cos() 期望弧度使用:
x = center[0] + (radius * cos(angle*pi/180));
有两点需要更改。
- 首先,您需要将
range = 90
更改为range = radians(90)
,这意味着您需要导入弧度。
- 其次,您需要从弧度 (360) 中减去范围,这样您就可以从象限 I 而不是象限 IV 开始。完成后,您应该将
range = 90
更改为 range = radians(360 - 90)
并导入弧度。
然后如果你想阻止你的答案有浮点数,你在你的函数末尾有 return int(x), int(y)
而不是 return x,y
。我进行了这些更改并且有效。
好的,我有一个圆,我想在原点 90 度的圆上得到点。
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]
因为 pi 从 3 点钟开始,我希望得到 x=0
和 y=100
但我不知道为什么我会得到那个。
我做错了什么?
编辑:即使我转换为弧度,我仍然得到奇怪的结果。
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]
知道如何获得准确的数字吗?
math.cos
和 math.sin
需要弧度,而不是度数。只需将 90
替换为 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
您将得到 (6.123233995736766e-15, 100.0)
,接近 (0, 100)
。
如果你想要更好的精度,你可以try SymPy online在自己安装之前:
>>> from sympy import pi, mpmath
>>> mpmath.cos(pi/2)
6.12323399573677e−17
我们越来越接近了,但这仍然使用浮点数。然而,mpmath.cospi 得到正确的结果:
>>> mpmath.cospi(1/2)
0.0
sin() 和 cos() 期望弧度使用:
x = center[0] + (radius * cos(angle*pi/180));
有两点需要更改。
- 首先,您需要将
range = 90
更改为range = radians(90)
,这意味着您需要导入弧度。 - 其次,您需要从弧度 (360) 中减去范围,这样您就可以从象限 I 而不是象限 IV 开始。完成后,您应该将
range = 90
更改为range = radians(360 - 90)
并导入弧度。
然后如果你想阻止你的答案有浮点数,你在你的函数末尾有 return int(x), int(y)
而不是 return x,y
。我进行了这些更改并且有效。