]> Creatis software - CreaPhase.git/blob - octave_packages/miscellaneous-1.1.0/peano_curve.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / miscellaneous-1.1.0 / peano_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}} peano_curve (@var{n})
18 ## Creates an iteration of the Peano space-filling curve with @var{n} points. 
19 ## The argument @var{n} must be of the form @code{3^M}, where @var{m} is an 
20 ## integer greater than 0.
21 ## 
22 ## @example
23 ## n = 9;
24 ## [x, y] = peano_curve (n);
25 ## line (x, y, "linewidth", 4, "color", "red");
26 ## @end example
27 ## 
28 ## @end deftypefn
29
30 function [x, y] = peano_curve (n)
31   
32   if (nargin != 1)
33     print_usage ();
34   endif
35   
36   check_power_of_three (n);
37   if (n == 3)
38     x = [0, 0, 0, 1, 1, 1, 2, 2, 2];
39     y = [0, 1, 2, 2, 1, 0, 0, 1, 2];
40   else
41     [x1, y1] = peano_curve (n/3);
42     x2 = n/3 - 1 - x1;
43     x3 = n/3 + x1;
44     x4 = n - n/3 - 1 - x1;
45     x5 = n - n/3 + x1;
46     x6 = n - 1 - x1;
47     y2 = n/3 + y1;
48     y3 = n - n/3 + y1;
49     y4 = n - 1 - y1;
50     y5 = n - n/3 - 1 - y1;
51     y6 = n/3 - 1 - y1;
52     x = [x1, x2, x1, x3, x4, x3, x5, x6, x5];
53     y = [y1, y2, y3, y4, y5, y6, y1, y2, y3];
54   endif
55   
56 endfunction
57
58 function check_power_of_three (n)
59   if (frac_part (log (n) / log (3)) != 0)
60     error ("peano_curve: input argument must be a power of 3.")
61   endif
62 endfunction
63
64 function d = frac_part (f)
65   d = f - floor (f);
66 endfunction
67
68 %!test
69 %! n = 3;
70 %! expect(1,:) = [0, 0, 0, 1, 1, 1, 2, 2, 2];
71 %! expect(2,:) = [0, 1, 2, 2, 1, 0, 0, 1, 2];
72 %! [get(1,:), get(2,:)] = peano_curve (n);
73 %! if (any(size (expect) != size (get)))
74 %!   error ("wrong size: expected %d,%d but got %d,%d", size (expect), size (get));
75 %! elseif (any (any (expect!=get)))
76 %!   error ("didn't get what was expected.");
77 %! endif
78
79 %!test
80 %! n = 5;
81 %!error peano_curve (n);
82
83 %!demo
84 %! clf
85 %! n = 9;
86 %! [x, y] = peano_curve (n);
87 %! line (x, y, "linewidth", 4, "color", "red");
88 %! % --------------------------------------------------------------------
89 %! % the figure window shows an iteration of the Peano space-fillig curve 
90 %! % with 9 points on each axis.
91
92 %!demo
93 %! clf
94 %! n = 81;
95 %! [x, y] = peano_curve (n);
96 %! line (x, y, "linewidth", 2, "color", "red");
97 %! % --------------------------------------------------------------------
98 %! % the figure window shows an iteration of the Peano space-fillig curve 
99 %! % with 81 points on each axis.