Hướng dẫn 3d rotation matrix python - Python ma trận xoay 3d

Here is the counter clockwise matrix rotation as one line in pure python (i.e., without numpy):

Nội dung chính

  • Lời mở đầu
  • Xoay vector trong không gian 2D
  • Xoay vector trong không gian 3D
  • Xoay vector theo trục
  • Euler angle
  • Công thức Euler–Rodrigues
  • Quaternions
  • Lời kết
new_matrix = [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]

If you want to do this in a function, then

def rotate_matrix( m ):
    return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]

and either way, the result for

m = [ [1,2,3], [2,3,3], [5,4,3]]

is

[[3, 3, 3], [2, 3, 4], [1, 2, 5]]

Aside, if you want the usual transpose, then the simple one line pure python version is

[[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]

Lời mở đầu

Xoay vector trong không gian 2D

Xoay vector trong không gian 2D

Xoay vector trong không gian 3D

from matplotlib import pyplot as plt
import math
import numpy as np
p1 = [0, 0]
p2 = [40, 40]

plt.axis([-50, 50, -50, 50])
plt.plot([p1[0], p2[0]], [p1[1], p2[1]], label = "line 1")

Hướng dẫn 3d rotation matrix python - Python ma trận xoay 3d

Xoay vector theo trục

Euler angle

newX = v[0] * math.cos(angle) - v[1] * math.sin(angle)
newY = v[0] * math.sin(angle) + v[1] * math.cos(angle)

Công thức Euler–Rodrigues

newX = v[0] * math.cos(angle) + v[1] * math.sin(angle)
newY = -v[0] * math.sin(angle) + v[1] * math.cos(angle)

Quaternions

p1 = [0, 0]
p2 = [40, 40]

plt.axis([-50, 50, -50, 50])
plt.plot([p1[0], p2[0]], [p1[1], p2[1]], label = "line 1")

v = [p2[0] - p1[0], p2[1] - p1[1]]

angle = np.deg2rad(90)

newX = v[0] * math.cos(angle) + v[1] * math.sin(angle)
newY = -v[0] * math.sin(angle) + v[1] * math.cos(angle)

p3 = [p1[0] + newX, p1[1] + newY]

plt.axis([-50, 50, -50, 50])
plt.plot([p1[0], p3[0]], [p1[1], p3[1]], label = "line 2")

plt.xlabel('x')
plt.ylabel('y')
plt.show()

Lời kết

Xoay vector trong không gian 3D

Xoay vector theo trục

Xoay vector theo trục

Euler angle

  • Công thức Euler–Rodrigues
  • Quaternions
  • Lời kết
If you want to do this in a function, then

and either way, the result for

def rotate_matrix( m ):
    return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
1

Euler angle

Công thức Euler–Rodrigues

Quaternions

  • Lời kết
  • If you want to do this in a function, then
  • and either way, the result for
  • is
def rotate_matrix( m ):
    return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
2

Công thức Euler–Rodrigues

Quaternions

Lời kết

If you want to do this in a function, then

and either way, the result for

def rotate_matrix( m ):
    return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
3

Quaternions

Lời kết

If you want to do this in a function, then

def rotate_matrix( m ):
    return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
4

and either way, the result for

is

Aside, if you want the usual transpose, then the simple one line pure python version is

Chào các bạn, mọi người có khỏe không, hôm nay tôi muốn bàn một chút về việc xoay vector trong không gian 2 chiều và 3 chiều. Do không phải dân chuyên Toán nên các thuật ngữ như Euler, Quaternions, ... tôi cũng chả có cách nào giải thích cho các bạn, nhưng đưa ra vài ví dụ trong lập trình chắc vẫn được nên hôm nay tôi viết bài này cũng muốn tham khảo ý kiến một chút.

Như các bạn đã biết, không gian 2D chỉ chứa trục x và trục y nên ta nhìn qua là một mặt phẳng không có chiều sâu. Để rõ ràng, tôi sẽ minh họa một vector tạo thành từ 2 điểm bất kỳ bằng thư viện matplotlib.

def rotate_matrix( m ):
    return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
5

Tôi đã có 1 vector trong không gian 2 chiều, giờ tôi sẽ xoay vector theo một góc chỉ định, ở đây tôi lấy góc 90 độ. Dựa trên công thức dưới đây sẽ sinh ra x y của vector mới

def rotate_matrix( m ):
    return [[m[j][i] for j in range(len(m))] for i in range(len(m[0])-1,-1,-1)]
6

Lời kết

If you want to do this in a function, then

and either way, the result for

is

Aside, if you want the usual transpose, then the simple one line pure python version is

https://gist.github.com/LyleScott/e36e08bfb23b1f87af68c9051f985302

Chào các bạn, mọi người có khỏe không, hôm nay tôi muốn bàn một chút về việc xoay vector trong không gian 2 chiều và 3 chiều. Do không phải dân chuyên Toán nên các thuật ngữ như Euler, Quaternions, ... tôi cũng chả có cách nào giải thích cho các bạn, nhưng đưa ra vài ví dụ trong lập trình chắc vẫn được nên hôm nay tôi viết bài này cũng muốn tham khảo ý kiến một chút.

https://stackoverflow.com/questions/17763655/rotation-of-a-point-in-3d-about-an-arbitrary-axis-using-python

https://en.wikipedia.org/wiki/Rotation_matrix#Examples

https://en.wikipedia.org/wiki/Euler–Rodrigues_formula