"指向圈子"基于给定的角度

时间:2016-02-15 06:13:33

标签: python python-2.7 trigonometry

好吧,我有一个圆圈,我希望在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=0y=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]

任何想法如何获得准确数字?

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:1)

罪恶()& cos()期望弧度使用:

x = center[0] + (radius * cos(angle*pi/180));

答案 2 :(得分:1)

有两件事需要改变。

  • 首先,您需要将 range = 90 更改为 range = radians(90),这意味着您需要导入弧度。
  • 其次,您需要从弧度 (360) 中减去范围,以便从象限 I 而不是象限 IV 开始计算。完成后,您应该将 range = 90 更改为 range = radians(360 - 90) 并导入弧度。

然后,如果您想阻止您的答案具有浮点数,您可以在函数末尾使用 return int(x), int(y) 而不是 return x,y。我进行了这些更改,并且奏效了。