画三维小车,先给上几个用到的函数
画轮盘
function[XD,YD,ZD] = CarDisc(a,b)
discr= linspace(0,b-a,7);
th= linspace(0,2*pi,16);
XD=discr'*cos(th);
YD=discr'*sin(th);
ZD=zeros(7,16);
end
画轮环
function [x,y,z]=cartire(a,b)
r=linspace(b-a,b+a,10);
th=linspace(0,2*pi,22);
xx=r'*cos(th);
yy=r'*sin(th);
zz=real(sqrt(a^2-(sqrt(xx.^2+yy.^2)-b).^2));
x=[xx xx];
y=[yy yy];
z=[zz -zz];
end
画轮轴
function [XC,YC,ZC]=CarShaft(a,b,L2)
th=linspace(0,2*pi,16);
[XC,ZC]=meshgrid(0.3*(b-a)*cos(th),[-L2,L2]);
YC=meshgrid(0.3*(b-a)*sin(th),[-L2,L2]);
end
自由变换
function [Xrt,Yrt,Zrt] = EulerAngles1(psi,chi,phi,Lx,Ly,Lz,x,y,z)
R=[];
R(1)=cos(psi) * cos(chi);R(2)=-cos(psi) * sin(chi);R(3)=sin(psi);
R(4)=cos(phi) * sin(chi) + sin(phi) * sin(psi) * cos(chi);R(5)=cos(phi) * cos(chi) - sin(phi) * sin(psi) * sin(chi);R(6)=- sin(phi)* cos(psi);
R(7)=sin(phi) * sin(chi) - cos(phi) * sin(psi) * cos(chi);R(8)=sin(phi) * cos(chi) + cos(phi) * sin(psi) * sin(chi);R(9)=cos(phi)*cos(psi);
%以上为ppt中矩阵九个值
Xrt=R(1)*x+R(2)*y+R(3)*z+Lx;
Yrt=R(4)*x+R(5)*y+R(6)*z+Ly;
Zrt=R(7)*x+R(8)*y+R(9)*z+Lz;
end
主程序
clc;clear;close all;
%创建分块图布局,要画两个图因此有两个布局
tiledlayout(1,2);
% 定义大小径
r = 1; R = 6;
% 生成4个车轮环
[xtire, ytire, ztire] = cartire(r, R);
[xT1, yT1, zT1] = EulerAngles1(0, 0, 0, -37, 29 * sqrt(3), 0, ztire, ytire, xtire);
[xT2, yT2, zT2] = EulerAngles1(0, 0, 0, 37, 29 * sqrt(3), 0, ztire, ytire, xtire);
[xT3, yT3, zT3] = EulerAngles1(0, 0, 0, -37, -29 * sqrt(3), 0, ztire, ytire, xtire);
[xT4, yT4, zT4] = EulerAngles1(0, 0, 0, 37, -29 * sqrt(3), 0, ztire, ytire, xtire);
% 生成4个车轮盘
[xdisc, ydisc, zdisc] = CarDisc(r, R);
[xD1, yD1, zD1] = EulerAngles1(0, 0, 0, -37, 29 * sqrt(3), 0, zdisc, ydisc, xdisc);
[xD2, yD2, zD2] = EulerAngles1(0, 0, 0, 37, 29 * sqrt(3), 0, zdisc, ydisc, xdisc);
[xD3, yD3, zD3] = EulerAngles1(0, 0, 0, -37, -29 * sqrt(3), 0, zdisc, ydisc, xdisc);
[xD4, yD4, zD4] = EulerAngles1(0, 0, 0, 37, -29 * sqrt(3), 0, zdisc, ydisc, xdisc);
% 生成两根轮轴
[xshaft, yshaft, zshaft] = CarShaft(r, R, 37);
[xSH1, ySH1, zSH1] = EulerAngles1(0, 0, 0, 0, 29 * sqrt(3), 0, zshaft, yshaft, xshaft);
[xSH2, ySH2, zSH2] = EulerAngles1(0, 0, 0, 0, -29 * sqrt(3), 0, zshaft, yshaft,xshaft);
% 生成两根竖轴
[xver, yver, zver] = CarShaft(r, R, 29 * sqrt(3));
[xVA1, yVA1, zVA1] = EulerAngles1(0, 0, 0, 29, 0, 0, xver, zver, yver);
[xVA2, yVA2, zVA2] = EulerAngles1(0, 0, 0, -29, 0, 0, xver, zver, yver);
% 生成两根斜轴
[xobl, yobl, zobl] = CarShaft(r, R, 50);
[xOA1, yOA1, zOA1] = EulerAngles1(0, pi / 6, 0, 0, 0, 0, xobl, zobl, yobl);
[xOA2, yOA2, zOA2] = EulerAngles1(0, -pi / 6, 0, 0, 0, 0, xobl, zobl, yobl);
nexttile
surf(xT1, yT1, zT1), hold on
surf(xT2, yT2, zT2), hold on
surf(xT3, yT3, zT3), hold on
surf(xT4, yT4, zT4), hold on
surf(xD1, yD1, zD1), hold on
surf(xD2, yD2, zD2), hold on
surf(xD3, yD3, zD3), hold on
surf(xD4, yD4, zD4), hold on
surf(xSH1, ySH1, zSH1), hold on
surf(xSH2, ySH2, zSH2), hold on
surf(xVA1, yVA1, zVA1), hold on
surf(xVA2, yVA2, zVA2), hold on
surf(xOA1, yOA1, zOA1), hold on
surf(xOA2, yOA2, zOA2), title('surf')
axis equal
nexttile
mesh(xT1, yT1, zT1), hold on
mesh(xT2, yT2, zT2), hold on
mesh(xT3, yT3, zT3), hold on
mesh(xT4, yT4, zT4), hold on
mesh(xD1, yD1, zD1), hold on
mesh(xD2, yD2, zD2), hold on
mesh(xD3, yD3, zD3), hold on
mesh(xD4, yD4, zD4), hold on
mesh(xSH1, ySH1, zSH1), hold on
mesh(xSH2, ySH2, zSH2), hold on
mesh(xVA1, yVA1, zVA1), hold on
mesh(xVA2, yVA2, zVA2), hold on
mesh(xOA1, yOA1, zOA1), hold on
mesh(xOA2, yOA2, zOA2), title('mesh')
axis equal