[英]python find x,y coordinates with radian, angle given
如果圆的弧度和角度以 (0,0) 为圆心,我试图找到多个 (x,y) 坐标。 比方说,弧度 = 4,角度 = 360/n
我需要多个坐标,这意味着如果我的 n = 4 那么我的角度就是 90 度。 所以我不仅需要 90 度的 x,y 坐标,还需要 90,180,270,360 度的坐标。
同样,如果我的 n= 6,那么我需要 360/6=60 处的 x,y 坐标。 所以每+60度直到360度。 示例 x,y 坐标为 60、120、180、240、300、360。
我只知道如何为一位天使做到这一点,这就是我所尝试的
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)
任何帮助,将不胜感激。 谢谢!
我将在没有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]
输出
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)]
我没有在此舍入这些,因为通常这只是您为格式化所做的事情,但如果您确实想要,只需
return [(round(radius*math.cos(a), 2), round(radius*math.sin(a), 2)) for a in angles]
这是你如何在 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])
您应该能够使用循环遍历 [1, n]。
例如:
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)
您可以遍历所有子角度,然后生成 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
我对你的问题的几分钱:
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
您可以尝试打印angle
以说服自己它提供您想要的。
要获得 360° 中的所有部分,您可以使用range(0, 360, 360//n)
的列表理解。 此外,您可以使用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)]
这对于对这些点进行进一步计算也可能很有用,例如添加它们。 如果您更喜欢(圆形)元组,则可以使用嵌套列表理解:
>>> [(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.