【问题标题】:Rotate a square by an angle in degree将正方形旋转角度度数
【发布时间】:2014-05-05 00:12:35
【问题描述】:

我有一个中心为 x0, y0 的正方形。我希望将这个正方形的顶点旋转给定的角度(θ),以度数表示,并以顺时针方向返回新的旋转顶点。我正在使用这个approach 来旋转应用于每个顶点的单个点

将点 (px, py) 围绕点 (x0, y0) 旋转角度 theta,您将得到:

p'x = cos(theta) * (px-x0) - sin(theta) * (py-y0) + x0
p'y = sin(theta) * (px-x0) + cos(theta) * (py-y0) + y0

where: 
px, py = coordinate of the point
y0, x0, = centre of rotation
theta = angle of rotation

我在 Python 中编写了一个函数,其中的参数是:x、y(=正方形的中心)、正方形的边和 theta_degree(以度为单位的旋转角度),但返回是逆时针方向

from math import cos, sin

def get_square_plot(x, y, side, theta_degree=0):
    theta = theta_degree * pi/180
    xa = x-side/2
    ya = y+side/2
    xb = x+side/2
    yb = y+side/2
    xc = x+side/2
    yc = y-side/2
    xd = x-side/2
    yd = y-side/2
    xa_new = cos(theta) * (xa - x) - sin(theta) * (ya - y) + x
    ya_new = sin(theta) * (xa - x) - cos(theta) * (ya - y) + y
    xb_new = cos(theta) * (xb - x) - sin(theta) * (yb - y) + x
    yb_new = sin(theta) * (xb - x) - cos(theta) * (yb - y) + y
    xc_new = cos(theta) * (xc - x) - sin(theta) * (yc - y) + x
    yc_new = sin(theta) * (xc - x) - cos(theta) * (yc - y) + y
    xd_new = cos(theta) * (xd - x) - sin(theta) * (yd - y) + x
    yd_new = sin(theta) * (xd - x) - cos(theta) * (yd - y) + y
    return [(xa_new, ya_new),(xb_new, yb_new),(xc_new, yc_new),(xd_new, yd_new)]

get_square_plot(0, 0, 10, 0)
[(-5.0, -5.0), (5.0, -5.0), (5.0, 5.0), (-5.0, 5.0)]

而不是

[(-5.0, 5.0), (5.0, 5.0), (5.0, -5.0), (-5.0, -5.0)]

【问题讨论】:

    标签: python rotation computational-geometry angle


    【解决方案1】:

    这很简单——你的所有 y 值的公式都是错误的。

    应该是:

    ya_new = sin(theta) * (xa - x) + cos(theta) * (ya - y) + y
    

    加法而不是减法。

    【讨论】:

    • 最简单的最难捕捉。
    【解决方案2】:

    也不要忘记几何模块。它可以处理各种基本形状并处理平移、旋转等...

    可以用RegularPolygon 构造一个正方形。它通过从中心定位给定半径的顶点来做到这一点;要获得具有给定边长的正方形,请除以 sqrt(2)。这是一个旋转菱形方向的函数,使边平行于轴,然后旋转所需的角度,a

    >>> Square = lambda c, r, a: RegularPolygon(c, r/sqrt(2), 4, -rad(a) - pi/4)
    >>> Square((0,0),10,0).vertices
    [Point(5, -5), Point(5, 5), Point(-5, 5), Point(-5, -5)]
    >>> [w.n(2) for w in Square((0,0),10,1).vertices]
    [Point(4.9, -5.1), Point(5.1, 4.9), Point(-4.9, 5.1), Point(-5.1, -4.9)]
    

    请注意,轻微的 CW 旋转 1 度 (-rad(1)) 会使第一个顶点更靠近 y 轴,并且如我们预期的那样低一点。您也可以输入角度的符号:

    >>> from sympy.utilities.misc import filldedent
    >>> print filldedent(Square((0,0),10,a).vertices)
    
    [Point(5*sqrt(2)*cos(pi*a/180 + pi/4), -5*sqrt(2)*sin(pi*a/180 +
    pi/4)), Point(5*sqrt(2)*sin(pi*a/180 + pi/4), 5*sqrt(2)*cos(pi*a/180 +
    pi/4)), Point(-5*sqrt(2)*cos(pi*a/180 + pi/4), 5*sqrt(2)*sin(pi*a/180
    + pi/4)), Point(-5*sqrt(2)*sin(pi*a/180 + pi/4),
    -5*sqrt(2)*cos(pi*a/180 + pi/4))]
    

    您还可以通过旋转一个点 -theta(对于 CW)来检查您的点旋转公式:

    >>> var('px py theta x0 y0')
    (px, py, theta, x0, y0)
    >>> R = Point(px,py).rotate(-theta, Point(x0,y0))
    >>> R.x
    x0 + (px - x0)*cos(theta) + (py - y0)*sin(theta)
    >>> R.y
    y0 + (-px + x0)*sin(theta) + (py - y0)*cos(theta)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-23
      • 2022-01-18
      • 2021-01-09
      • 1970-01-01
      相关资源
      最近更新 更多