gusucode.com > 三维模仿源码程序 > 三维模仿源码程序/MathRubik2/RotateCube.m
function CubeOut = RotateCube(hAxes,Cube,Axe,Direction,Anime) %RotateCube - to rotate all cube % Cube = RotateCube(hAxes,Cube,Axe,Direction) % If output argument is not given, the result is put in hAxes's % UserData. % Axe : 1 = F, 2 = R, 3 = U % Direction : 1 = clockwise, -1 = counter clockwise. if Direction if ~exist('Anime','var')|isempty(Anime) Anime=1; end %!!!!!!! if ~isempty(Cube.texture)&Anime==1 Anime=-1; end %!!!!!!! if Anime>0 MyCube = Cube; % copy object a1 = Axe; % 1st axe a2 = Axe+1; if a2>3, a2 = 1; end % 2nd axe a3 = 6-a1-a2; % 3rd axe cosa = cos(Direction*pi/36); % cos(+-1 degree) sina = sin(Direction*pi/36); % sin(+-1 degree) T = zeros(3); T(a1,a1) = 1; T(a2,a2) = cosa; T(a2,a3) = -sina; T(a3,a2) = sina; T(a3,a3) = cosa; for k1=1:18, % angle loop for k2=1:27, % small cube loop MyCube.Nodes(:,:,k2) = MyCube.Nodes(:,:,k2)*T'; end PlotCube(hAxes,MyCube,1,0); drawnow; end end ind = 2*Axe-1+(1-sign(Direction))/2; % index number MyColor = zeros(size(Cube.Color)); for iDir=1:abs(Direction) % Direction == 2 is allowed (!) for k1=1:27, % small cube loop for k2=1:6, % flat loop MyColor(Cube.RotCubeFlat(ind,k2),Cube.RotCubeCube(ind,k1)) = ... Cube.Color(k2,k1); end end Cube.Color = MyColor; % change colors end if Anime PlotCube(hAxes,Cube,Anime>0,1); ShowTitle(hAxes,Cube) end H=Cube.history; Aconv=[1 -3 2;3 2 -1;-2 1 3]; for i=1:size(H,1) if Axe~=H(i) a=Aconv(H(i),Axe); H(i)=abs(a); H(i,2)=H(i,2)*sign(a)*Direction; end end Cube.history=H; end if nargout CubeOut=Cube; else set(hAxes,'UserData',Cube) end