image thumbnail

Simple Draw Helix Spring 3D Rotate Translate Transform Dilate Save JPG




Simple Draw Helix Spring 3D Rotate Translate Transform Dilate Save JPG

close all; clear all
disp 'Demonstrates 3D rotation translation dilation'
disp 'draw any number of helix or spring in 3D'
disp 'save output image as jpg file'
disp 'Uploaded December 21, 2011'
disp 'Author: Amitava Biswas, PhD, FIEI, FIETE'
disp 'The University of Texas at El Paso'
disp ' Phone:(915)747-8307'

% change following choices as needed
turns= 15; % How many turns in each spring?
radiu= 2; % Coil radius of each spring? 
creat= 2; % How many springs in the box? 
mybox= 20; % Overall box size? 

t = 0:0.001:1; % each spring is 1001 points in space
% build a master spring along z-axis from [0 0 0] to [0 0 1] in 3D
xs=radiu*sin(t*2*pi*turns); % x-coordinates
ys=radiu*cos(t*2*pi*turns); % y-coordinates
zs=t; % z-coordinates

% create arbitrary end points in mybox to project the master spring
q=mybox*rand(creat,3,2); % number of springs, xyz, two endpoints
x1=q(:,1,1); % x-coordinates of one end of springs 
x2=q(:,1,2); % x-coordinates of other end of springs
y1=q(:,2,1); % y-coordinates of one end of springs 
y2=q(:,2,2); % y-coordinates of other end of springs
z1=q(:,3,1); % z-coordinates of one end of springs
z2=q(:,3,2); % z-coordinates of other end of springs

for k=1:creat % each spring at a time 
    % log xyz coordinates of two end points for this spring
    disp(['[' num2str(q(k,:,1),'% 4.1f') '] - [' num2str(q(k,:,2),' %4.1f') ']'])
    dx=x2(k)-x1(k); % extent of this spring along x-axis
    dy=y2(k)-y1(k); % extent of this spring along y-axis
    dz=z2(k)-z1(k); % extent of this spring along z-axis
    ds=sqrt(dx^2+dy^2+dz^2); % needed length of this spring
    % dz=dz*ds; % stretch the master spring as much as necessary
    % rotate the master spring towards the needed orientation
    p=[xs' ys' zs'*ds]*vrrotvec2mat(vrrotvec([dx dy dz],[0 0 1]));
    sx=p(:,1)+x1(k); % shift x-coordinates
    sy=p(:,2)+y1(k); % shift y-coordinates
    sz=p(:,3)+z1(k); % shift z-coordinates
    plot3(sx,sy,sz,'linewidth',2,'color', rand(1,3))
    hold on % to add next spring
    % change linewidth if necessary
    % change color if necessary

az=30; % azimuth view degrees
el=30; % elevation view degrees
xmin=0; xmax=mybox; ymin=0; ymax=mybox; zmin=0; zmax=mybox;
axis([xmin xmax ymin ymax zmin zmax])
axis on % may be off
grid on % may be off
box on % may be off

xlabel('x-axis') % change if necessary
ylabel('y-axis') % change if necessary
zlabel('z-axis') % change if necessary
title('Spring Demo by Amitava Biswas') % change if necessary

disp('Select menu-tools-rotate then use mouse interactively!!!')
disp('You can also brush, pan, zoom etc by mouse!!!')

print('-r600', '-djpeg', 'Biswas47') % change if necessary

disp('pretty easy and fun, no?')

Contact us