Cube rotation solid line in plot missing

1 次查看(过去 30 天)
I am trying to get cube roatation.. the code is as follows: What I am getting as output is a plot which is attached here. Now, the plot should be the full cube. So in Orignal and New one must have all edges of cube. I am not getting what I am doing wrong in the code. Please help.
p1=[-1,1,1];
p2=[1,1,1];
p3=[1,-1,1];
p4=[-1,-1,1];
p5=[-1,1,-1];
p6=[1,1,-1];
p7=[1,-1,-1];
p8=[-1,-1,-1];
c=[p1' p2' p3' p4' p5' p6' p7' p8';1 1 1 1 1 1 1 1 ];
tx=1;
ty=1;
tz=1;
T1=[1 0 0 tx;0 1 0 ty;0 0 1 tz;0 0 0 1];
a=45;
r1=[cosd(a) -sind(a) 0 0; sind(a) cosd(a) 0 0;0 0 1 0;0 0 0 1];
T2=inv(T1);
m=T2*r1*T1;
d=m*c;
fprintf('new vertices:\n');
for i=1:8
fprintf(('%2f,%2f,%2f)\n'),d(1,1),d(2,1),d(3,i));
end;
%ploting
c=[p1' p2' p3' p4' p5' p6' p7' p8' p5' p4' p3' p7' p6' p2';1 1 1 1 1 1 1 1 1 1 1 1 1 1];
d=m*c;
plot3(c(1,:),c(2,:),c(3,:),'b');
hold on;
plot3(d(1,:),d(2,:),d(3,:),'r');
xlabel('x');
ylabel('y');
zlabel('z');
legend('orginal','new');
axis equal;
grid;
hold off

回答(1 个)

Poorna
Poorna 2024-4-1
Hi Mohit,
I see that you are trying to plot a cube parallel to the axes and then rotate the cube by 45 degrees along the line parallel to z-axis passing through x = -1 and y = -1.
Although the rotation logic is correct the reason you couldn't see all edges of the cube is because of the way you plot the cube. The below code will plot a line plot connecting adjancent points as ordered in c.
c=[p1' p2' p3' p4' p5' p6' p7' p8' p5' p4' p3' p7' p6' p2';1 1 1 1 1 1 1 1 1 1 1 1 1 1];
plot3(c(1,:),c(2,:),c(3,:),'b')
This method, however, doesn't fully represent a cube since it misses plotting some of the line segments that define the cube's structure.
To correctly visualize the cube, you should plot each edge distinctly. This can be achieved by separately plotting the top and bottom faces, and then connecting them with the four edges (pillars) that complete the cube's form as shown below
%plot the top face
c=[p1' p2' p3' p4' p1' ; 1 1 1 1 1];
plot3(c(1,:),c(2,:),c(3,:),'b');
hold on;
%plot the bottom face
c = [p5' p6' p7' p8' p5' ; 1 1 1 1 1];
hold on;
plot3(c(1,:),c(2,:),c(3,:),'b');
%plot the remaining 4 pillar edges that connect the top and bottom faces.
c = [p1' p5' ; 1 1];
hold on;
plot3(c(1,:),c(2,:),c(3,:),'b');
c = [p2' p6' ; 1 1];
hold on;
plot3(c(1,:),c(2,:),c(3,:),'b');
c = [p3' p7' ; 1 1];
hold on;
plot3(c(1,:),c(2,:),c(3,:),'b');
c = [p4' p8' ; 1 1];
hold on;
plot3(c(1,:),c(2,:),c(3,:),'b');
You could do the same for the rotated cube.
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by