-
Notifications
You must be signed in to change notification settings - Fork 0
/
rotation.py
80 lines (65 loc) · 2.09 KB
/
rotation.py
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import numpy as np
import cv2
from math import *
def rotate(points):
#输入旋转点和旋转角度
x_original, y_original = input("请输入指定点的坐标:").split()
theta = -int(input("请输入指定的角度:"))
x_original = (int(x_original) + 750)
y_original = 1200 - (int(y_original) + 600)
#初始化旋转矩阵
rotation_array = np.array([cos(theta), sin(theta), 0, -sin(theta), cos(theta), 0, 0, 0, 1]).reshape(3, 3)
#开始旋转
new_points = []
for point in points:
#将该点转换成向量
point[0] -= x_original
point[1] -= y_original
#坐标转换为齐次坐标
point.append(1)
#进行旋转
point = np.array(point).dot(rotation_array)
#旋转后的向量转化为坐标
point = list(point)
point[0] = int(point[0] + x_original)
point[1] = int(point[1] + y_original)
#将齐次坐标变为正常坐标
point.pop()
new_points.append(point)
return new_points
def draw(points):
# 初始化一个空画布 1000×500 三通道 背景色为白色
col = 1500
row = 1200
canvas = np.ones((row, col, 3), dtype="uint8")
canvas *= 255
# 点集矩阵变形
points = np.array(points)
points = points.reshape((-1, 1, 2))
cv2.polylines(canvas, pts=[points], isClosed=True, color=[0, 0, 0], thickness=2)
cv2.imshow("polylines", canvas)
cv2.waitKey(0)
cv2.destroyWindow("polylines")
def main():
# 输入六边形的六个顶点坐标
points = []
ymin = 1000
ymax = -1000
print("请按照x递增的顺序顺时针依次输入六边形六个顶点的坐标:")
for i in range(6):
x, y = input().split()
x = int(x) + 750
y = 600 - int(y)
if y < ymin:
ymin = y
if y > ymax:
ymax = y
points.append([x, y])
# 画出旋转前的六边形
draw(points)
#对六边形进行旋转
points = rotate(points)
#画出旋转后的六边形
draw(points)
if __name__ == "__main__":
main()