function diceplot(varargin)
%DICEPLOT plots a die for dice games
% DICEPLOT(NUMBER) plots the dice with dots for the given dice NUMBER,
% values which range from 1 to 6. Unknown is represented by either -1 or '?'
% DICEPLOT(NUMBER, HANDLE) sets the the current axis to that specified
% by HANDLE
% DICEPLOT(NUMBER, HANDLE, COLOR) sets the the current axis to the
% specified COLOR, a thre element vector
% make constant array of colors for the axis
colorsofdice = [1.0 0.8 0.8; 0.8 1.0 0.8; 0.8 0.8 1.0; 1.0 1.0 0.8; 1.0 0.8 1.0; 0.8 1.0 1.0];
if nargin
number = varargin{1};
if number == '?'
number = -1 ; %unkown case
end
if ischar(number)
number = str2num(number);
end
if ~sum( (number == [-1,1:6]) ) % check for integer match, -1 means '?'
error('Matlab:diceplot', 'Dice number is not valid');
end
else
number = -1; %default to unknown if no inputs
end
if nargin >= 2
axes(varargin{2}); % set current axis
end
cla;
str = '\cdot'; % symbol to plot
col = 'k'; % color of symbol
ax = axis; % get current scaling of axis
fsize = 2.5/diff(ax(3:4)); % font size
offy = 0.15*fsize*diff(ax(3:4));% calculate y-offset
% | 1 5 |
% | 2 4 6 | % die dots positions
% | 3 7 |
xlocs = [0.2 0.2 0.2 0.5 0.8 0.8 0.8];
ylocs = [0.8 0.5 0.2 0.5 0.8 0.5 0.2];
switch number % select dot positions for given die number
case 1
dots = [4];
case 2
dots = [3 5];
case 3
dots = [3 4 5];
case 4
dots = [1 3 5 7];
case 5
dots = [1 3 4 5 7];
case 6
dots = [1 2 3 5 6 7];
otherwise
str = '?';
dots = [4];
fsize = fsize/2.5;
offy = 0;
end
xlocs = xlocs(dots);
ylocs = ylocs(dots);
h = text(xlocs, ylocs+offy, str); % display dots
set(h, 'HitTest', 'off'); % make unselectable
set(h,'fontunits','norm','fontsize',fsize,'HorizontalAlignment','center','VerticalAlignment','middle')
if nargin >= 3 % enter color routines
if varargin{3} == 1 % we want to set color
if number > 0
set(varargin{2}, 'Color', colorsofdice(number,:) ); % set current axis
end
else
set(varargin{2}, 'Color', [1 1 1]); % set axis to white
end
end