ddhr.org
Code - box3d Tuesday, Apr 14, 2009 4:41 pm

This is a simple Matlab function that draws a 3-dimensional rectangular solid object. 

function box3d(o,x,y,z,a,b,g,c)
%BOX3D   Draw 3D box.
%   Draws a solid, 3-dimensional rectangular object based on a
%   center point o, length x, width y, height z, roll angle a
%   (about x), pitch angle b (about y), yaw angle g (about z),
%   and color c. Rotation angles are about the center point.
%
%   Version:  0.3
%
%   Example:
%      box3d([12 30 57],3,7,5,10,15,-25,'g')

p = [o(1)+x/2 o(2)+y/2 o(3)+z/2;...
     o(1)-x/2 o(2)+y/2 o(3)+z/2;...
     o(1)-x/2 o(2)-y/2 o(3)+z/2;...
     o(1)+x/2 o(2)-y/2 o(3)+z/2;...
     o(1)+x/2 o(2)+y/2 o(3)-z/2;...
     o(1)-x/2 o(2)+y/2 o(3)-z/2;...
     o(1)-x/2 o(2)-y/2 o(3)-z/2;...
     o(1)+x/2 o(2)-y/2 o(3)-z/2];

rol = [1 0 0; 0 cosd(a) sind(a); 0 -sind(a) cosd(a)];
pit = [cosd(b) 0 -sind(b); 0 1 0; sind(b) 0 cosd(b)];
yaw = [cosd(g) sind(g) 0; -sind(g) cosd(g) 0; 0 0 1];

q = ((p-ones(8,1)*o)*rol*pit*yaw) + (ones(8,1)*o);

f = [1 2 3 4; 5 6 7 8; 1 4 8 5;...
     1 2 6 5; 2 3 7 6; 3 4 8 7];

for i = 1:length(f)
    fill3(q(f(i,:),1),q(f(i,:),2),q(f(i,:),3),c)
    hold on
end

Download

Linked:  Code

Hello
Hi, my name is Dave Hosier, and this website is where I write my unfounded opinions on trivial matters. Feel free to look around, but please refrain from reading anything.

about | contact | feed

Comments (feed)
Dave on Cubeicals wine rack: I ended up cutting a...
Harry on Cubeicals wine rack: So Dave, how did you...
Emily on Constant vs. average speed: I can give you sun...
Dave on Accents that annoy me: I'm pretty much saying I...
Wendy on Accents that annoy me: Are you passively aggressively saying...
JSW on Accents that annoy me: KInda like I can't tell...
Dave on Accents that annoy me: Understood, but my untrained ears...
Dave on Constant vs. average speed: I don't think so.  There...
JSW on Accents that annoy me: I realize with you being...
Emily on Constant vs. average speed: You don't think the beaches...