Python函数用于旋转2D对象函数、对象、Python

2023-09-07 12:35:49 作者:古巷

是否可以用Python语言编写一个函数,可以旋转任何2D结构的参数是在结构上分只有合作条例(X,Y)? (参数将包括轴,速度和方向)

要我的理解,将只可能通过计算对称点,轴点的距离,因此总是会有所不同,除了由标准形状(三角形,长方形,正方形等)

如果有人知道任何很好的例子这将是AP preciated。

解决方案

我已经写了这样的功能,之前pygame的使用。这是有点不言自明,但随时提出需要澄清的部分。这是我的code:

 进口数学
高清rotatePolygon(多边形,THETA):
    旋转给定的多边形它由拐角重新psented为(x,y)的$ P $,
    围绕原点,顺时针,2θ角
    THETA = math.radians(THETA)
    rotatedPolygon = []
    拐角的多边形:
        rotatedPolygon.append((角[0] * math.cos(THETA)-corner [1] * math.sin(THETA),角[0] * math.sin(THETA)+角落[1] * math.cos( THETA)))
    返回rotatedPolygon


my_polygon = [(0,0),(1,0),(0,1)]
打印rotatePolygon(my_polygon,90)
 
python 36 封装与面向对象函数

本打印:   [(0.0,0.0),(6.123233995736766e-17,1.0),(-1.0,6.123233995736766e-17)] 这是近 [(0,0),(0,1),( - 1,0)]

我也有一个功能,旋转给定的点(x,y)的周围另一个。我的情况下分享你觉得它有用(你可以结合这两个函数来绕另一个多边形的点,而不是原来的):

 高清rotatePoint(中心点,点,角度):
    绕另一个中心点,角点为度。
    旋转逆时针是
    角= math.radians(角度)
    temp_point =点[0] -centerPoint [0],点[1] -centerPoint [1]
    temp_point =(temp_point [0] * math.cos(角度)-temp_point [1] * math.sin(角),temp_point [0] * math.sin(角度)+ temp_point [1] * math.cos(角度) )
    temp_point = temp_point [0] +中心点[0],temp_point [1] +中心点[1]
    回报temp_point

打印rotatePoint((1,1),(2,2),45)
 

这将打印(1.0,2.414213562373095),这等于(1,1 +的sqrt(2)) (请注意,这个人是逆时针)

Is it possible to write a function in python that could rotate any 2d structure with the arguments being only the co-ordinance (x,y) of the points in the structure? (args 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)

If anyone knows any good examples it would be appreciated.

解决方案

I have written such a function to use in pygame before. It is kinda self-explanatory but feel free to ask the parts that require clarification. Here is my code:

import math
def rotatePolygon(polygon,theta):
    """Rotates the given polygon which consists of corners represented as (x,y),
    around the ORIGIN, clock-wise, theta degrees"""
    theta = math.radians(theta)
    rotatedPolygon = []
    for corner in polygon :
        rotatedPolygon.append(( corner[0]*math.cos(theta)-corner[1]*math.sin(theta) , corner[0]*math.sin(theta)+corner[1]*math.cos(theta)) )
    return rotatedPolygon


my_polygon = [(0,0),(1,0),(0,1)]
print rotatePolygon(my_polygon,90)

This prints: [(0.0, 0.0), (6.123233995736766e-17, 1.0), (-1.0, 6.123233995736766e-17)] Which is nearly [(0,0),(0,1),(-1,0)]

I also have a function that rotates the given point(x,y) around another one. I share it in case you find it useful (And you can combine these two functions to rotate the polygon around another point rather than the origin):

def rotatePoint(centerPoint,point,angle):
    """Rotates a point around another centerPoint. Angle is in degrees.
    Rotation is counter-clockwise"""
    angle = math.radians(angle)
    temp_point = point[0]-centerPoint[0] , point[1]-centerPoint[1]
    temp_point = ( temp_point[0]*math.cos(angle)-temp_point[1]*math.sin(angle) , temp_point[0]*math.sin(angle)+temp_point[1]*math.cos(angle))
    temp_point = temp_point[0]+centerPoint[0] , temp_point[1]+centerPoint[1]
    return temp_point

print rotatePoint((1,1),(2,2),45)

This will print (1.0, 2.414213562373095),which is equal to (1,1+sqrt(2)) (Note that this one is counter-clockwise)