how to connect lines in a 3D plot ?

8 views (last 30 days)
Maitha bin gharib
Maitha bin gharib on 25 Mar 2016
Answered: Mike Garrity on 25 Mar 2016
So i need to plot my robot that consists of a base, platform, and 4 legs. When i use the plot3 function, only one point from the platform is connected to the corresponding point in the base. how can i connect all 4 points from the platform to the base. I basically found the lengths of legs (di(i)) where i =1-4 , and i found the euler angles, leg 1 and 4 have spherical joints ( theta and phi are available) and leg 2 and 3 are revolute (theta only is non-zero). Attached is the figure and you can see how it's only connected from one point to the base.
The code is the following
for i=1:50
t(i)=0.1*i;
P=[0 0 20]';
s=[ 0 0 1];
R1 = [cos(i*0.0174532) 0 sin(i*0.0174532)];
R2 = [0 1 0];
R3 = [-sin(i*0.0174532) 0 cos(i*0.0174532)];
R = [R1; R2; R3];
B1 = [ 15 15 0 ]'; %sph
B2= [-15 15 0]'; %rev
B3 = [-15 -15 0]'; %rev
B4 = [15 -15 0]'; %sph
A1 = [5,14,0]'; %sph
A2 = [-30,30,0]'; %rev
A3 = [-30,-30,0]'; %rev
A4 = [5,-14,0]'; %sph
b1 = P +(R*B1); %sph
b2 = P +(R*B2); %rev
b3 = P +(R*B3); %rev
b4 = P +(R*B4); %sph
%position%
x1(i)= dot((b1 - A1),(b1 - A1));
d1(i)= sqrt(x1(i)); %sph
x2(i)= dot((b2 - A2),(b2 - A2));
d2(i)= sqrt(x2(i)); %rev
x3(i)= dot((b3 - A3),(b3 - A3));
d3(i)= sqrt(x3(i)); %rev
x4(i)= dot((b4 - A4),(b4 - A4));
d4(i)= sqrt(x4(i)); %sph
%angles%
s1 = (b1-A1)/d1(i);
s2 = (b2-A2)/d2(i);
s3 = (b3-A3)/d3(i);
s4 = (b4-A4)/d4(i);
th1(i)=acos(s1(3)); %sph
th2(i)=acos(s2(3)); %rev
th3(i)=acos(s3(3)); %rev
th4(i)=acos(s4(3)); %sph
ss1(i) =dot((b3-A3)/d3(i),[0 1 0]);
ss2(i) =dot((b4-A4)/d4(i),[0 1 0]);
phy1(i) = asin(s1(2)/sin(th1(i)));
phy4(i) = asin(s4(2)/sin(th4(i)));
%animation%
xplot = [5,-30,-30,5,5];
yplot = [14,30,-30,-14,14];
zplot= [ 0 0 0 0 0 ];
x1plot= [d1(i)*sin(th1(i))*cos(phy1(i)) , d2(i)*sin(th2(i))*cos(0) , d3(i)*sin(th3(i))*cos(0) , d4(i)*sin(th4(i))*cos(phy4(i)) , d1(i)*sin(th1(i))*cos(phy1(i))];
y1plot= [b1(2)+d1(i)*sin(th1(i))*sin(phy1(i)) , b2(2)+d2(i)*sin(th1(i))*sin(0) , b3(2)+d3(i)*sin(th1(i))*sin(0) , b4(2)+d4(i)*sin(th4(i))*sin(phy4(i)) , b1(2)+d1(i)*sin(th1(i))*sin(phy1(i))];
z1plot = [d1(i)*cos(th1(i)) , d2(i)*cos(th2(i)) , d3(i)*cos(th3(i)) , d4(i)*cos(th4(i)) , d1(i)*cos(th1(i))];
%plot(x1plot,y1plot)
plot3([xplot,x1plot],[yplot,y1plot],[zplot,z1plot],'linewidth',2)
pause(0.003)
%%%%%%%%%%%%
end

Answers (1)

