annotate inst/checkerboard.m @ 897:d3ec45cd8660 stable release-2.2.2

maint: release 2.2.2.
author Carnë Draug <carandraug@octave.org>
date Mon, 06 Oct 2014 17:16:02 +0100
parents 606fc74cc118
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
682
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
1 ## Copyright (C) 2012 Carnë Draug <carandraug+dev@gmail.com>
683
606fc74cc118 checkerboard: faster and shorter code by Pantxo Diribarne
carandraug
parents: 682
diff changeset
2 ## Copyright (C) 2012 Pantxo Diribarne <pantxo@dibona>
682
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
3 ##
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
4 ## This program is free software; you can redistribute it and/or modify
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
5 ## it under the terms of the GNU General Public License as published by
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
6 ## the Free Software Foundation; either version 3 of the License, or
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
7 ## (at your option) any later version.
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
8 ##
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
12 ## GNU General Public License for more details.
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
13 ##
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
14 ## You should have received a copy of the GNU General Public License
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
15 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
16
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
17 ## -*- texinfo -*-
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
18 ## @deftypefn {Function File} {@var{board} =} checkerboard ()
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
19 ## @deftypefnx {Function File} {@var{board} =} checkerboard (@var{side})
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
20 ## @deftypefnx {Function File} {@var{board} =} checkerboard (@var{side}, @var{size})
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
21 ## @deftypefnx {Function File} {@var{board} =} checkerboard (@var{side}, @var{M}, @var{N})
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
22 ## Create checkerboard.
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
23 ##
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
24 ## Each tile of the checkerboard is made of four squares @var{side} pixels wide.
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
25 ## The created checkerboard itself will be @var{size}, or @var{M}x@var{N} tiles
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
26 ## wide. Defaults to 4x4 tiles 10 pixels wide.
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
27 ##
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
28 ## @seealso{repmat}
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
29 ## @end deftypefn
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
30
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
31 function [board] = checkerboard (side = 10, nRows = 4, nCols = nRows)
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
32 if (nargin > 3)
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
33 print_usage ();
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
34 endif
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
35 check_checkerboard (side, "square side");
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
36 check_checkerboard (nRows, "number of rows");
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
37 check_checkerboard (nCols, "number of columns");
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
38
683
606fc74cc118 checkerboard: faster and shorter code by Pantxo Diribarne
carandraug
parents: 682
diff changeset
39 [xx, yy] = meshgrid (linspace (-1, 1, 2*side));
606fc74cc118 checkerboard: faster and shorter code by Pantxo Diribarne
carandraug
parents: 682
diff changeset
40 tile = (xx .* yy) < 0;
606fc74cc118 checkerboard: faster and shorter code by Pantxo Diribarne
carandraug
parents: 682
diff changeset
41 board = double (repmat (tile, nRows, nCols));
606fc74cc118 checkerboard: faster and shorter code by Pantxo Diribarne
carandraug
parents: 682
diff changeset
42 board(:, (end/2+1):end) *= .7; # matlab compatible
682
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
43
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
44 endfunction
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
45
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
46 function check_checkerboard (in, name)
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
47 ## isindex makes easy to check if it's a positive integer but also returns
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
48 ## true for a logical matrix. Hence the use for islogical
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
49 if (! isscalar (in) || ! isindex (in) || islogical (in))
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
50 error ("checkerboard: %s must be a positive integer.", name)
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
51 endif
205e5f657033 New function: checkerboard
carandraug
parents:
diff changeset
52 endfunction