I need help with this code

조회 수: 2 (최근 30일)
Redwan
Redwan 2023년 12월 4일
댓글: Redwan 2023년 12월 5일
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

답변 (1개)

Chunru
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);
  댓글 수: 1
Redwan
Redwan 2023년 12월 5일
thanks alot, it helped

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Rubik's Cube에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by