本文主要是介绍MATLAB——patch绘制长方体(cuboid)+长方体平移+长方体旋转,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 围绕着中心点旋转
- 代码
- 效果
- 围绕原点旋转
- 代码
- 效果
- 区别
- 其他方法
围绕着中心点旋转
代码
-
新建一个脚本命名为:drawCuboid.m
function [CuboidHandle, verts, facs] = drawCuboid(varargin) % Draw Cuboid % Draw a Cuboid using 8 rectangular faces. Places Cuboid Into Current Figure % % Inputs (SL, CV, EA, colr, alph) % ---------------- % SL - [X;Y;Z] Length of Cuboid Side (SL - SideLength) % CV - [X;Y;Z] Center of volume % EA - [Yaw(Z-axis);Pitch(y-axis);Roll(x-axis)] Euler/Rotation angles [radians] % colr - Color of cuboid; string (ex. 'r','b','g') or vector [R G B] % alph - Alpha transparency value of cuboid % % Outputs (CuboidHandle, verts, facs) % ---------------- % CuboidHandle = Handle for Patch Object % verts = 3x8 XYZ Vertices % facs = 6x4 Order of faces % %% Number of inputs and assign defaults if not specified % Define Default Values SL = [1;1;1]; CV = [0;0;0]; EA = [0;0;0]; colr = [0.5 0.5 0.5]; alph = 0.5; switch nargincase 0% All Inputs Empty Using Default Valuescase 1SL = varargin{1};case 2SL = varargin{1}; CV = varargin{2};case 3SL = varargin{1}; CV = varargin{2}; EA = varargin{3};case 4SL = varargin{1}; CV = varargin{2}; EA = varargin{3}; colr = varargin{4};case 5SL = varargin{1}; CV = varargin{2}; EA = varargin{3}; colr = varargin{4}; alph = varargin{5};otherwiseerror('Invalid number of inputs') end %% Form ZYX Rotation Matrix % Calculate Sines and Cosines c1 = cos(EA(1)); s1 = sin(EA(1)); c2 = cos(EA(2)); s2 = sin(EA(2)); c3 = cos(EA(3)); s3 = sin(EA(3)); % Calculate Matrix R = [c1*c2 -c2*s1 s2c3*s1+c1*s2*s3 c1*c3-s1*s2*s3 -c2*s3s1*s3-c1*c3*s2 c3*s1*s2+c1*s3 c2*c3]'; %% Create Vertices x = 0.5*SL(1)*[-1 1 1 -1 -1 1 1 -1]'; y = 0.5*SL(2)*[1 1 1 1 -1 -1 -1 -1]'; z = 0.5*SL(3)*[-1 -1 1 1 1 1 -1 -1]'; %% Create Faces facs = [1 2 3 45 6 7 84 3 6 53 2 7 62 1 8 71 4 5 8]; %% Rotate and Translate Vertices verts = zeros(3,8); for i = 1:8verts(1:3,i) = R*[x(i);y(i);z(i)]+CV; end %% Draw Patch Object CuboidHandle = patch('Faces',facs,'Vertices',verts','FaceColor',colr,'FaceAlpha',alph); end
-
新建另外一个脚本,输入:
drawCuboid([2;2;2],[2;2;2],[30/180*pi;0;0],'g',0.1) drawCuboid([2;2;2],[2;2;2],[0;0;0],'b',0.1)
效果
下面两幅图中绿色立方体为原始立方体,紫色立方体为绕立方体中心顺时针旋转30°后的立方体。
图1 俯视图
图2 旋转图
围绕原点旋转
代码
- 新建一个脚本命名为:drawCuboid.m
function [CuboidHandle, verts, facs] = drawCuboid(varargin) % Draw Cuboid % Draw a Cuboid using 8 rectangular faces. Places Cuboid Into Current Figure % % Inputs (SL, CV, EA, colr, alph) % ---------------- % SL - [X;Y;Z] Length of Cuboid Side (SL - SideLength) % CV - [X;Y;Z] Center of volume % EA - [Yaw(Z-axis);Pitch(y-axis);Roll(x-axis)] Euler/Rotation angles [radians] % colr - Color of cuboid; string (ex. 'r','b','g') or vector [R G B] % alph - Alpha transparency value of cuboid % % Outputs (CuboidHandle, verts, facs) % ---------------- % CuboidHandle = Handle for Patch Object % verts = 3x8 XYZ Vertices % facs = 6x4 Order of faces % %% Number of inputs and assign defaults if not specified % Define Default Values SL = [1;1;1]; CV = [0;0;0]; EA = [0;0;0]; colr = [0.5 0.5 0.5]; alph = 0.5; switch nargincase 0% All Inputs Empty Using Default Valuescase 1SL = varargin{1};case 2SL = varargin{1}; CV = varargin{2};case 3SL = varargin{1}; CV = varargin{2}; EA = varargin{3};case 4SL = varargin{1}; CV = varargin{2}; EA = varargin{3}; colr = varargin{4};case 5SL = varargin{1}; CV = varargin{2}; EA = varargin{3}; colr = varargin{4}; alph = varargin{5};otherwiseerror('Invalid number of inputs') end %% Form ZYX Rotation Matrix % Calculate Sines and Cosines c1 = cos(EA(1)); s1 = sin(EA(1)); c2 = cos(EA(2)); s2 = sin(EA(2)); c3 = cos(EA(3)); s3 = sin(EA(3)); % Calculate Matrix R = [c1*c2 -c2*s1 s2c3*s1+c1*s2*s3 c1*c3-s1*s2*s3 -c2*s3s1*s3-c1*c3*s2 c3*s1*s2+c1*s3 c2*c3]'; %% Create Vertices x = 0.5*SL(1)*[-1 1 1 -1 -1 1 1 -1]'; y = 0.5*SL(2)*[1 1 1 1 -1 -1 -1 -1]'; z = 0.5*SL(3)*[-1 -1 1 1 1 1 -1 -1]'; %% Create Faces facs = [1 2 3 45 6 7 84 3 6 53 2 7 62 1 8 71 4 5 8]; %% Rotate and Translate Vertices verts = zeros(3,8); for i = 1:8verts(1:3,i) = R*[x(i);y(i);z(i)]+R*CV; end %% Draw Patch Object CuboidHandle = patch('Faces',facs,'Vertices',verts','FaceColor',colr,'FaceAlpha',alph); end
- 新建另外一个脚本,输入:
drawCuboid([2;2;2],[2;2;2],[30/180*pi;0;0],'g',0.1) drawCuboid([2;2;2],[2;2;2],[0;0;0],'b',0.1)
效果
下面两幅图中绿色立方体为原始立方体,紫色立方体为原点(0,0,0),顺时针旋转30°后的立方体。
图1 俯视图
图2 旋转图
区别
绕立方体中心旋转时计算公式如下,其中R是旋转矩阵
for i = 1:8verts(1:3,i) = R*[x(i);y(i);z(i)]+CV;end
绕原点旋转时计算公式如下,其中R是旋转矩阵
for i = 1:8verts(1:3,i) = R*[x(i);y(i);z(i)]+R*CV;end
其他方法
还可以使用MATLAB提供的multicuboid函数画立方体,但是不太清楚如何实现平移、旋转等变换。详情请看资料multicuboid函数。
这篇关于MATLAB——patch绘制长方体(cuboid)+长方体平移+长方体旋转的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!