MATLAB魔方
阿新 • • 發佈:2019-01-03
魔方程式說明
一、執行介面
執行程式。點選下方按鈕,可旋轉魔方指定的一層。
二、程式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即可。