[英]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.如果圆的弧度和角度以 (0,0) 为圆心,我试图找到多个 (x,y) 坐标。 Lets say, radian = 4, angle = 360/n
比方说,弧度 = 4,角度 = 360/n
I need multiple coordinates meaning if my n = 4 then my angle would be 90 degrees.我需要多个坐标,这意味着如果我的 n = 4 那么我的角度就是 90 度。 So I need x,y coordinates not just at 90 degrees but also at 90,180,270,360 degrees.
所以我不仅需要 90 度的 x,y 坐标,还需要 90,180,270,360 度的坐标。
Similarly, if my n= 6 then I need x,y coordinates at 360/6=60.同样,如果我的 n= 6,那么我需要 360/6=60 处的 x,y 坐标。 So at every +60 degrees till 360 degrees.
所以每+60度直到360度。 Example x,y coordinates at 60, 120, 180, 240, 300, 360.
示例 x,y 坐标为 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我将在没有numpy 的情况下执行此操作,尽管它会更容易
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:这是你如何在 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].您应该能够使用循环遍历 [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.您可以遍历所有子角度,然后生成 x,y 对的元组。
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.您可以尝试打印
angle
以说服自己它提供您想要的。
To get all the sections in 360° you can use a list comprehension over range(0, 360, 360//n)
.要获得 360° 中的所有部分,您可以使用
range(0, 360, 360//n)
的列表理解。 Also, instead of using sin
and cos
, you could use cmath
to get complex numbers in polar coordinates.此外,您可以使用
cmath
来获取极坐标中的复数,而不是使用sin
和cos
。
>>> 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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.