https://postimg.org/image/uzdalt4s1/
下面的脚本将给出一个通过正弦函数的点的坐标( URL图像中的图A)
与图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)
发布于 2016-06-28 16:01:50
这应该是可行的:
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)
https://stackoverflow.com/questions/38072524
复制