“Point on Circle” base on given angle

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=100but 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?

Answer

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

Leave a Reply

Your email address will not be published. Required fields are marked *