Creating a 3d diagram using multiple matrix

6 次查看(过去 30 天)
I am trying to plot a rubiks cube, I currently have a 9x2 matrix with the coordinates of the peices. I have a seperate 9x3 matrix that has the color in RGB triplets.
I am trying to find a way to plot this, then eventually I would like to be able to watch it solve a cube.
I have this code here, which uses patch() to create a 3d cube, for some reason, only the top and bottom show up though.
axisLim=5;
figure
hold on
for p = 1:9; %White Face D
x = MwCoords(p,1);
y = MwCoords(p,2);
patch([x,x,x-1,x-1],[y-2,y-3,y-3,y-2],[0,0,0,0], MwC(p,:))
end
for p = 1:9; %Yellow Face U
x = MyCoords(p,1);
y = MyCoords(p,2);
patch([x,x,x-1,x-1],[y-2,y-3,y-3,y-2],[3,3,3,3], MyC(p,:))
end
for p = 1:9; %Orange Face F
y = MoCoords(p,1);
z = MoCoords(p,2);
patch([0,0,0,0],[y-3,y-3,y-2,y-2],[z,z,z,z], MoC(p,:))
end
for p = 1:9; %Red Face F
y = MrCoords(p,1);
z = MrCoords(p,2);
patch([3,3,3,3],[y-3,y-3,y-2,y-2],[z,z,z,z], MrC(p,:))
end
for p = 1:9; %Blue Face F
x = MbCoords(p,1);
y = MbCoords(p,2);
patch([x-1,x-1,x,x],[-2,-2,-2,-2],[y,y,y,y], MbC(p,:))
end
for p = 1:9; %Green Face F
x = MgCoords(p,1);
y = MgCoords(p,2);
patch([x-1,x-1,x,x],[1,1,1,1],[y,y,y,y], MgC(p,:))
end
hold off
You can just use this MwC for the colors, as an example
MwC =
1 1 1
1 0 1
0 1 0
0 0 1
1 1 1
1 0 0
1 0 0
1 0 0
1 1 0
Here are the all the coordinates
MwCoords =
1 3
1 2
1 1
2 1
2 2
2 3
3 1
3 2
3 3
MyCoords =
1 3
1 2
1 1
2 3
2 2
2 1
3 3
3 1
3 2
MoCoords =
1 3
1 2
1 1
2 1
2 2
2 3
3 1
3 3
3 2
MrCoords =
1 1
1 2
1 3
2 3
2 1
2 2
3 1
3 2
3 3
MbCoords =
1 3
1 2
1 1
2 3
2 2
2 1
3 2
3 3
3 1
MgCoords =
1 3
1 2
1 1
2 3
2 1
2 2
3 2
3 3
3 1
Sorry for this being a messy post, please let me know any ideas you may have

采纳的回答

darova
darova 2021-2-9
编辑:darova 2021-2-9
Maybe you will be intersted
  • create surface
[x,y] = meshgrid(1:5,1:4);
surf(x,y,x*0)
50
  • remove unwanted parts
z([1 4 13 16 17 20]) = nan;
  • assign Z coordinate
z([2 3 5 8 9 12:end]) = 1;
  • move X and Y coordinate
[x,y] = meshgrid([0 0 1 1 0],[0 0 1 1]);
  • add for loop
for i = 1:3
for j = 1:3
for k = 1:3
surf(0.9*x+i, 0.9*y+j, 0.9*z+k, 'facecolor', rand(1,3))
end
end
end
  1 个评论
Conner Carriere
Conner Carriere 2021-2-9
编辑:Conner Carriere 2021-2-9
So i am trying the code at the moment, and I am having some issues. I cannot remove unwanted parts with that line you gave. Whenever I apply the next line of code that you provide, nothing happens to the original 3x4 figure, any suggestions?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by