二维(三维)坐标系中旋转矩阵详细推导

求三维坐标系的旋转矩阵通常需要求分别沿3个坐标轴的二维坐标系下的旋转矩阵,二维坐标系下的旋转矩阵的推导过程通常以某一点逆时针旋转 θ \theta θ角度进行推理。以下将通过此例来详细讲解二维坐标系下的旋转矩阵推导过程,并进一步给出其他方式的旋转矩阵。

一、二维坐标中点的旋转变换

点的旋转矩阵(逆时针旋转)

已知点 P ( x , y ) P(x, y) P(x,y),将该点以逆时针方向旋转 θ \theta θ角度后得到点 P ′ ( x ′ , y ′ ) P^{\prime}(x^{\prime}, y^{\prime}) P(x,y),如下图所示。求由点 P P P到点 P ′ P^{\prime} P的旋转矩阵。
请添加图片描述

  • 设半径为 r r r,由图可以分别得到以下三角公式:

    • 对于点 P ( x , y ) P(x, y) P(x,y)
      x = r cos ⁡ α y = r sin ⁡ α (1) \begin{aligned} & x=r \cos \alpha \\ & y=r \sin \alpha \end{aligned} \tag{1} x=rcosαy=rsinα(1)
    • 对于点 P ′ ( x ′ , y ′ ) P^{\prime}(x^{\prime}, y^{\prime}) P(x,y)
      x ′ = r cos ⁡ ( α + θ ) y ′ = r sin ⁡ ( α + θ ) (2) \begin{aligned} & x^{\prime}=r \cos (\alpha+\theta) \\ & y^{\prime}=r \sin (\alpha+\theta) \end{aligned} \tag{2} x=rcos(α+θ)y=rsin(α+θ)(2)
  • 根据两角和的正弦与余弦公式:
    sin ⁡ ( α + θ ) = sin ⁡ α cos ⁡ θ + cos ⁡ α sin ⁡ θ cos ⁡ ( α + θ ) = cos ⁡ α cos ⁡ θ − sin ⁡ α sin ⁡ θ (3) \begin{aligned} & \sin (\alpha+\theta)=\sin \alpha \cos \theta+\cos \alpha \sin \theta \\ & \cos (\alpha+\theta)=\cos \alpha \cos \theta-\sin \alpha \sin \theta \end{aligned} \tag{3} sin(α+θ)=sinαcosθ+cosαsinθcos(α+θ)=cosαcosθsinαsinθ(3)

  • 将公式(3)代入公式(2),可得到:
    x ′ = r cos ⁡ α cos ⁡ θ − r sin ⁡ α sin ⁡ θ y ′ = r sin ⁡ α cos ⁡ θ + r cos ⁡ α sin ⁡ θ (4) \begin{aligned} & x^{\prime} =r \cos \alpha \cos \theta-r \sin \alpha \sin \theta \\ & y^{\prime} =r \sin \alpha \cos \theta+r \cos \alpha \sin \theta \\ \end{aligned} \tag{4} x=rcosαcosθrsinαsinθy=rsinαcosθ+rcosαsinθ(4)

  • 将公式(1)代入公式(4),化简掉 α \alpha α,可得到 ( x ′ , y ′ ) (x^{\prime}, y^{\prime}) (x,y) ( x , y ) (x, y) (x,y)的关系,即:
    x ′ = x cos ⁡ θ − y sin ⁡ θ y ′ = y cos ⁡ θ + x sin ⁡ θ (5) \begin{aligned} & x^{\prime} =x \cos \theta-y \sin \theta \\ & y^{\prime} =y \cos \theta+x \sin \theta \\ \end{aligned} \tag{5} x=xcosθysinθy=ycosθ+xsinθ(5)

  • 整理公式(5)可得到旋转矩阵公式:
    x ′ = cos ⁡ θ ⋅ x − sin ⁡ θ ⋅ y y ′ = sin ⁡ θ ⋅ x + cos ⁡ θ ⋅ y ⇒ [ x ′ y ′ ] = [ cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ] [ x y ] (6) \begin{aligned} x^{\prime}&=\cos \theta \cdot x-\sin \theta \cdot y \\ y^{\prime}&=\sin \theta \cdot x+\cos \theta \cdot y \\ \Rightarrow \left[\begin{array}{l} x^{\prime} \\ y^{\prime}\end{array}\right] &=\left[\begin{array}{cc}\cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{array}\right] \left[\begin{array}{l} x \\ y \end{array}\right] \end{aligned} \tag{6} xy[xy]=cosθxsinθy=sinθx+cosθy=[cosθsinθsinθcosθ][xy](6)

  • 因此二维坐标系下逆时针旋转矩阵 R R R为:
    R = [ cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ] R = \left[\begin{array}{cc}\cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{array}\right] R=[cosθsinθsinθcosθ]

