以度为单位围绕另一个点旋转点python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34372480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:53:22  来源:igfitidea点击:

Rotate point about another point in degrees python

pythonmathdegrees

提问by user2592835

If you had a point (in 2d), how could you rotate that point by degrees around the other point (the origin) in python?

如果你有一个点(在 2d 中),你怎么能在 python 中围绕另一个点(原点)旋转那个点?

You might, for example, tilt the first point around the origin by 10 degrees.

例如,您可以将第一个点围绕原点倾斜 10 度。

Basically you have one point PointA and origin that it rotates around. The code could look something like this:

基本上你有一个 PointA 和它旋转的原点。代码可能如下所示:

PointA=(200,300)
origin=(100,100)

NewPointA=rotate(origin,PointA,10) #The rotate function rotates it by 10 degrees

回答by Mark Dickinson

The following rotatefunction performs a rotation of the point pointby the angle angle(counterclockwise, in radians) around origin, in the Cartesian plane, with the usual axis conventions: x increasing from left to right, y increasing vertically upwards. All points are represented as length-2 tuples of the form (x_coord, y_coord).

以下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

If your angle is specified in degrees, you can convert it to radians first using math.radians. For a clockwise rotation, negate the angle.

如果您的角度以度数指定,您可以先使用 将其转换为弧度math.radians。对于顺时针旋转,取消角度。

Example: rotating the point (3, 4)around an origin of (2, 2)counterclockwise by an angle of 10 degrees:

示例:将点(3, 4)绕原点(2, 2)逆时针旋转 10 度:

>>> point = (3, 4)
>>> origin = (2, 2)
>>> rotate(origin, point, math.radians(10))
(2.6375113976783475, 4.143263683691346)

Note that there's some obvious repeated calculation in the rotatefunction: math.cos(angle)and math.sin(angle)are each computed twice, as are px - oxand py - oy. I leave it to you to factor that out if necessary.

请注意,rotate函数中有一些明显的重复计算:math.cos(angle)math.sin(angle)每个都计算了两次,px - oxpy - oy。如有必要,我让您自行考虑。

回答by Gabriel Eng

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]

回答by ImportanceOfBeingErnest

An option to rotate a point by some degrees about another point is to use numpyinstead of math. This allows to easily generalize the function to take any number of points as input, which might e.g. be useful when rotating a polygon.

将一个点绕另一点旋转一定度数的一个选项是使用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)

回答by an0nym0use

After going through a lot of code and repositories. This function worked best for me. Also it is efficient as it calculates sine and cosine values only once.

经过大量代码和存储库之后。这个功能对我来说效果最好。它也很有效,因为它只计算一次正弦和余弦值。

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