%% 块定义
% 块八顶点定义(魔方每一个小块上的8个顶点的相对坐标)
blockVertices = [ 1,-1,-1; 1, 1,-1;-1, 1,-1;-1,-1,-1;...
1,-1, 1; 1, 1, 1;-1, 1, 1;-1,-1, 1]*0.95;
% 块六面顶点索引(在上次的基础上优化了一下,用索引表示)
blockFace{1} = [1,2,3,4];
blockFace{2} = [1,2,6,5];
blockFace{3} = [2,3,7,6];
blockFace{4} = [3,4,8,7];
blockFace{5} = [4,1,5,8];
blockFace{6} = [5,6,7,8];
%% 26块中心定义(先用半径1写,完事儿*2,简单快捷,以后想变大直接改)
% 6轴
axisPosList = [0, 0,-1; 1, 0, 0; 0, 1, 0; -1, 0, 0; 0,-1, 0; 0, 0, 1]*2;
% 8角
cornerPosList = [1,-1,-1; 1, 1,-1; -1, 1,-1; -1,-1,-1;...
1,-1, 1; 1, 1, 1; -1, 1, 1; -1,-1, 1]*2;
% 12棱
edgePosList = [1, 0,-1; 0, 1,-1;-1, 0,-1; 0,-1,-1;...
1, 1, 0;-1, 1, 0;-1,-1, 0; 1,-1, 0;
1, 0, 1; 0, 1, 1;-1, 0, 1; 0,-1, 1]*2;
%% 显示一下
figure
plot3(axisPosList(:,1),axisPosList(:,2),axisPosList(:,3),'K*'),hold on
plot3(edgePosList(:,1),edgePosList(:,2),edgePosList(:,3),'bo')
plot3(cornerPosList(:,1),cornerPosList(:,2),cornerPosList(:,3),'r<')
axis equal
axis([-1,1,-1,1,-1,1]*4)
grid on
view([2,1,1])
%% 颜色与不透明度 白 蓝 红 绿 橙 黄
color = {[1,1,1],[0,0.2,0.8],[0.8,0,0],[0,0.8,0.2],...
[1,0.4,0],[1,1,0],[0.1,0.1,0.1]};
colorAlpha = [1,1,1,1,1,1,0.7];
%% 26块6面上色
% 轴块属性初始化
axisBlock = cell(6,1);
for n = 1:6
axisBlock{n}.RotateMatrix = [1,0,0;0,1,0;0,0,1];
axisBlock{n}.position = axisPosList(n,:);
axisBlock{n}.color = ones(1,6)*7;
axisBlock{n}.color(n) = n;
end
% 角块属性初始化
cornerBlock = cell(8,1);
for n = 1:8
cornerBlock{n}.RotateMatrix = [1,0,0;0,1,0;0,0,1];
cornerBlock{n}.position = cornerPosList(n,:);
cornerBlock{n}.color = ones(1,6)*7;
end
cornerBlock{1}.color([1,2,5]) = [1,2,5];
cornerBlock{2}.color([1,2,3]) = [1,2,3];
cornerBlock{3}.color([1,3,4]) = [1,3,4];
cornerBlock{4}.color([1,4,5]) = [1,4,5];
cornerBlock{5}.color([6,2,5]) = [6,2,5];
cornerBlock{6}.color([6,2,3]) = [6,2,3];
cornerBlock{7}.color([6,3,4]) = [6,3,4];
cornerBlock{8}.color([6,4,5]) = [6,4,5];
% 棱块属性初始化
edgeBlock = cell(12,1);
for n = 1:12
edgeBlock{n}.RotateMatrix = [1,0,0;0,1,0;0,0,1];
edgeBlock{n}.position = edgePosList(n,:);
edgeBlock{n}.color = ones(1,6)*7;
end
edgeBlock{1}.color([1,2]) = [1,2];
edgeBlock{2}.color([1,3]) = [1,3];
edgeBlock{3}.color([1,4]) = [1,4];
edgeBlock{4}.color([1,5]) = [1,5];
edgeBlock{5}.color([2,3]) = [2,3];
edgeBlock{6}.color([3,4]) = [3,4];
edgeBlock{7}.color([4,5]) = [4,5];
edgeBlock{8}.color([5,2]) = [5,2];
edgeBlock{9}.color([6,2]) = [6,2];
edgeBlock{10}.color([6,3]) = [6,3];
edgeBlock{11}.color([6,4]) = [6,4];
edgeBlock{12}.color([6,5]) = [6,5];
%魔方块的位置
blockIdx.axisIdx=[1,2,3,4,5,6];
blockIdx.cornerIdx=[1,2,3,4,5,6,7,8];
blockIdx.edgeIdx=[1,2,3,4,5,6,7,8,9,10,11,12];
%%操作
%FRULDBfruldbSMExyz
[blockIdx, axisBlock, cornerBlock, edgeBlock]=operation(6, ...
blockIdx,axisBlock,cornerBlock,edgeBlock)
[blockIdx, axisBlock, cornerBlock, edgeBlock]=operation(12, ...
blockIdx,axisBlock,cornerBlock,edgeBlock)
% 轴块
for n = 1:6
for f = 1:6
V = (axisBlock{n}.position+blockVertices(blockFace{f},:))*axisBlock{n}.RotateMatrix;
C = color{axisBlock{n}.color(f)};
A = colorAlpha(axisBlock{n}.color(f));
h = patch('Faces',[1 2 3 4],'Vertices',V,'FaceColor',C,...
'FaceAlpha',A);
end
end
% 棱
for n = 1:12
for f = 1:6
V = (edgeBlock{n}.position+blockVertices(blockFace{f},:))*edgeBlock{n}.RotateMatrix;
C = color{edgeBlock{n}.color(f)};
A = colorAlpha(edgeBlock{n}.color(f));
h = patch('Faces',[1 2 3 4],'Vertices',V,'FaceColor',C,...
'FaceAlpha',A);
end
end
% 角块
for n = 1:8
for f = 1:6
V = (cornerBlock{n}.position+blockVertices(blockFace{f},:))*cornerBlock{n}.RotateMatrix;
C = color{cornerBlock{n}.color(f)};
A = colorAlpha(cornerBlock{n}.color(f));
h = patch('Faces',[1 2 3 4],'Vertices',V,'FaceColor',C,...
'FaceAlpha',A);
end
end
function R = getRotateMatrix(alpha,beta,gamma)
% alpha,beta,gamma 为1或-1,分别表示顺时针转和逆时针转
alpha = -pi/2*alpha;
beta = -pi/2*beta;
gamma = -pi/2*gamma;
Rx = [1,0,0;0,cos(alpha),-sin(alpha);0,sin(alpha),cos(alpha)];
Ry = [cos(beta),0,sin(beta);0,1,0;-sin(beta),0,cos(beta)];
Rz = [cos(gamma),-sin(gamma),0;sin(gamma),cos(gamma),0;0,0,1];
R = (Rx*Ry*Rz)';
end
function [blockIdx, axisBlock, cornerBlock, edgeBlock] = operation(opt, ...
blockIdx, axisBlock, cornerBlock, edgeBlock)
switch opt
case 1 % F
R = getRotateMatrix(1,0,0);
for n = blockIdx.cornerIdx([1,2,6,5])
cornerBlock{n}.RotateMatrix = cornerBlock{n}.RotateMatrix*R;
end
for n = blockIdx.edgeIdx([1,5,9,8])
edgeBlock{n}.RotateMatrix = edgeBlock{n}.RotateMatrix*R;
end
blockIdx.cornerIdx([1,2,6,5]) = blockIdx.cornerIdx([2,6,5,1]) ;
blockIdx.edgeIdx([1,5,9,8]) = blockIdx.edgeIdx([5,9,8,1]);
case -1 % F'
R = getRotateMatrix(-1,0,0);
for n = blockIdx.cornerIdx([1,2,6,5])
cornerBlock{n}.RotateMatrix = cornerBlock{n}.RotateMatrix*R;
end
for n = blockIdx.edgeIdx([1,5,9,8])
edgeBlock{n}.RotateMatrix = edgeBlock{n}.RotateMatrix*R;
end
blockIdx.cornerIdx([1,2,6,5]) = blockIdx.cornerIdx([5,1,2,6]) ;
blockIdx.edgeIdx([1,5,9,8]) = blockIdx.edgeIdx([8,1,5,9]);
case 2 % R
R = getRotateMatrix(0,1,0);
for n = blockIdx.cornerIdx([2,3,7,6])
cornerBlock{n}.RotateMatrix = cornerBlock{n}.RotateMatrix*R;
end
for n = blockIdx.edgeIdx([2,6,10,5])
edgeBlock{n}.RotateMatrix = edgeBlock{n}.RotateMatrix*R;
end
blockIdx.cornerIdx([2,3,7,6]) = blockIdx.cornerIdx([3,7,6,2]) ;
blockIdx.edgeIdx([2,6,10,5]) = blockIdx.edgeIdx([6,10,5,2]);
case -2 % R'
R = getRotateMatrix(0,-1,0);
for n = blockIdx.cornerIdx([2,3,7,6])
cornerBlock{n}.RotateMatrix = cornerBlock{n}.RotateMatrix*R;
end
for n = blockIdx.edgeIdx([2,6,10,5])
edgeBlock{n}.RotateMatrix = edgeBlock{n}.RotateMatrix*R;
end
blockIdx.cornerIdx([2,3,7,6]) = blockIdx.cornerIdx([6,2,3,7]) ;
blockIdx.edgeIdx([2,6,10,5]) = blockIdx.edgeIdx([5,2,6,10]);
case 3 % U
R = getRotateMatrix(0,0,1);
for n = blockIdx.cornerIdx([5,6,7,8])
cornerBlock{n}.RotateMatrix = cornerBlock{n}.RotateMatrix*R;
end
for n = blockIdx.edgeIdx([9,10,11,12])
edgeBlock{n}.RotateMatrix = edgeBlock{n}.RotateMatrix*R;
end
blockIdx.cornerIdx([5,6,7,8]) = blockIdx.cornerIdx([6,7,8,5]) ;
blockIdx.edgeIdx([9,10,11,12]) = blockIdx.edgeIdx([10,11,12,9]);
case -3 % U'
R = getRotateMatrix(0,0,-1);
for n = blockIdx.cornerIdx([5,6,7,8])
cornerBlock{n}.RotateMatrix = cornerBlock{n}.RotateMatrix*R;
end
for n = blockIdx.edgeIdx([9,10,11,12])
edgeBlock{n}.RotateMatrix = edgeBlock{n}.RotateMatrix*R;
end
blockIdx.cornerIdx([5,6,7,8]) = blockIdx.cornerIdx([8,5,6,7]) ;
blockIdx.edgeIdx([9,10,11,12]) = blockIdx.edgeIdx([12,9,10,11]);
case 4 % B
R = getRotateMatrix(-1,0,0);
for n = blockIdx.cornerIdx([3,4,8,7])
cornerBlock{n}.RotateMatrix = cornerBlock{n}.RotateMatrix*R;
end
for n = blockIdx.edgeIdx([3,7,11,6])
edgeBlock{n}.RotateMatrix = edgeBlock{n}.RotateMatrix*R;
end
blockIdx.cornerIdx([3,4,8,7]) = blockIdx.cornerIdx([4,8,7,3]) ;
blockIdx.edgeIdx([3,7,11,6]) = blockIdx.edgeIdx([7,11,6,3]);
case -4 % B'
R = g