I need help with this code
1 次查看(过去 30 天)
显示 更早的评论
Good evening, I made this code that represents a colorful Rubiks cube in matlab using my own multidimensional array, now the problem is that the figure is a cube with 6 faces but each face is not divided into 4 pparts like the real rubiks cube.
this is the code:
A = [1 2 3; 4 5 6; 7 8 9; 10 11 12];
A(:,:,2) = [13 14 15; 16 17 18; 19 20 21; 22 23 24];
B = cat(3, A, [25 26 27; 28 29 30; 31 32 33; 34 35 36]);
B(:,:,4) = [37 38 39; 40 41 42; 43 44 45; 46 47 48];
B(:,:,5) = [49 50 51; 52 53 54; 55 56 57; 58 59 60];
B(:,:,6) = [61 62 63; 64 65 66; 67 68 69; 70 71 72];
PocketCube = B;
figure;
facesColor = {[1 0.5 0], [1 1 1], [1 0 0], [1 1 0], [0 0 1], [0 1 0]};
facesVertices = PocketCube;
facesVertices(:,:,1) = [0 0 0; 1 0 0; 1 1 0; 0 1 0];
facesVertices(:,:,2) = [1 0 0; 1 0 1; 1 1 1; 1 1 0];
facesVertices(:,:,3) = [0 0 1; 1 0 1; 1 1 1; 0 1 1];
facesVertices(:,:,4) = [0 0 0; 0 0 1; 0 1 1; 0 1 0];
facesVertices(:,:,5) = [0 1 0; 1 1 0; 1 1 1; 0 1 1];
facesVertices(:,:,6) = [0 0 0; 1 0 0; 1 0 1; 0 0 1];
for i = 1:6
faceColor = facesColor{i};
vertices = facesVertices(:,:,i);
patch('Vertices', vertices, 'Faces', [1 2 3 4], 'FaceColor', faceColor, 'EdgeColor', 'k', 'LineWidth', 2);
hold on;
grid on;
end
axis equal;
axis off;
view(3);
I tried so many techniques and codes to divide each face into 4 sections but nothing seems to work. Please if anyone knows how to proceed, let me know
0 个评论
回答(1 个)
Chunru
2023-12-5
figure;
facesColor = {[1 0.5 0], [1 1 1], [1 0 0], [1 1 0], [0 0 1], [0 1 0]};
acesVertices(:,:,1) = [0 0 0; 1 0 0; 1 1 0; 0 1 0];
facesVertices(:,:,2) = [1 0 0; 1 0 1; 1 1 1; 1 1 0];
facesVertices(:,:,3) = [0 0 1; 1 0 1; 1 1 1; 0 1 1];
facesVertices(:,:,4) = [0 0 0; 0 0 1; 0 1 1; 0 1 0];
facesVertices(:,:,5) = [0 1 0; 1 1 0; 1 1 1; 0 1 1];
facesVertices(:,:,6) = [0 0 0; 1 0 0; 1 0 1; 0 0 1];
%You have 2x2x2 cubes instead of 1
for x=1:2
for y=1:2
for z=1:2
for i = 1:6
faceColor = facesColor{i};
vertices = facesVertices(:,:,i) + [x-1 y-1 z-1]; % specify color according to your requirement
patch('Vertices', vertices, 'Faces', [1 2 3 4], 'FaceColor', faceColor, 'EdgeColor', 'k', 'LineWidth', 2);
hold on;
grid on;
end
end
end
end
axis equal;
axis off;
view(3);
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!