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)
Rich on Old age pains: Injuries from sleeping: yes, me...
Dave on Wedding investment: Well obviously stay away from...
Rus on Wedding investment: Can you advise us on...
Someone who is not young but not old on Old age pains: Regarding Paragraph 1 Sentence 2,...
Bobby on Blackberry vs. black raspberry: I grew up in Olney,...
Richi near Frankfurt/M. on WinXP .mov thumbnails: Thanks Dude, that solved my...
saphrenne on Strummit From The Summit: I love this song so...
Rosalyne on Blackberry vs. black raspberry: I grew up eating BlackCaps...
BONNIE on Glitter cards: thats awsome... i laugh out...
Christy on WinXP .mov thumbnails: was skeptical at first... Windows...