MATLAB魔方

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_25634581/article/details/82286158

魔方程序说明

一、运行界面

运行程序。点击下方按钮,可旋转魔方指定的一层。

二、程序GUI代码

%Author:LeiZhen
%Date:2018-10-16
function varargout = mofang6(varargin)
% MOFANG6 MATLAB code for mofang6.fig
%      MOFANG6, by itself, creates a new MOFANG6 or raises the existing
%      singleton*.
%
%      H = MOFANG6 returns the handle to a new MOFANG6 or the handle to
%      the existing singleton*.
%
%      MOFANG6('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MOFANG6.M with the given input arguments.
%
%      MOFANG6('Property','Value',...) creates a new MOFANG6 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before mofang6_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to mofang6_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help mofang6

% Last Modified by GUIDE v2.5 19-Aug-2015 11:28:05

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @mofang6_OpeningFcn, ...
                   'gui_OutputFcn',  @mofang6_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT



% --- Executes just before mofang6 is made visible.
function mofang6_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to mofang6 (see VARARGIN)

% Choose default command line output for mofang6
handles.output = hObject;

x = [-0.5  0.5  0.5 -0.5 -0.5 -0.5;  0.5  0.5 -0.5 -0.5  0.5  0.5;  0.5  0.5 -0.5 -0.5  0.5  0.5; -0.5  0.5  0.5 -0.5 -0.5 -0.5]; %%X轴中层[-0.5,0.5]
y = [-0.5 -0.5  0.5 -0.5 -0.5 -0.5;-0.5  0.5  0.5  0.5 -0.5 -0.5; -0.5  0.5  0.5  0.5  0.5  0.5; -0.5 -0.5  0.5 -0.5  0.5  0.5]; %%Y轴中层[-0.5,0.5]
z = [0 0 0 0 0 1; 0 0 0 0 0 1; 1 1 1 1 0 1; 1 1 1 1 0 1];%%Z轴中层[0,1]%%第五列Z=0的平面,第六列控制Z=1的平面
x1 = ones(size(x))+x;%%X轴上层[0.5,1.5]
y1 = ones(size(x))+y;%%Y轴上层[0.5,1.5]
x2 = -1*ones(size(x))+x;%%X轴下层[-1.5,-0.5]
y2 = -1*ones(size(x))+y;%YX轴下层[-1.5,-0.5]
z1 = ones(size(z))+z;%%Z轴上层[1,2]
z2 = -1*ones(size(z))+z;%%Z轴下层[-1,0]
Orgcell1 = [1 0 1  0  1 1 ];        %决定六个面的颜色
Orgcell2 = [0 0 0.46 1  1 1];
Orgcell3 = [0 1 0  0  0 1 ];
Org1(:,1) = Orgcell1;%%转置
Org2(:,1) = Orgcell2;%%转置
Org3(:,1) = Orgcell3;%%转置
Org(1,:,:)=[Org1 Org2 Org3 ];%%1*6*3的三维矩阵
%Org(2,:,:)=[Org1 Org2 Org3 ];
%Org(3,:,:)=[Org1 Org2 Org3 ];
%Org(4,:,:)=[Org1 Org2 Org3 ];
%axis(handles.axes_mofang)
figure(1);
hold on
%%魔方共27个小块,hn代表其中某一小块
global h0 h1 h2 h3 h4 h5 h6 h7 h8 h10 h11 h12 h13 h14 h15 h16 h17 h18 h20 h21 h22 h23 h24 h25 h26 h27 h28
h0= patch(x2,y1,z, Org);%%X轴下层[-1.5,0.5],Y轴上层[0.5,1.5],Z轴中层[0,1]
h1 = patch(x,y1,z, Org);  %%X轴中层[-0.5,0.5],Y轴上层[0.5,1.5],Z轴中层[0,1]
h2 = patch(x1,y1,z, Org);%%X轴上层[0.5,1.5],Y轴上层[0.5,1.5],Z轴中层[0,1]
h3 = patch(x2,y,z, Org);  %%X轴下层[-1.5,0.5],Y轴中层[-0.5,0.5],Z轴中层[0,1]
h4 = patch(x,y,z, Org);    %%X轴中层[-0.5,0.5],Y轴中层[-0.5,0.5],Z轴中层[0,1]
h5 = patch(x1,y,z, Org);  %%X轴上层[0.5,1.5],Y轴中层[-0.5,0.5],Z轴中层[0,1]
h6 = patch(x2,y2,z, Org);%%X轴下层[-1.5,0.5],Y轴下层[-1.5,-0.5],Z轴中层[0,1]
h7 = patch(x,y2,z, Org);  %%X轴中层[-0.5,0.5],Y轴下层[-1.5,-0.5],Z轴中层[0,1]
h8 = patch(x1,y2,z, Org);%%X轴上层[0.5,1.5],Y轴下层[-1.5,-0.5],Z轴中层[0,1]
 
h10 = patch(x2,y1,z1, Org);%%X轴下层[-1.5,0.5],Y轴上层[0.5,1.5],Z轴上层[1,2]
h11 = patch(x,y1,z1, Org);%%X轴中层[-0.5,0.5],Y轴上层[0.5,1.5],Z轴上层[1,2]
h12 = patch(x1,y1,z1, Org);%%X轴上层[0.5,1.5],Y轴上层[0.5,1.5],Z轴上层[1,2]
h13 = patch(x2,y,z1, Org);%%X轴下层[-1.5,0.5],Y轴中层[-0.5,0.5],Z轴上层[1,2]
h14 = patch(x,y,z1, Org);%%X轴中层[-0.5,0.5],Y轴中层[-0.5,0.5],Z轴上层[1,2]
h15 = patch(x1,y,z1, Org);%%X轴上层[0.5,1.5],Y轴中层[-0.5,0.5],Z轴上层[1,2]
h16 = patch(x2,y2,z1, Org);%%X轴下层[-1.5,0.5],Y轴下层[-1.5,-0.5],Z轴上层[1,2]
h17 = patch(x,y2,z1, Org);%%X轴中层[-0.5,0.5],Y轴下层[-1.5,-0.5],Z轴上层[1,2]
h18 = patch(x1,y2,z1, Org);%%X轴上层[0.5,1.5],Y轴下层[-1.5,-0.5],Z轴上层[1,2]
 
h20 = patch(x2,y1,z2, Org);%%X轴下层[-1.5,0.5],Y轴上层[0.5,1.5],Z轴下层[-1,0]
h21 = patch(x,y1,z2, Org);%%X轴中层[-0.5,0.5],Y轴上层[0.5,1.5],Z轴下层[-1,0]
h22 = patch(x1,y1,z2, Org);%%X轴上层[0.5,1.5],Y轴上层[0.5,1.5],Z轴下层[-1,0]
h23 = patch(x2,y,z2, Org);%%X轴下层[-1.5,0.5],Y轴中层[-0.5,0.5],Z轴下层[-1,0]
h24 = patch(x,y,z2, Org);%%X轴中层[-0.5,0.5],Y轴中层[-0.5,0.5],Z轴下层[-1,0]
h25 = patch(x1,y,z2, Org);%%X轴上层[0.5,1.5],Y轴中层[-0.5,0.5],Z轴下层[-1,0]
h26 = patch(x2,y2,z2, Org);%%X轴下层[-1.5,0.5],Y轴下层[-1.5,-0.5],Z轴下层[-1,0]
h27 = patch(x,y2,z2, Org);%%X轴中层[-0.5,0.5],Y轴下层[-1.5,-0.5],Z轴下层[-1,0]
h28 = patch(x1,y2,z2, Org);%%X轴上层[0.5,1.5],Y轴下层[-1.5,-0.5],Z轴下层[-1,0]
hold off;
axis equal tight                                %%显示的三维背景
axis([-4 4 -4 4 -4 4]);                        %%
title('三阶魔方');                               %%
xlabel('Variable X');                        %%
ylabel('Variable Y');                        %%
zlabel('Variable Z');                         %%
grid on                                               %%显示的三维背景


% Update handles structure
guidata(hObject, handles);
% UIWAIT makes mofang6 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = mofang6_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


function ssz=shunshizhen(h,direct)
origin=[0 0 0.5]; %魔方正中心,此为旋转中心
theta=11.25;%5.625;  %每次转动的角度
H=h;
for i=1:8        
    rotate(H(:),direct,theta,origin)   
    pause(0.01)   %%停顿的秒数
end
h11s=h(1,1);h12s=h(1,2);h13s=h(1,3);h21s=h(2,1);h22s=h(2,2);h23s=h(2,3);h31s=h(3,1);h32s=h(3,2);h33s=h(3,3);
h(1,1)=h31s;h(1,2)=h21s;h(1,3)=h11s;h(2,1)=h32s;h(2,2)=h22s;h(2,3)=h12s;h(3,1)=h33s;h(3,2)=h23s;h(3,3)=h13s;
ssz=h;


function nsz=nishizhen(h,direct)
origin=[0 0 0.5]; %魔方正中心,为旋转中心
theta=11.25;  %每次转动的角度
H=h;
for i=1:8        
    rotate(H(:),direct,theta,origin)   
    pause(0.01)   %%停顿的秒数
end
h11s=h(1,1);h12s=h(1,2);h13s=h(1,3);h21s=h(2,1);h22s=h(2,2);h23s=h(2,3);h31s=h(3,1);h32s=h(3,2);h33s=h(3,3);
h(1,1)=h13s;h(1,2)=h23s;h(1,3)=h33s;h(2,1)=h12s;h(2,2)=h22s;h(2,3)=h32s;h(3,1)=h11s;h(3,2)=h21s;h(3,3)=h31s;
nsz = h;


function tiaozheng(handles)
global h0 h1 h2 h3 h4 h5 h6 h7 h8 h10 h11 h12 h13 h14 h15 h16 h17 h18 h20 h21 h22 h23 h24 h25 h26 h27 h28
if get(handles. pushbutton_xup,'value')==1
    H1=[h18  h15  h12; h8 h5  h2;h28 h25  h22]; %H1为有蓝色面的九小块,X轴上层
    h=nishizhen(H1,[1,0,0]);
    h18=h(1,1);h15=h(1,2);h12=h(1,3);h8=h(2,1);h5=h(2,2);h2=h(2,3);h28=h(3,1);h25=h(3,2);h22=h(3,3);
elseif get(handles. pushbutton_xmid,'value')==1    
    H2=[h17  h14  h11; h7 h4  h1;h27 h24  h21]; %H2为X轴中层
    h=nishizhen(H2,[1,0,0]);
    h17=h(1,1);h14=h(1,2);h11=h(1,3);h7=h(2,1);h4=h(2,2);h1=h(2,3);h27=h(3,1);h24=h(3,2);h21=h(3,3);  
elseif get(handles. pushbutton_xdown,'value')==1    
    H3=[h16  h13  h10; h6 h3  h0;h26 h23  h20]; %H3为X轴下层
    h=nishizhen(H3,[1,0,0]);
    h16=h(1,1);h13=h(1,2);h10=h(1,3);h6=h(2,1);h3=h(2,2);h0=h(2,3);h26=h(3,1);h23=h(3,2);h20=h(3,3);  
elseif get(handles. pushbutton_yup,'value')==1   
    H4=[ h10 h11 h12;h0  h1  h2; h20 h21 h22];  %H4为Y轴上层
    h=shunshizhen(H4,[0,1,0]);
    h10=h(1,1);h11=h(1,2);h12=h(1,3);h0=h(2,1);h1=h(2,2);h2=h(2,3);h20=h(3,1);h21=h(3,2);h22=h(3,3);    
elseif get(handles. pushbutton_ymid,'value')==1   
    H5=[ h13 h14 h15;h3  h4  h5; h23 h24 h25];  %H5为Y轴中层
    h=shunshizhen(H5,[0,1,0]);
    h13=h(1,1);h14=h(1,2);h15=h(1,3);h3=h(2,1);h4=h(2,2);h5=h(2,3);h23=h(3,1);h24=h(3,2);h25=h(3,3);
elseif get(handles. pushbutton_ydown,'value')==1    
    H6=[h16 h17 h18;h6 h7 h8;h26 h27 h28];  %H6为有红色面的九小块,Y轴下层
    h=shunshizhen(H6,[0,1,0]);
    h16=h(1,1);h17=h(1,2);h18=h(1,3);h6=h(2,1);h7=h(2,2);h8=h(2,3);h26=h(3,1);h27=h(3,2);h28=h(3,3);
elseif get(handles. pushbutton_zup,'value')==1    
    H7=[h10 h11  h12; h13 h14  h15;h16 h17  h18]; %H7为Z轴上层  
    h=nishizhen(H7,[0,0,1]);
    h10=h(1,1);h11=h(1,2);h12=h(1,3);h13=h(2,1);h14=h(2,2);h15=h(2,3);h16=h(3,1);h17=h(3,2);h18=h(3,3);    
elseif get(handles. pushbutton_zmid,'value')==1    
    H8=[h0 h1  h2; h3 h4  h5;h6 h7  h8]; %H8为Z轴中层  
    h=nishizhen(H8,[0,0,1]);
    h0=h(1,1);h1=h(1,2);h2=h(1,3);h3=h(2,1);h4=h(2,2);h5=h(2,3);h6=h(3,1);h7=h(3,2);h8=h(3,3);
elseif get(handles. pushbutton_zdown,'value')==1
    H9=[h20 h21 h22; h23 h24 h25;h26 h27 h28];  %H9为有黄色面的九小块,Z轴下层
    h=nishizhen(H9,[0,0,1]);
    h20=h(1,1);h21=h(1,2);h22=h(1,3);h23=h(2,1);h24=h(2,2);h25=h(2,3);h26=h(3,1);h27=h(3,2);h28=h(3,3);
end


% --- Executes on button press in pushbutton_xup.
function pushbutton_xup_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_xup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tiaozheng(handles);


% --- Executes on button press in pushbutton_xmid.
function pushbutton_xmid_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_xmid (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tiaozheng(handles);


% --- Executes on button press in pushbutton_xdown.
function pushbutton_xdown_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_xdown (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tiaozheng(handles);


% --- Executes on button press in pushbutton_yup.
function pushbutton_yup_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_yup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tiaozheng(handles);


% --- Executes on button press in pushbutton_ymid.
function pushbutton_ymid_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_ymid (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tiaozheng(handles);


% --- Executes on button press in pushbutton_ydown.
function pushbutton_ydown_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_ydown (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tiaozheng(handles);


% --- Executes on button press in pushbutton_zup.
function pushbutton_zup_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_zup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tiaozheng(handles);


% --- Executes on button press in pushbutton_zmid.
function pushbutton_zmid_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_zmid (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tiaozheng(handles);


% --- Executes on button press in pushbutton_zdown.
function pushbutton_zdown_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_zdown (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
tiaozheng(handles);

三、GUI修改位置

新建一个空的GUI,命名为mofang6并保存。添加9个按钮,并按下图方式命名,每个按钮的属性都只修改String和Tag即可。

猜你喜欢

转载自blog.csdn.net/qq_25634581/article/details/82286158