PlanePath/HilbertSides.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 124 CC 8 SCAR 24.2
CC
8
Cov
0%
n_to_xy
30.0
xy_to_n_list
20.0
xy_to_n
2.0
CRAP 11.2
1 # Copyright 2015, 2016, 2017, 2018, 2019, 2020 Kevin Ryde
2  
3 # This file is part of Math-PlanePath.
4 #
5 # Math-PlanePath is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by the
7 # Free Software Foundation; either version 3, or (at your option) any later
8 # version.
9 #
10 # Math-PlanePath is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
17  
18  
19 package Math::PlanePath::HilbertSides;
20 use 5.004;
21 use strict;
22 use List::Util 'min'; # 'max'
23 *max = \&Math::PlanePath::_max;
24  
25 use vars '$VERSION', '@ISA';
26 $VERSION = 128;
27  
28 use Math::PlanePath;
29 use Math::PlanePath::Base::NSEW;
30 @ISA = ('Math::PlanePath::Base::NSEW',
31 'Math::PlanePath');
32  
33 *_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
34  
35 use Math::PlanePath::Base::Generic
36 'is_infinite',
37 'round_nearest';
38 use Math::PlanePath::Base::Digits
39 'round_down_pow',
40 'digit_split_lowtohigh',
41 'digit_join_lowtohigh';
42  
43 # uncomment this to run the ### lines
44 # use Smart::Comments;
45  
46 use Math::PlanePath::HilbertCurve;
47 my $hilbert_path = Math::PlanePath::HilbertCurve->new;
48  
49  
50 use constant n_start => 0;
51 use constant class_x_negative => 0;
52 use constant class_y_negative => 0;
53  
54 #------------------------------------------------------------------------------
55  
56 # ---3
57 # |
58 # state=0 3--2 plain 2
59 # | |
60 # 0--1 0--1
61 #
62 # state=4 1--2 transpose 1--2--3
63 # | | | |
64 # 0 3 0 |
65 #
66 # state=8 1--0 rot180 1--0
67 # | |
68 # 2--3 2
69 # |
70 # 3---
71 #
72 # state=12 3 0 rot180 + transpose | 0
73 # | | | |
74 # 2--1 3--2--1
75 #
76 # generated by tools/hilbert-curve-table.pl
77 my @next_state = (4,0,0,12, 0,4,4,8, 12,8,8,4, 8,12,12,0);
78 my @digit_to_x = (0,1,1,0, 0,0,1,1, 1,0,0,1, 1,1,0,0);
79 my @digit_to_y = (0,0,1,1, 0,1,1,0, 1,1,0,0, 1,0,0,1);
80 my @state_to_frac = (0,1, undef,undef,
81 1,0, undef,undef,
82 0,-1, undef,undef,
83 -1,0, undef,undef);
84  
85 sub n_to_xy {
86 my ($self, $n) = @_;
87 ### HilbertSides n_to_xy(): $n
88 ### hex: sprintf "%#X", $n
89  
90 if ($n < 0) { return; }
91 if (is_infinite($n)) { return ($n,$n); }
92  
93 my $int = int($n);
94 $n -= $int; # fraction part
95  
96 my @ndigits = digit_split_lowtohigh($int,4);
97 my $state = ($#ndigits & 1 ? 4 : 0);
98  
99 my (@xbits, @ybits);
100 foreach my $i (reverse 0 .. $#ndigits) { # digits high to low
101 $state += $ndigits[$i];
102 $xbits[$i] = $digit_to_x[$state];
103 $ybits[$i] = $digit_to_y[$state];
104 $state = $next_state[$state];
105 }
106  
107 my $zero = ($int * 0); # inherit bigint 0
108 # print "state $state state $state\n";
109 my $add = ($state >= 8 ? 1 : 0);
110  
111 return ($n * $state_to_frac[$state] # frac
112 + digit_join_lowtohigh (\@xbits, 2, $zero)
113 + $add,
114  
115 $n * $state_to_frac[$state+1] # frac
116 + digit_join_lowtohigh (\@ybits, 2, $zero)
117 + $add);
118 }
119  
120  
121 sub xy_to_n {
122 return scalar((shift->xy_to_n_list(@_))[0]);
123 }
124 sub xy_to_n_list {
125 my ($self, $x, $y) = @_;
126 ### HilbertSides xy_to_n(): "$x, $y"
127  
128 $x = round_nearest ($x);
129 $y = round_nearest ($y);
130  
131 my @n_list;
132 foreach my $d (0,1) {
133 ### try: ($x-$d).','.($y-$d)
134 my $n = $hilbert_path->xy_to_n($x-$d,$y-$d);
135 ### $n
136 if (defined $n) {
137 if (my ($gx,$gy) = $self->n_to_xy($n)) {
138 ### is at: "$gx,$gy"
139 if ($x == $gx && $y == $gy) {
140 ### push: $n
141 push @n_list, $n;
142 }
143 }
144 }
145 }
146 # if (@n_list == 2 && $n_list[0] > $n_list[1]) {
147 # @n_list = reverse @n_list;
148 # }
149 @n_list = sort {$a <=> $b} @n_list;
150 ### @n_list
151 return @n_list;
152 }
153  
154 # not exact
155 sub rect_to_n_range {
156 my ($self, $x1,$y1, $x2,$y2) = @_;
157 ### HilbertSides rect_to_n_range(): "$x1,$y1, $x2,$y2"
158  
159 $x1 = round_nearest ($x1);
160 $x2 = round_nearest ($x2);
161 $y1 = round_nearest ($y1);
162 $y2 = round_nearest ($y2);
163  
164 my ($pow, $exp) = round_down_pow (max(abs($x1),abs($y1), abs($x2),abs($y2)),
165 2);
166 return (0, 4*$pow*$pow);
167 }
168  
169 1;
170 __END__