旋转2d对象的函数? [英] Function for rotating 2d objects?

查看:136
本文介绍了旋转2d对象的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在python中编写一个函数来旋转任何2d结构,参数只是结构中点的坐标(x,y)?其他参数将包括轴,速度和方向。

根据我的理解,只有通过计算从对称点到轴线的点距离才有可能,因此它总是会变化的,因此除了2d结构标准形状(三角形,长方形,正方形等)

好的例子值得赞赏。

解决方案<首先,我们需要一个函数来旋转原点周围的点。



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

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


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

 从数学导入sin,cos,弧度

def rotate_point(point,angle,center_point =(0,0)):
围绕center_point旋转一个点(默认为原点)
角度以度为单位
旋转是逆时针

angle_rad =弧度(角度%360)
#移动该点,使得center_point成为原点
new_point =(point [0] - center_point [0],点[1] - center_point [1])$ ​​b $ b 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))
#反转我们完成的移动
new_point =(new_point [0] + center_point [0],new_point [1] + center_point [ 1])$ ​​b $ b return new_point

一些输出:

$ prerint $($($,$))$ print $($ $ $ $ $ $)打印(rotate_point((1,1),90,(2,1)))
#打印(rotate_p (2.0,1.0)
print(rotate_point((2,2),45,(1,1) ))
#打印(1.0,2.4142),它等于(1,1 + sqrt(2))

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

  def rotate_polygon(多边形,角度,center_point =(0,0)):
旋转由center_point(默认原点)表示为(x,y)
的拐角组成的给定多边形
旋转为逆时针
角度为度数

rotate_polygon = []
为多边形中的角:
rotate_corner = rotate_point(角,角,center_point)
rotate_polygon.append(rotated_corner)
返回rotate_polygon

输出示例:

  my_polygon = [(0,0),(1,0),(0,1)] 
print(rotate_polygon(my_polygon,90))
#这会给出[(0.0,0.0),(0.0,1。 0),(-1.0,0.0)]


Is it possible to write a function in python that could rotate any 2d structure with the arguments being only the coordinates (x,y) of the points in the structure? Additional arguments would be included for axis, speed and direction.

To my understanding it would only be possible by calculating point distance from symmetrical points and the axis and therefore it would always vary and is thus impossible except for 2d structures made up of standard shapes (triangles, rectangles, squares etc)

Good examples would be appreciated.

解决方案

First, we need a function to rotate a point around origin.

When we rotate a point (x,y) around origin by theta degrees, we get the coordinates:

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

If we want to rotate it around a point other than the origin, we just need to shift it so the center point becomes the origin. Now, we can write the following function:

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

Some outputs:

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

Now, we just need to rotate every corner of the polygon using our previous function:

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

Example output:

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

这篇关于旋转2d对象的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