use*_*835 27 python math degrees
如果你有一个点(在2d),你怎么能在python的另一个点(原点)周围旋转那个点?
例如,您可以将原点周围的第一个点倾斜10度.
基本上你有一个点PointA和它旋转的原点.代码看起来像这样:
PointA=(200,300)
origin=(100,100)
NewPointA=rotate(origin,PointA,10) #The rotate function rotates it by 10 degrees
Run Code Online (Sandbox Code Playgroud)
Mar*_*son 51
以下rotate函数以笛卡尔平面周围point的角度angle(逆时针,以弧度表示)旋转点origin,使用通常的轴约定:x从左向右增加,y从垂直向上增加.所有点都表示为表单的长度为2的元组(x_coord, y_coord).
import math
def rotate(origin, point, angle):
"""
Rotate a point counterclockwise by a given angle around a given origin.
The angle should be given in radians.
"""
ox, oy = origin
px, py = point
qx = ox + math.cos(angle) * (px - ox) - math.sin(angle) * (py - oy)
qy = oy + math.sin(angle) * (px - ox) + math.cos(angle) * (py - oy)
return qx, qy
Run Code Online (Sandbox Code Playgroud)
如果角度以度为单位指定,则可以先将其转换为弧度math.radians.对于顺时针旋转,否定角度.
示例:(3, 4)围绕原点(2, 2)逆时针旋转10度角:
>>> point = (3, 4)
>>> origin = (2, 2)
>>> rotate(origin, point, math.radians(10))
(2.6375113976783475, 4.143263683691346)
Run Code Online (Sandbox Code Playgroud)
请注意,rotate函数中有一些明显的重复计算:math.cos(angle)并且math.sin(angle)每个都计算两次,因为px - ox和py - oy.如果有必要,我留给你考虑一下.
Imp*_*est 23
将一个点绕另一点旋转一定度数的一个选项是使用numpy代替math。这允许轻松地将函数泛化为将任意数量的点作为输入,这在例如旋转多边形时可能很有用。
import numpy as np
def rotate(p, origin=(0, 0), degrees=0):
angle = np.deg2rad(degrees)
R = np.array([[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]])
o = np.atleast_2d(origin)
p = np.atleast_2d(p)
return np.squeeze((R @ (p.T-o.T) + o.T).T)
points=[(200, 300), (100, 300)]
origin=(100,100)
new_points = rotate(points, origin=origin, degrees=10)
print(new_points)
Run Code Online (Sandbox Code Playgroud)
import math
def rotate(x,y,xo,yo,theta): #rotate x,y around xo,yo by theta (rad)
xr=math.cos(theta)*(x-xo)-math.sin(theta)*(y-yo) + xo
yr=math.sin(theta)*(x-xo)+math.cos(theta)*(y-yo) + yo
return [xr,yr]
Run Code Online (Sandbox Code Playgroud)
小智 5
经过大量代码和存储库之后。这个功能对我来说效果最好。它也很有效,因为它只计算一次正弦和余弦值。
import numpy as np
def rotate(point, origin, degrees):
radians = np.deg2rad(degrees)
x,y = point
offset_x, offset_y = origin
adjusted_x = (x - offset_x)
adjusted_y = (y - offset_y)
cos_rad = np.cos(radians)
sin_rad = np.sin(radians)
qx = offset_x + cos_rad * adjusted_x + sin_rad * adjusted_y
qy = offset_y + -sin_rad * adjusted_x + cos_rad * adjusted_y
return qx, qy
Run Code Online (Sandbox Code Playgroud)
如果您将点表示为复数并使用带有虚数参数的 exp 函数(相当于其他答案中显示的 cos/sin 运算,但更容易编写和记住),这很容易。这是一个围绕所选原点旋转任意数量的点的函数:
import numpy as np
def rotate(points, origin, angle):
return (points - origin) * np.exp(complex(0, angle)) + origin
Run Code Online (Sandbox Code Playgroud)
要将单个点 (x1,y1) 围绕原点 (x0,y0) 旋转一定角度(以度为单位),您可以使用以下参数调用该函数:
points = complex(x1,y1)
origin = complex(x0,y0)
angle = np.deg2rad(degrees)
Run Code Online (Sandbox Code Playgroud)
要旋转多个点 (x1,y1), (x2,y2), ...,请使用:
points = np.array([complex(x1,y1), complex(x2,y2), ...])
Run Code Online (Sandbox Code Playgroud)
单个点 (200,300) 围绕 (100,100) 旋转 10 度的示例:
>>> new_point = rotate(complex(200,300), complex(100,100), np.deg2rad(10))
>>> new_point
(163.75113976783473+314.3263683691346j)
>>> (new_point.real, new_point.imag)
(163.75113976783473, 314.3263683691346)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28214 次 |
| 最近记录: |