旋转2D对象的函数?

11

是否可能在Python中编写一个函数,通过只提供2D结构的坐标(x,y)作为参数,旋转该结构?其他参数将包括轴、速度和方向。

据我所知,这只有通过计算点到对称点和轴的距离来实现,因此它总是会变化的。除非是由标准形状(三角形、矩形、正方形等)组成的2D结构,否则是不可能做到的。

欢迎提供好的示例。


真的不理解你的第二段。但是除此之外,如果你有一个点/轴可以旋转,并且有一个角度,那么你可以独立地旋转每个点。请参见https://dev59.com/yHE95IYBdhLWcg3wkekf#2259502,其中提供了C++示例。 - YXD
请问您能否扩展第二段内容?我真的不确定您在暗示什么。 - Shog9
2个回答

23

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

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

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

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

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

一些输出:

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))

现在,我们只需要使用之前的函数来旋转多边形的每个角落:

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)]

喜欢这个答案,非常有帮助!由于某些原因,对于大于360度的角度,它的效果不是很好,所以我在此之后添加了while counterangle > 0: counterangle -= 360while counterangle < 0: counterangle += 360。这样,所有的角度都变成了正数并且小于360度。我还添加了counterangle = 360 - angle,这样我就可以处理顺时针角度了。 - Luke Taylor
1
@Luke:在该函数中使用counterangle % 360作为旋转量应该就可以了。 - martineau
@martineau 是的,现在我已经深入了解这个领域一年半了,我意识到了这一点。谢谢 ;) - Luke Taylor
太棒了,这正是我所需要的完美答案! - zen_of_python

6
您可以围绕平面上任意一点旋转二维点阵数组,方法是先将所有点平移(移动),使旋转点成为原点(0, 0),对每个点的x和y坐标应用标准旋转公式,然后通过刚开始操作的完全相反的方法进行“取消平移”。

在计算机图形学中,通常使用称为变换矩阵的东西来完成这个过程。

相同的概念也可以轻松扩展到使用三维点。

编辑:

查看我的回答给定两个顶点,围绕中心点旋转直线,其中包含使用此技术的完整示例。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接