点的旋转矩阵(顺时针旋转)

已知点 P ′ ( x ′ , y ′ ) P^{\prime}(x^{\prime}, y^{\prime}) P(x,y),将该点以顺时针方向旋转 θ \theta θ角度后得到点 P ( x , y ) P(x, y) P(x,y),求 P P P

1、利用求逆矩阵的方式
  • 二阶矩阵的逆的求法为:主对角元素换位置,次对角元素变符号,再乘上系数1/(ad-bc),由于旋转矩阵 R R R的ad-bc=1,因此 R R R的逆矩阵 R ′ R^{\prime} R为:
    R ′ = [ cos ⁡ θ sin ⁡ θ − sin ⁡ θ cos ⁡ θ ] R^{\prime} = \left[\begin{array}{cc}\cos \theta & \sin \theta \\ -\sin \theta & \cos \theta \end{array}\right] R=[cosθsinθsinθcosθ]
  • 因此旋转公式为:
    [ x y ] = [ cos ⁡ θ sin ⁡ θ − sin ⁡ θ cos ⁡ θ ] [ x ′ y ′ ] \left[\begin{array}{l} x \\ y \end{array}\right] = \left[\begin{array}{cc}\cos \theta & \sin \theta \\ -\sin \theta & \cos \theta \end{array}\right] \left[\begin{array}{l} x^{\prime} \\ y^{\prime}\end{array}\right] [xy]=[cosθsinθsinθcosθ][xy]
2、利用几何知识推导

P ′ P^{\prime} P顺时针旋转 θ \theta θ角度到 P P P,相当于逆时针旋转 ( 2 π − θ ) (2 \pi-\theta) (2πθ),由于三角函数的周期为 2 π 2 \pi 2π,因此相当于旋转 − θ -\theta θ角度。

  • − θ -\theta θ代入到公式(6),得到:
    x = cos ⁡ ( − θ ) ⋅ x ′ − sin ⁡ ( − θ ) ⋅ y ′ = cos ⁡ θ ⋅ x ′ + sin ⁡ θ ⋅ y ′ y = sin ⁡ ( − θ ) ⋅ x ′ + cos ⁡ ( − θ ) ⋅ y ′ = − sin ⁡ θ ⋅ x ′ + cos ⁡ θ ⋅ y ′ \begin{aligned} x & =\cos (-\theta) \cdot x^{\prime}-\sin (-\theta) \cdot y^{\prime} \\ & =\cos \theta \cdot x^{\prime}+\sin \theta \cdot y^{\prime} \\ y & =\sin (-\theta) \cdot x^{\prime}+\cos (-\theta) \cdot y^{\prime} \\ & =-\sin \theta \cdot x^{\prime}+\cos \theta \cdot y^{\prime} \end{aligned} xy=cos(θ)xsin(θ)y=cosθx+sinθy=sin(θ)x+cos(θ)y=sinθx+cosθy
  • 整理上述公式可得到顺时针时的旋转矩阵公式:
    x = cos ⁡ θ ⋅ x ′ + sin ⁡ θ ⋅ y ′ y = − sin ⁡ θ ⋅ x ′ + cos ⁡ θ ⋅ y ′ ⇒ [ x y ] = [ cos ⁡ θ sin ⁡ θ − sin ⁡ θ cos ⁡ θ ] [ x ′ y ′ ] \begin{aligned} x&=\cos \theta \cdot x^{\prime}+\sin \theta \cdot y^{\prime} \\ y&=-\sin \theta \cdot x^{\prime}+\cos \theta \cdot y^{\prime} \\ \Rightarrow \left[\begin{array}{l} x \\ y \end{array}\right] &= \left[\begin{array}{cc}\cos \theta & \sin \theta \\ -\sin \theta & \cos \theta \end{array}\right] \left[\begin{array}{l} x^{\prime} \\ y^{\prime}\end{array}\right] \end{aligned} xy[xy]=cosθx+sinθy=sinθx+cosθy=[cosθsinθsinθcosθ][xy]

二、二维坐标系的旋转变换

