如何旋转一个函数并获取Python中的坐标?
问题描述:
https://postimg.org/image/uzdalt4s1/如何旋转一个函数并获取Python中的坐标?
下面的脚本会给通过正弦函数(图A中的URL图像)去点
类似于图A的坐标,我怎么旋转的函数的坐标? (图B)
from time import sleep
import math
x = 100
y = 500
f = 0
while 1:
print('X: '+str(x))
print('Y: '+str(math.sin(f)*100+y))
f += math.pi/50
x += 1
sleep(0.01)
答
这应该工作:
from time import sleep
import math
def get_rotated_coordinates(x, y, fi, angle = 'deg'):
''' function rotates coordinates x and y for angle fi, variable
angle tells if angle fi is in degrees or radians, default value
is 'deg' for degrees, but you can also use 'rad' for radians'''
if angle == 'deg':
fi = math.radians(fi)
elif angle != 'rad':
raise ValueError('{} is unsuported type for angle.\nYou can use "deg" for degrees and "rad" for radians.'.format(angle))
k = math.tan(fi)
denominator = math.sqrt(k**2 + 1)
x1 = x/denominator
y1 = k * x1
x2 = -(y * k/denominator) + x1
y2 = (x1 - x2)/k + y1
return x2, y2
x = 100
y = 500
f = 0
while 1:
y = math.sin(f)*100+y
x2, y2 = get_rotated_coordinates(x, y, 30)
print('X: '+str(x2))
print('Y: '+str(y2))
f += math.pi/50
x += 1
sleep(0.01)
您可以通过一个旋转矩阵相乘。 – syntonym