以度为单位围绕python的另一个点旋转点 [英] Rotate point about another point in degrees python

查看:228
本文介绍了以度为单位围绕python的另一个点旋转点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有一个点(在2d中),那么如何围绕python中的另一个点(原点)旋转该点呢?

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

例如,您可以将原点周围的第一个点倾斜10度.

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

基本上,您有一个点PointA及其绕其旋转的原点. 代码可能看起来像这样:

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

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

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

推荐答案

以下rotate函数将点point围绕origin旋转角度angle(逆时针,以弧度表示),其中笛卡尔平面,具有通常的轴约定:x从左到右增加,y垂直向上增加.所有点都表示为长度为2的元组,格式为(x_coord, y_coord).

The following rotate function performs a rotation of the point point by 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).

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

如果以度为单位指定角度,则可以先使用math.radians将其转换为弧度.要顺时针旋转,请抵消角度.

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

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

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

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

请注意,在rotate函数中有一些显而易见的重复计算:math.cos(angle)math.sin(angle)分别计算两次,px - oxpy - oy也是如此.如果需要的话,我留给您考虑.

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

这篇关于以度为单位围绕python的另一个点旋转点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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