]> Creatis software - CreaPhase.git/blob - octave_packages/miscellaneous-1.1.0/hilbert_curve.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / miscellaneous-1.1.0 / hilbert_curve.m
1 ## Copyright (C) 2009 Javier Enciso <j4r.e4o@gmail.com>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function file} {@var{x}, @var{y}} hilbert_curve (@var{n})
18 ## Creates an iteration of the Hilbert space-filling curve with @var{n} points. 
19 ## The argument @var{n} must be of the form @code{2^M}, where @var{m} is an 
20 ## integer greater than 0.
21 ## 
22 ## @example
23 ## n = 8
24 ## [x ,y] = hilbert_curve (n);
25 ## line (x, y, "linewidth", 4, "color", "blue");
26 ## @end example
27 ## 
28 ## @end deftypefn
29
30 function [x, y] = hilbert_curve (n)
31
32   if (nargin != 1)
33     print_usage ();
34   endif
35   
36   check_power_of_two (n);
37   if (n == 2)
38     x = [0, 0, 1, 1];
39     y = [0, 1, 1, 0];
40   else
41     [x1, y1] = hilbert_curve (n/2);
42     x = [y1, x1, n/2 + x1, n - 1 - y1];
43     y = [x1, n/2 + y1, n/2 + y1, n/2 - 1 - x1];
44   endif
45   
46 endfunction
47
48 function check_power_of_two (n)
49   if (frac_part (log (n) / log (2)) != 0)
50     error ("hilbert_curve: input argument must be a power of 2.")
51   endif
52 endfunction
53
54 function d = frac_part (f)
55   d = f - floor (f);
56 endfunction
57
58 %!test
59 %! n = 2;
60 %! expect = [0, 0, 1, 1; 0, 1, 1, 0];
61 %! [get(1,:), get(2,:)] = hilbert_curve (n);
62 %! if (any(size (expect) != size (get)))
63 %!   error ("wrong size: expected %d,%d but got %d,%d", size (expect), size (get));
64 %! elseif (any (any (expect!=get)))
65 %!   error ("didn't get what was expected.");
66 %! endif
67
68 %!test
69 %! n = 5;
70 %!error hilbert_curve (n);
71
72 %!demo
73 %! clf
74 %! n = 4;
75 %! [x, y] = hilbert_curve (n);
76 %! line (x, y, "linewidth", 4, "color", "blue");
77 %! % -----------------------------------------------------------------------
78 %! % the figure window shows an iteration of the Hilbert space-fillig curve 
79 %! % with 4 points on each axis.
80
81 %!demo
82 %! clf
83 %! n = 64;
84 %! [x, y] = hilbert_curve (n);
85 %! line (x, y, "linewidth", 2, "color", "blue");
86 %! % ----------------------------------------------------------------------
87 %! % the figure window shows an iteration of the Hilbert space-fillig curve 
88 %! % with 64 points on each axis.