# 【手把手制作三阶魔方模拟器】用MATLAB绘制一个三阶魔方

MATLAB 同时被 2 个专栏收录
69 篇文章 11 订阅
26 篇文章 2 订阅

by 今天不飞了

# 绘制三阶魔方每个块的中心

%% 块定义
% 块八顶点定义（魔方每一个小块上的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];
% 块六面顶点索引（在上次的基础上优化了一下，用索引表示）
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.3];

%% 26块6面上色
% 轴块属性初始化
axisBlock = cell(6,1);
for n = 1:6
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}.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}.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];

%% 显示一下（利用循环全部绘制）
%% 显示

% 轴块
for n = 1:6
for f = 1:6
V = axisBlock{n}.position+blockVertices(blockFace{f},:);
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},:);
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},:);
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


% 块八顶点定义（魔方每一个小块上的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; % *0.95


# # 其他

1. 持续更新
2. B站录播视频：【三阶魔方】手把手从零实现，魔方观察训练程序（一）我们来画个魔方
• 2
点赞
• 1
评论
• 2
收藏
• 打赏
• 扫一扫，分享海报

01-06
08-24

09-27 7084
05-20
11-13 2万+
12-21
04-08
03-24
10-16 2672
04-10 1228
11-30
02-04 811

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。