checked task3

This commit is contained in:
2024-09-06 17:31:01 +08:00
parent 26b81de3e8
commit 486d453dcc
3 changed files with 72 additions and 1 deletions

1
.gitignore vendored
View File

@@ -3,4 +3,5 @@
*.bak
*.dat
*.log
*.gif
__pycache__/

View File

@@ -8,6 +8,8 @@ from typing import *
import threading
import numba
import multiprocessing
import io
from PIL import Image
mp.dps = 15 # 设置精度为15位小数
@@ -62,6 +64,63 @@ class Dragon:
node_list[i+1]["v"] = node_list[i]["v"] * (-mp.cos(alpha_i + beta_i) / mp.cos(alpha_ip1 - gama_i))
return node_list
def GenerateImg(self, node_list):
fig = plt.figure(figsize=(12, 12))
# 绘制灰色螺旋线
theta = np.linspace(0, float(node_list[-1]["theta"]), 1000)
x = [float(self.Theta2Dot(t)[0]) for t in theta]
y = [float(self.Theta2Dot(t)[1]) for t in theta]
plt.plot(x, y, color='gray', linewidth=0.5)
# 绘制节点、连接线和木板
for i in range(len(node_list) - 1):
x1, y1 = [float(coord) for coord in node_list[i]["node"]]
x2, y2 = [float(coord) for coord in node_list[i+1]["node"]]
# 绘制红色节点
plt.plot(x1, y1, 'ro', markersize=3)
# 绘制蓝色连接线
plt.plot([x1, x2], [y1, y2], 'b-', linewidth=0.5)
# 计算并绘制木板(长方形)
dx = x2 - x1
dy = y2 - y1
length = np.sqrt(dx**2 + dy**2)
angle = np.arctan2(dy, dx)
rect_length = length + 0.55 # 总长度加上两端各延伸的0.275m
rect_width = 0.3
# 计算长方形的中心点
center_x = (x1 + x2) / 2
center_y = (y1 + y2) / 2
# 计算长方形的左下角坐标
rect_x = center_x - rect_length/2 * np.cos(angle) + rect_width/2 * np.sin(angle)
rect_y = center_y - rect_length/2 * np.sin(angle) - rect_width/2 * np.cos(angle)
rect = Rectangle((rect_x, rect_y), rect_length, rect_width,
angle=angle*180/np.pi, fill=False, edgecolor='g')
plt.gca().add_patch(rect)
# 绘制最后一个节点
x, y = [float(coord) for coord in node_list[-1]["node"]]
plt.plot(x, y, 'ro', markersize=3)
plt.axis('equal')
# 创建一个 BytesIO 对象来存储图像
buf = io.BytesIO()
plt.savefig(buf, format='png')
buf.seek(0)
# 清除当前图形,释放内存
plt.close(fig)
# 返回图像对象
return Image.open(buf)
# 将结果转换为float并保留6位小数

View File

@@ -28,3 +28,14 @@ if __name__ == "__main__":
print("Error")
else:
print("OK")
# Now generate an gif for human to check
dragen = Dragon(mp.mpf(kPitchToTest)/(2*mp.pi))
kTotalFrames=100
kStepDeltaTheta=(kDeltaThetaEnd-kDeltaThetaBeg)/kTotalFrames
frame_list=[]
for i in range(kTotalFrames):
delta_theta=kDeltaThetaBeg+kStepDeltaTheta*i
cur_frame=dragen.GenerateImg(mp2float(dragen.CalcMoveList(delta_theta)))
frame_list.append(cur_frame)
frame_list.reverse()
frame_list[0].save('dragon.gif', save_all=True, append_images=frame_list[1:], optimize=False, duration=1000, loop=0)