Mercurial > hg > octave-image
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 |
rev | line source |
---|---|
682 | 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 | 3 ## |
4 ## This program is free software; you can redistribute it and/or modify | |
5 ## it under the terms of the GNU General Public License as published by | |
6 ## the Free Software Foundation; either version 3 of the License, or | |
7 ## (at your option) any later version. | |
8 ## | |
9 ## This program is distributed in the hope that it will be useful, | |
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 ## GNU General Public License for more details. | |
13 ## | |
14 ## You should have received a copy of the GNU General Public License | |
15 ## along with this program; if not, see <http://www.gnu.org/licenses/>. | |
16 | |
17 ## -*- texinfo -*- | |
18 ## @deftypefn {Function File} {@var{board} =} checkerboard () | |
19 ## @deftypefnx {Function File} {@var{board} =} checkerboard (@var{side}) | |
20 ## @deftypefnx {Function File} {@var{board} =} checkerboard (@var{side}, @var{size}) | |
21 ## @deftypefnx {Function File} {@var{board} =} checkerboard (@var{side}, @var{M}, @var{N}) | |
22 ## Create checkerboard. | |
23 ## | |
24 ## Each tile of the checkerboard is made of four squares @var{side} pixels wide. | |
25 ## The created checkerboard itself will be @var{size}, or @var{M}x@var{N} tiles | |
26 ## wide. Defaults to 4x4 tiles 10 pixels wide. | |
27 ## | |
28 ## @seealso{repmat} | |
29 ## @end deftypefn | |
30 | |
31 function [board] = checkerboard (side = 10, nRows = 4, nCols = nRows) | |
32 if (nargin > 3) | |
33 print_usage (); | |
34 endif | |
35 check_checkerboard (side, "square side"); | |
36 check_checkerboard (nRows, "number of rows"); | |
37 check_checkerboard (nCols, "number of columns"); | |
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 | 43 |
44 endfunction | |
45 | |
46 function check_checkerboard (in, name) | |
47 ## isindex makes easy to check if it's a positive integer but also returns | |
48 ## true for a logical matrix. Hence the use for islogical | |
49 if (! isscalar (in) || ! isindex (in) || islogical (in)) | |
50 error ("checkerboard: %s must be a positive integer.", name) | |
51 endif | |
52 endfunction |