【Python】 圆角四边形三轴联动仿真 用于印刷用的
记录项目中仿真测试的代码,效果图如下:https://attach.52pojie.cn//forum/202103/15/192933k21mf1y2viyoii2f.gif?l
欢迎一起学习和讨论
主要代码如下:
import numpy as np
from math import sqrt, atan, pi, tan, sin, cos, fabs
import matplotlib.pyplot as plt
from matplotlib import animation
plt.rcParams['figure.figsize'] = [6, 6]# for square canvas
plt.rcParams['figure.subplot.left'] = 0
plt.rcParams['figure.subplot.bottom'] = 0
plt.rcParams['figure.subplot.right'] = 1
plt.rcParams['figure.subplot.top'] = 1
plt.rcParams['animation.convert_path'] = r'E:\Program Files\ImageMagick-7.0.11-Q16-HDRI\magick.exe'
def angle_translate(angle):
while angle > 360.0:
angle -= 360.0
while angle < 0.0:
angle += 360.0
return angle
def platform(point, resolution):
high_temp = []
for j in range(resolution + 1):
loc = 0.0
scope = 90.0 * (resolution / 360.0)
dis = 0
for i in range(int(j - scope), int(j + scope)):
i = int(angle_translate(i))
temp = point[2] * cos((i - j) * pi / 180.0)
if temp > loc:
loc = temp
dis = point[2] * sin((i - j) * pi / 180.0)
high_temp.append(, dis])
return high_temp
def refresh_data(num):
global angle_count, angle_scale, resolution, x_transfer, tool_vel, tool_pos
if angle_count >= 360.0:
print(temp)
angle_count = 0
tool_dis = high_arr[int(angle_count + angle_scale)][1]
if fabs(tool_pos - tool_dis) > tool_vel:
if tool_pos > tool_dis:
tool_pos -= tool_vel
else:
tool_pos += tool_vel
else:
tool_pos = tool_dis
angle_count += angle_scale
x_pic_offset = tool_x + tool_pos
y_pic_offset = tool_y + high_arr[int(angle_count)][0]
tool.set_data(x_pic_offset, y_pic_offset)
workpiece.set_data(point[2] * np.sin((point[3] - angle_count + offset_angle) * pi / 180.0),
point[2] * np.cos((point[3] - angle_count + offset_angle) * pi / 180.0))
halftone.set_data([-50, 50], high_arr[int(angle_count)][0])
def modeling(a, b, r, resolution=360):
point = []
# 倒角区域角度
area_angle_l = atan((a / 2.0 - r) / (b / 2)) * 180.0 / pi
area_angle_s = atan((b / 2.0 - r) / (a / 2)) * 180.0 / pi
# 变换比例
if 90.0 - (area_angle_l + area_angle_s) != 0:
scale = 90.0 / (90.0 - (area_angle_l + area_angle_s))
else:
scale = 0
# 参数初始化
x = y = 0
point = []
for i in range(resolution + 1):
angle = i / (resolution / 360)
# 上侧边
if 0 <= angle < area_angle_l or 360 - area_angle_l < angle < 360:
x = (b / 2) * tan(angle * pi / 180)
y = b / 2
# 第一圆弧区
if area_angle_l <= angle < 90 - area_angle_s:
angle_temp = (angle - area_angle_l) * scale
x = (a / 2) - r + (r * sin(angle_temp * pi / 180))
y = (b / 2) - r + (r * cos(angle_temp * pi / 180))
# 右侧边
elif (90 - area_angle_s) <= angle < 90 + area_angle_s:
x = a / 2
y = (a / 2) * tan((90 - angle) * pi / 180)
# 第二圆弧区
elif 90 + area_angle_s <= angle < 180 - area_angle_l:
angle_temp = (angle - 90 - area_angle_s) * scale + 90
x = (a / 2) - r + (r * sin(angle_temp * pi / 180))
y = -((b / 2) - r) + (r * cos(angle_temp * pi / 180))
# 下侧边
elif 180 - area_angle_l <= angle < 180 + area_angle_l:
x = -(b / 2) * tan(angle * pi / 180)
y = - b / 2
# 第三圆弧区
elif 180 + area_angle_l <= angle < 270 - area_angle_s:
angle_temp = (angle - 180 - area_angle_l) * scale + 180
x = -((a / 2) - r) + (r * sin(angle_temp * pi / 180))
y = -((b / 2) - r) + (r * cos(angle_temp * pi / 180))
# 左侧边
elif 270 - area_angle_s <= angle < 270 + area_angle_s:
x = -a / 2
y = -(a / 2) * tan((270 - angle) * pi / 180)
# 第四圆弧区
elif 270 + area_angle_s <= angle < 360 - area_angle_l:
angle_temp = (angle - 270 - area_angle_s) * scale + 270
x = -(a / 2.0) + r + (r * sin(angle_temp * pi / 180))
y = (b / 2.0) - r + (r * cos(angle_temp * pi / 180))
radius = sqrt(x ** 2.0 + y ** 2.0)
point.append(, y, radius, angle])
return point
# 长度
a = 15
# 深度
b = 15
# 倒角
r = 4
resolution = 360
angle_scale = 360.0 / resolution
angle_count = 0
offset_angle = 0
distance = 0
x_transfer = 0
tool_vel = 0.4
tool_pos = 0
step = 0
point = modeling(a, b, r, resolution)
point = np.array(point)
point = np.hsplit(point, 4)
high_arr = platform(point, resolution)
fig, ax = plt.subplots()
ax.set_xlim(-a, a)
ax.set_ylim(-a, a)
workpiece, = ax.plot([], [], 'gray')
tool, = ax.plot([], [], 'green')
tool_x = np.array([-1.0, 0.0, 1.0, -1.0])
tool_y = np.array([4.0, 0.0, 4.0, 4.0])
halftone, = ax.plot([], [], 'blue')
ani = animation.FuncAnimation(fig=fig, func=refresh_data, frames=425, interval=1)
# Writer = animation.ImageMagickFileWriter()
# Writer.fps = 30
# ani.save('h.gif', writer=Writer,)
plt.grid()
plt.show() 第一次见,不明觉历,MARK 看着三角函数就头疼
页:
[1]