Clear Filters
Clear Filters

Creating a 3d diagram using multiple matrix

8 views (last 30 days)
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

Accepted Answer

darova
darova on 9 Feb 2021
Edited: darova on 9 Feb 2021
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 Comment
Conner Carriere
Conner Carriere on 9 Feb 2021
Edited: Conner Carriere on 9 Feb 2021
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?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!