简体   繁体   中英

python find x,y coordinates with radian, angle given

I am trying to find multiple (x,y) co-ordinates if radian and angle of a circle is given with (0,0) being the center of the circle. Lets say, radian = 4, angle = 360/n

I need multiple coordinates meaning if my n = 4 then my angle would be 90 degrees. So I need x,y coordinates not just at 90 degrees but also at 90,180,270,360 degrees.

Similarly, if my n= 6 then I need x,y coordinates at 360/6=60. So at every +60 degrees till 360 degrees. Example x,y coordinates at 60, 120, 180, 240, 300, 360.

I only know how to do it for one angel and this is what I tried

import math

number = 4
Angles_req = (360/number)
radius = 4

x = round(4*math.cos(360/number), 2)
y = round(4*math.sin(360/number), 2)

Any help would be appreciated. Thank you!

I'm going to do this without numpy , although it would be easier

import math

def circle_sections(divisions, radius=1):
    # the difference between angles in radians -- don't bother with degrees
    angle = 2 * math.pi / divisions

    # a list of all angles using a list comprehension
    angles = [i*angle for i in range(divisions)]

    # finally return the coordinates on the circle as a list of 2-tuples
    return [(radius*math.cos(a), radius*math.sin(a)) for a in angles]

Output

circle_sections(4)

#[(1.0, 0.0),
# (6.123233995736766e-17, 1.0),
# (-1.0, 1.2246467991473532e-16),
# (-1.8369701987210297e-16, -1.0)]

circle_sections(6)

#[(1.0, 0.0),
# (0.5000000000000001, 0.8660254037844386),
# (-0.4999999999999998, 0.8660254037844387),
# (-1.0, 1.2246467991473532e-16),
# (-0.5000000000000004, -0.8660254037844384),
# (0.4999999999999993, -0.866025403784439)]

I didn't round these here because normally that's only something you do for formatting but if you do want to, just

return [(round(radius*math.cos(a), 2), round(radius*math.sin(a), 2)) for a in angles]

And here is how you do it in numpy:

import numpy as np 
import math

radius = 4
number = 4
rad = np.radians(np.linspace(360/number,360,number))
xy = radius *np.array([[math.cos(x),math.sin(x)] for x in rad])

You should be able to use a loop to iterate through [1, n].

For example:

import math

n = 4
r = 4

for i in range(1, n + 1):
    theta = math.radians((360 / n) * i)
    x = round(r * math.cos(theta), 2)
    y = round(r * math.sin(theta), 2)

You can iterate over all sub angles and then yield tuples of the x,y pairs.

def divide_angle(num_units, angle=360):
    for unit in range(num_unit):
        sub_angle = (unit+1)*angle//unit
        x = round(4*math.cos(sub_angle), 2)
        y = round(4*math.sin(sub_angle), 2)
        yield x,y

My few cents to your problem:

n = 6
step = int(360/n)
for i in range(step, 361, step):
    angle = math.radians(i)
    x = round(4*math.cos(angle), 2)
    y = round(4*math.sin(angle), 2)
    # Do something with x and y

You can try printing angle to convince yourself that it gives what you want.

To get all the sections in 360° you can use a list comprehension over range(0, 360, 360//n) . Also, instead of using sin and cos , you could use cmath to get complex numbers in polar coordinates.

>>> radius, n = 4, 3

>>> [cmath.rect(radius, math.radians(a)) for a in range(0, 360, 360//n)]
[(4+0j),
 (-1.9999999999999991+3.464101615137755j),
 (-2.0000000000000018-3.4641016151377535j)]

This might also be useful for doing further calculations on those points, eg adding them. If you prefer (rounded) tuples, you can use a nested list comprehension:

>>> [(round(c.real, 2), round(c.imag, 2))
...  for c in (cmath.rect(radius, math.radians(a)) for a in range(0, 360, 360//n))]
[(4.0, 0.0), (-2.0, 3.46), (-2.0, -3.46)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM