还是先把Python代码做出来,scratch的积木等到周末慢慢搭
应该还是比较好理解的
def project_3d_to_2d(
x, y, z, # 物体原始坐标
rx, ry, rz, # X/Y/Z轴旋转角度(度)
cx, cy, cz, # 旋转中心坐标
k, # 焦距
eps=1e-6 # 防止除零的小量
):
#角度转弧度
theta_x = math.radians(rx)
theta_y = math.radians(ry)
theta_z = math.radians(rz)
# 计算三角函数
sx, cx_rot = math.sin(theta_x), math.cos(theta_x)
sy, cy_rot = math.sin(theta_y), math.cos(theta_y)
sz, cz_rot = math.sin(theta_z), math.cos(theta_z)
# 坐标平移(相对旋转中心)
x_rel = x - cx
y_rel = y - cy
z_rel = z - cz
# 分步三维旋转
# 绕X轴旋转
y1 = y_rel \* cx_rot - z_rel \* sx
z1 = y_rel \* sx + z_rel \* cx_rot
x1 = x_rel # X轴旋转后x不变
# 绕Y轴旋转
x2 = x1 \* cy_rot + z1 \* sy
z2 = -x1 \* sy + z1 \* cy_rot
y2 = y1# Y轴旋转后y不变
# 第三步:绕Z轴旋转
x3 = x2 \* cz_rot - y2 \* sz
y3 = x2 \* sz + y2 \* cz_rot
z3 = z2 # Z轴旋转后z不变
# 坐标还原
x_world = cx + x3
y_world = cy + y3
z_world = cz + z3
#透视投影
if abs(z_world) < eps:
z_world = eps if z_world >= 0 else -eps
proj_x = (x_world \* k) / z_world
proj_y = (y_world \* k) / z_world
return proj_x, proj_y