二维坐标系 XOY \text{XOY} XOY中有一点 P ( x , y ) P(x, y) P(x,y),将坐标系 XOY \text{XOY} XOY逆时针旋转 θ \theta θ角度后得到新坐标系 X ′ O ′ Y ′ \text{X}^{\prime}\text{O}^{\prime}\text{Y}^{\prime} XOY,求点 P P P在新坐标系下的坐标 P ′ ( x ′ , y ′ ) P^{\prime}(x^{\prime}, y^{\prime}) P(x,y),这个过程称为坐标系变换。

新坐标的求法可以转换为逆向旋转坐标点的方式,并利用点的旋转变换求得。因此,坐标系时针旋转 θ \theta θ角度,相当于原坐标系中某一点时针旋转 θ \theta θ角度,反之亦然。

总结

  • 某点逆时针旋转 θ \theta θ角度(坐标系顺时针旋转 θ \theta θ角度)的旋转矩阵为:
    R = [ cos ⁡ θ − sin ⁡ θ sin ⁡ θ cos ⁡ θ ] R = \left[\begin{array}{cc}\cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{array}\right] R=[cosθsinθsinθcosθ]
  • 某点顺时针旋转 θ \theta θ角度(坐标系逆时针旋转 θ \theta θ角度)的旋转矩阵为:
    R ′ = [ cos ⁡ θ sin ⁡ θ − sin ⁡ θ cos ⁡ θ ] R^{\prime} = \left[\begin{array}{cc}\cos \theta & \sin \theta \\ -\sin \theta & \cos \theta \end{array}\right] R=[cosθsinθsinθcosθ]
    三维坐标变换可进一步参考Link3

参考:
Link1
Link2
Link3

  • 5
    点赞
  • 53
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是用C语言写的三维旋转矩阵代码,可以根据需要修改旋转角度和坐标轴: ```c #include <stdio.h> #include <math.h> #define PI 3.14159265 // 三维向量结构体 typedef struct { double x, y, z; } Vector3; // 旋转矩阵结构体 typedef struct { double m[3][3]; } Matrix3x3; // 初始化旋转矩阵 Matrix3x3 init_rotation_matrix(double angle, char axis) { Matrix3x3 matrix; double radian = angle * PI / 180.0; double c = cos(radian); double s = sin(radian); switch (axis) { case 'x': matrix.m[0][0] = 1.0; matrix.m[1][1] = c; matrix.m[1][2] = -s; matrix.m[2][1] = s; matrix.m[2][2] = c; break; case 'y': matrix.m[0][0] = c; matrix.m[0][2] = s; matrix.m[1][1] = 1.0; matrix.m[2][0] = -s; matrix.m[2][2] = c; break; case 'z': matrix.m[0][0] = c; matrix.m[0][1] = -s; matrix.m[1][0] = s; matrix.m[1][1] = c; matrix.m[2][2] = 1.0; break; default: break; } return matrix; } // 矩阵乘法 Matrix3x3 matrix_multiply(Matrix3x3 a, Matrix3x3 b) { Matrix3x3 c; int i, j, k; for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { c.m[i][j] = 0.0; for (k = 0; k < 3; k++) { c.m[i][j] += a.m[i][k] * b.m[k][j]; } } } return c; } // 旋转向量 Vector3 rotate_vector(Vector3 vector, Matrix3x3 matrix) { Vector3 result; result.x = matrix.m[0][0] * vector.x + matrix.m[0][1] * vector.y + matrix.m[0][2] * vector.z; result.y = matrix.m[1][0] * vector.x + matrix.m[1][1] * vector.y + matrix.m[1][2] * vector.z; result.z = matrix.m[2][0] * vector.x + matrix.m[2][1] * vector.y + matrix.m[2][2] * vector.z; return result; } int main() { // 测试代码 Vector3 vector = {1.0, 0.0, 0.0}; Matrix3x3 matrix = init_rotation_matrix(45.0, 'z'); Vector3 result = rotate_vector(vector, matrix); printf("result: (%f, %f, %f)\n", result.x, result.y, result.z); return 0; } ``` 该代码定义了一个三维向量结构体和一个三维旋转矩阵结构体。`init_rotation_matrix`函数用于初始化旋转矩阵,根据旋转角度和坐标轴生成一个旋转矩阵;`matrix_multiply`函数用于矩阵乘法;`rotate_vector`函数用于旋转三维向量。在`main`函数,我们测试了一个向量绕z轴旋转45度后的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值