Mike Garrity
Mike Garrity on 25 Mar 2016
It's only drawing that connection because you didn't "lift the pen" between drawing the two platforms. You haven't told plot3 anything about the connections. You need to spell it out explicitly like this:
plot3([xplot, nan, ...
x1plot, nan, ...
xplot(1), x1plot(1), nan, ...
xplot(2), x1plot(2), nan, ...
xplot(3), x1plot(3), nan, ...
xplot(4), x1plot(4)], ...
[yplot, nan, ...
y1plot, nan, ...
yplot(1), y1plot(1), nan, ...
yplot(2), y1plot(2), nan, ...
yplot(3), y1plot(3), nan, ...
yplot(4), y1plot(4)], ...
[zplot, nan, ...
z1plot, nan, ...
zplot(1), z1plot(1), nan, ...
zplot(2), z1plot(2), nan, ...
zplot(3), z1plot(3), nan, ...
zplot(4), z1plot(4)], ...
'LineWidth',2)
Although I might go with something like this instead:
clf
h = patch(nan,nan,nan,'FaceColor','none','LineWidth',2);
view(3)
for i=1:50
t(i)=0.1*i;
P=[0 0 20]';
s=[ 0 0 1];
R1 = [cos(i*0.0174532) 0 sin(i*0.0174532)];
R2 = [0 1 0];
R3 = [-sin(i*0.0174532) 0 cos(i*0.0174532)];
R = [R1; R2; R3];
B1 = [ 15 15 0 ]'; %sph
B2= [-15 15 0]'; %rev
B3 = [-15 -15 0]'; %rev
B4 = [15 -15 0]'; %sph
A1 = [5,14,0]'; %sph
A2 = [-30,30,0]'; %rev
A3 = [-30,-30,0]'; %rev
A4 = [5,-14,0]'; %sph
b1 = P +(R*B1); %sph
b2 = P +(R*B2); %rev
b3 = P +(R*B3); %rev
b4 = P +(R*B4); %sph
%position%
x1(i)= dot((b1 - A1),(b1 - A1));
d1(i)= sqrt(x1(i)); %sph
x2(i)= dot((b2 - A2),(b2 - A2));
d2(i)= sqrt(x2(i)); %rev
x3(i)= dot((b3 - A3),(b3 - A3));
d3(i)= sqrt(x3(i)); %rev
x4(i)= dot((b4 - A4),(b4 - A4));
d4(i)= sqrt(x4(i)); %sph
%angles%
s1 = (b1-A1)/d1(i);
s2 = (b2-A2)/d2(i);
s3 = (b3-A3)/d3(i);
s4 = (b4-A4)/d4(i);
th1(i)=acos(s1(3)); %sph
th2(i)=acos(s2(3)); %rev
th3(i)=acos(s3(3)); %rev
th4(i)=acos(s4(3)); %sph
ss1(i) =dot((b3-A3)/d3(i),[0 1 0]);
ss2(i) =dot((b4-A4)/d4(i),[0 1 0]);
phy1(i) = asin(s1(2)/sin(th1(i)));
phy4(i) = asin(s4(2)/sin(th4(i)));
%animation%
xplot = [5,-30,-30,5,5];
yplot = [14,30,-30,-14,14];
zplot= [ 0 0 0 0 0 ];
x1plot= [d1(i)*sin(th1(i))*cos(phy1(i)) , ...
d2(i)*sin(th2(i))*cos(0) , ...
d3(i)*sin(th3(i))*cos(0) , ...
d4(i)*sin(th4(i))*cos(phy4(i)) , ...
d1(i)*sin(th1(i))*cos(phy1(i))];
y1plot= [b1(2)+d1(i)*sin(th1(i))*sin(phy1(i)) , ...
b2(2)+d2(i)*sin(th1(i))*sin(0) , ...
b3(2)+d3(i)*sin(th1(i))*sin(0) , ...
b4(2)+d4(i)*sin(th4(i))*sin(phy4(i)) , ...
b1(2)+d1(i)*sin(th1(i))*sin(phy1(i))];
z1plot = [d1(i)*cos(th1(i)) , d2(i)*cos(th2(i)) , ...
d3(i)*cos(th3(i)) , d4(i)*cos(th4(i)) , ...
d1(i)*cos(th1(i))];
verts = [xplot, x1plot; yplot, y1plot; zplot, z1plot];
faces = [1 2; 2 3; 3 4; 4 1; ... % first platform
5 6; 6 7; 7 8; 8 1; ... % second platform
1 5; 2 6; 3 7; 4 8]; % connections
h.Vertices = verts'; % note transpose!
h.Faces = faces;
pause(0.003)
%%%%%%%%%%%%
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!