image thumbnail

Mathworks Rubik version 2

by

 

04 Apr 2013 (Updated )

Display, tester and solver of the Rubik's game, including "full cubes".

RotateLayer(hAxes,Cube,Axe,Side,Direction,1); Congr(hAxes,Cube);
function RotateWithMouse(bOnlyRotCube)
% to rotate cube or layer with mouse
%    RotateWithMouse[(bOnlyRotCube)]

if nargin==0
	bOnlyRotCube=false;
end
hAxes=gca;
Cube=get(hAxes,'UserData');
hFig = get(hAxes,'Parent');
p = get(hFig,'CurrentPoint'); % pointer coordinates
pinpol = 0; % <>0, if Pointer is in any polygon
iCube=get(gco,'UserData');iCube=iCube(end);
if any(iCube==[23 15 17])
	% mid cube
else
	for k=1:size(Cube.PgRotCubeX,2), % for cube rotation
		if inpolygon(p(1),p(2),Cube.PgRotCubeX(:,k),Cube.PgRotCubeY(:,k))==1,
			pinpol = -k;
			break;
		end
	end
	if pinpol==0&~bOnlyRotCube, % for layer rotation
		for k=1:size(Cube.PgRotLayX,2),
			if inpolygon(p(1),p(2),Cube.PgRotLayX(:,k),Cube.PgRotLayY(:,k))==1,
				pinpol = k;
				break;
			end
		end
	end
end
if pinpol<0, % to rotate cube
	Axe = fix((-pinpol-1)/4)+1;
	Direction = 1-mod(fix((-pinpol-1)/2),2)*2;
	Cube = RotateCube(hAxes,Cube,Axe,Direction);
elseif pinpol>0, % to rotate layer
	Axe = fix((pinpol-1)/8)+1;
	Side = 1-mod(fix((pinpol-1)/4),2)*2;
	Direction = 1-mod(fix((pinpol-1)/2),2)*2;
	if ~isempty(Cube.sol)
		Cube.sol=[];
		showsol stop
	end
	Cube = RotateLayer(hAxes,Cube,Axe,Side,Direction,1);
	Congr(hAxes,Cube); % Congratulation
end
set(hAxes,'UserData',Cube);
if strcmp(get(gcf,'Tag'),'RubikSettingUp')
	SetColors('rotated')
end

Contact us