Python 旋转2d对象的函数?

Python 旋转2d对象的函数?,python,function,rotation,2d,physics,Python,Function,Rotation,2d,Physics,是否可以用python编写一个函数来旋转任何2d结构,参数仅为结构中点的坐标(x,y)?附加参数包括轴、速度和方向 据我所知,只有通过计算对称点和轴之间的点距离才有可能,因此它总是会变化,因此除了由标准形状(三角形、矩形、正方形等)组成的二维结构外,这是不可能的 好的例子将不胜感激。首先,我们需要一个函数来围绕原点旋转一个点 当我们将一个点(x,y)围绕原点旋转θ度时,我们得到坐标: (x*cos(θ)-y*sin(θ),x*sin(θ)+y*cos(θ)) 如果我们想围绕原点以外的点旋转它,我

是否可以用python编写一个函数来旋转任何2d结构,参数仅为结构中点的坐标(x,y)?附加参数包括轴、速度和方向

据我所知,只有通过计算对称点和轴之间的点距离才有可能,因此它总是会变化,因此除了由标准形状(三角形、矩形、正方形等)组成的二维结构外,这是不可能的


好的例子将不胜感激。

首先,我们需要一个函数来围绕原点旋转一个点

当我们将一个点(x,y)围绕原点旋转θ度时,我们得到坐标:

(x*cos(θ)-y*sin(θ),x*sin(θ)+y*cos(θ))

如果我们想围绕原点以外的点旋转它,我们只需要移动它,使中心点成为原点。 现在,我们可以编写以下函数:

from math import sin, cos, radians

def rotate_point(point, angle, center_point=(0, 0)):
    """Rotates a point around center_point(origin by default)
    Angle is in degrees.
    Rotation is counter-clockwise
    """
    angle_rad = radians(angle % 360)
    # Shift the point so that center_point becomes the origin
    new_point = (point[0] - center_point[0], point[1] - center_point[1])
    new_point = (new_point[0] * cos(angle_rad) - new_point[1] * sin(angle_rad),
                 new_point[0] * sin(angle_rad) + new_point[1] * cos(angle_rad))
    # Reverse the shifting we have done
    new_point = (new_point[0] + center_point[0], new_point[1] + center_point[1])
    return new_point
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
一些产出:

print(rotate_point((1, 1), 90, (2, 1)))
# This prints (2.0, 0.0)
print(rotate_point((1, 1), -90, (2, 1)))
# This prints (2.0, 2.0)
print(rotate_point((2, 2), 45, (1, 1)))
# This prints (1.0, 2.4142) which is equal to (1,1+sqrt(2))
现在,我们只需要使用前面的函数旋转多边形的每个角:

from math import sin, cos, radians

def rotate_point(point, angle, center_point=(0, 0)):
    """Rotates a point around center_point(origin by default)
    Angle is in degrees.
    Rotation is counter-clockwise
    """
    angle_rad = radians(angle % 360)
    # Shift the point so that center_point becomes the origin
    new_point = (point[0] - center_point[0], point[1] - center_point[1])
    new_point = (new_point[0] * cos(angle_rad) - new_point[1] * sin(angle_rad),
                 new_point[0] * sin(angle_rad) + new_point[1] * cos(angle_rad))
    # Reverse the shifting we have done
    new_point = (new_point[0] + center_point[0], new_point[1] + center_point[1])
    return new_point
def rotate_polygon(polygon, angle, center_point=(0, 0)):
    """Rotates the given polygon which consists of corners represented as (x,y)
    around center_point (origin by default)
    Rotation is counter-clockwise
    Angle is in degrees
    """
    rotated_polygon = []
    for corner in polygon:
        rotated_corner = rotate_point(corner, angle, center_point)
        rotated_polygon.append(rotated_corner)
    return rotated_polygon
示例输出:

my_polygon = [(0, 0), (1, 0), (0, 1)]
print(rotate_polygon(my_polygon, 90))
# This gives [(0.0, 0.0), (0.0, 1.0), (-1.0, 0.0)]

可以围绕平面上的任意点旋转二维点阵列,方法是首先平移(移动)所有点,使旋转点成为原点
(0,0)
,将标准旋转公式应用于每个点的x&y坐标,然后“取消平移”它们的数量与最初所做的完全相反

在计算机图形学中,这通常是通过使用一种叫做

同样的概念也可以很容易地扩展到三维点

编辑:


请参阅我对问题的回答,以获取使用此技巧的完整示例。

我真的不理解你的第二段。但是,如果你有一个旋转的点/轴和一个角度,那么你可以独立地旋转每个点。请看c++中的一个例子,您能扩展第二段吗?真的不知道你在暗示什么。喜欢这个答案,帮助很大!由于某些原因,对于大于360的角度,它不太适用,因此我添加了
而counterangle>0:counterangle-=360
而counterangle<0:counterangle+=360
。这样,所有角度均为正值且小于360度。我还添加了
counterangle=360-angle
,这样我就可以使用顺时针角度了。@Luke:你可能只需要使用
counterangle%360
作为函数中使用的旋转量。@martineau是的,现在我已经深入研究这方面一点一年半了,我意识到了这一点。谢谢;)真棒的答案-这是我所需要的完美!