【点】二维空间坐标变换

二维空间坐标变换由平移(translation)、旋转(rotation)和缩放(scale)组成,可以将一个点从一个位置移动到另外一个位置。

一、平移变换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np

def get_translation(tx, ty):
return np.array([
[1, 0, tx],
[0, 1, ty],
[0, 0, 1]
])

# 原始点
point = np.array([2, 2])
print(point)
# 原始点齐次坐标
point = np.insert(point, len(point), values=1)

# 平移矩阵
T = get_translation(4, 2)

# 目标点
point_trans = np.dot(T, point)
# point_trans = T @ point
# 目标点笛卡尔坐标
point_trans = np.delete(point_trans, len(point_trans)-1)
print(point_trans)

二、旋转变换

1、逆时针旋转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import numpy as np

def get_CCW_rotation(angle):
angle = np.radians(angle) # 以弧度作为参数
return np.array([
[np.cos(angle), -np.sin(angle), 0],
[np.sin(angle), np.cos(angle), 0],
[ 0, 0, 1]
])

# 原始点
point = np.array([2, 2])
print(point)
# 原始点齐次坐标
point = np.insert(point, len(point), values=1)

# 旋转矩阵
R = get_CCW_rotation(90)

# 目标点
point_rot = np.dot(R, point)
# point_rot = R @ point
# 目标点笛卡尔坐标
point_rot = np.delete(point_rot, len(point_rot)-1)
print(point_rot)

2、顺时针旋转

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import numpy as np

def get_CW_rotation(angle):
angle = np.radians(angle) # 以弧度作为参数
return np.array([
[ np.cos(angle), np.sin(angle), 0],
[-np.sin(angle), np.cos(angle), 0],
[ 0, 0, 1]
])

# 原始点
point = np.array([2, 2])
print(point)
# 原始点齐次坐标
point = np.insert(point, len(point), values=1)

# 旋转矩阵
R = get_CW_rotation(90)

# 目标点
point_rot = np.dot(R, point)
# point_rot = R @ point
# 目标点笛卡尔坐标
point_rot = np.delete(point_rot, len(point_rot)-1)
print(point_rot)

三、缩放变换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import numpy as np

def get_scale(s):
return np.array([
[s, 0, 0],
[0, s, 0],
[0, 0, 1]
])

# 原始点
point = np.array([2, 2])
print(point)
# 原始点齐次坐标
point = np.insert(point, len(point), values=1)

# 缩放矩阵
S = get_scale(2)

# 目标点
point_scale = np.dot(S, point)
# point_scale = S @ point
# 目标点笛卡尔坐标
point_scale = np.delete(point_scale, len(point_scale)-1)
print(point_scale)
0%