MATLAB Answers

0

write a function to translate a 3d object

Asked by Jiaoying Ren on 27 Aug 2019
Latest activity Commented on by Jiaoying Ren on 27 Aug 2019
Help!
So I have this assignment that ask me to write a function to translate this object
load tetmesh
TR = triangulation(tet,X);
[FBtri,FBpoints] = freeBoundary(TR);
T = FBtri;
S = FBpoints' - [zeros(1,1079);zeros(1,1079);40*ones(1,1079)];
to show the object, use the code given by
function show(T,S)
trisurf(T,S(1,:),S(2,:),S(3,:),'FaceColor','cyan','FaceAlpha', 0.8);
axis([-120 120 -120 120 -120 120])
xlabel('x')
ylabel('y')
zlabel('z')
end
the first part of my assignment is "Write a function shift.m which takes as input a 3 × n matrix S (for example, as obtained from running setup.m), a scalar dist which gives the distance that the object should translate, and a string ’x’, ’y’, or ’z’ which tells which axis you want to translate along. "
My attempt is
function shift(S,dist,axis)
n = size(S,2);
B = ones(1,n);
if axis == 'x'
S = S + [dist;0;0]*B; % move along x-axis
elseif axis =='y'
S = S + [0;dist;0]*B; % move along y-axis
else
S = S + [0;0;dist] * B; % move along z-axis
end
end
However, this doesn't quite work because I got this error message when I was trying it out.
>> show2(T,shift(S,80,'y'))
Error using shift
Too many output arguments.
So I'm just wondering where I did wrong and how I can fix it. Any help is greatly appreciated!
Thx in advance!

  0 Comments

Sign in to comment.

1 Answer

Answer by James Tursa
on 27 Aug 2019
 Accepted Answer

You didn't write your function to return an output. Try this:
function S = shift(S,dist,axis)

  1 Comment

It works! Thank you so much! I'm actually also stuck on the 2nd part of the question, if you can shed some light that 'd be awesome! for the 2nd part of the question, it asks to write a script The three objects start on top of each other at their default position.
• One object translates by increasing amounts in the +x direction, and one translates by the same magnitudes in the −x direction, until they are a distance of 80 away from their original position.
• The object which was translated in the −x direction should now translate by increasing amounts in the +z direction up to a height of 80. Simultaneously, the object which was translated in the +x direction should translate by the same magnitudes in the −z direction down to z = −80. These translations in the z direction should occur with the objects maintaining their new x coordinate.
So basically, I need to have three objects on the same figure, two of which are shifted.....
I just don't know how to continue to shift an object to one direction after shifting it to a different one.
the hint on the hw is:
for counter = start:increment:stop
show2(T,S)
hold on
show2(T,shift(S,...);
show2(T,shift(S,...));
pause(0.05)
clf
end
and my attempt is:
for counter = start:increment:stop
show2(T,S)
hold on
show2(T,shift(S,counter,'x'));
show2(T,shift(S,-80,'x'));
pause(0.05)
clf
end
which is not much.... because I'm really stuck and I don't even really understand the hint tbh.....
if you can help with this question, I'd be really grateful. but I'm super grateful already, thank you for your help on the 1st part of the question!

Sign in to comment.