| 1 | # Copyright 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::PeanoDiagonals; | ||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use 5.004; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 1 | $VERSION = 128; | |||||||||||||||||||||||||||
| 25 | 1 | ▶ | use Math::PlanePath; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 1 | @ISA = ('Math::PlanePath'); | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use Math::PlanePath; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 1 | *max = \&Math::PlanePath::_max; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use Math::PlanePath::PeanoCurve; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | *_n_to_xykk = \&Math::PlanePath::PeanoCurve::_n_to_xykk; | |||||||||||||||||||||||||||
| 33 | 1 | *_xykk_to_n = \&Math::PlanePath::PeanoCurve::_xykk_to_n; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | use Math::PlanePath::Base::Generic | ||||||||||||||||||||||||||||
| 36 | 1 | 'is_infinite', | |||||||||||||||||||||||||||
| 37 | 1 | ▶ | 'round_nearest'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | use Math::PlanePath::Base::Digits | ||||||||||||||||||||||||||||
| 39 | 1 | 'round_up_pow', | |||||||||||||||||||||||||||
| 40 | 'round_down_pow', | ||||||||||||||||||||||||||||
| 41 | 'digit_split_lowtohigh', | ||||||||||||||||||||||||||||
| 42 | 1 | ▶ | 'digit_join_lowtohigh'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 46 | # use Smart::Comments; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use constant n_start => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | use constant class_x_negative => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use constant class_y_negative => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | use constant turn_any_straight => 0; # never straight | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 1 | ▶ | use constant dx_minimum => -1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | use constant dx_maximum => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 1 | ▶ | use constant dy_minimum => -1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 1 | ▶ | use constant dy_maximum => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | use constant parameter_info_array => | |||||||||||||||||||||||||||
| 60 | [ { name => 'radix', | ||||||||||||||||||||||||||||
| 61 | share_key => 'radix_3', | ||||||||||||||||||||||||||||
| 62 | display => 'Radix', | ||||||||||||||||||||||||||||
| 63 | type => 'integer', | ||||||||||||||||||||||||||||
| 64 | minimum => 2, | ||||||||||||||||||||||||||||
| 65 | default => 3, | ||||||||||||||||||||||||||||
| 66 | width => 3, | ||||||||||||||||||||||||||||
| 67 | 1 | ▶ | } ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # odd radix is unit steps diagonally, | ||||||||||||||||||||||||||||
| 70 | # even radix unlimited | ||||||||||||||||||||||||||||
| 71 | sub _UNDOCUMENTED__dxdy_list { | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_UNDOCUMENTED__dxdy_list: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | return ($self->{'radix'} % 2 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | ? (1,1, -1,1, -1,-1, 1,-1) | ||||||||||||||||||||||||||||
| 75 | : ()); # even, unlimited | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | sub new { | ||||||||||||||||||||||||||||
| 79 | 58 | ▶ | my $self = shift->SUPER::new(@_); | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 58 | ▶ | if (! $self->{'radix'} || $self->{'radix'} < 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->{"radix"} or $self->{"radix"} < 2
MC/DC50%1/2
not $self->{"radix"} or $self->{"radix"} < 2
not $self->{"radix"}
$self->{"radix"} < 2
Truth table66%2/3
not(A) or B
A= not $self->{"radix"}B= $self->{"radix"} < 2
| |||||||||||||||||||||||||||||
| 82 | 7 | $self->{'radix'} = 3; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | 58 | return $self; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub n_to_xy { | ||||||||||||||||||||||||||||
| 88 | 40404 | ▶ | my ($self, $n) = @_; | ||||||||||||||||||||||||||
|
Subroutine
n_to_xy: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 89 | ### PeanoDiagonals n_to_xy(): "$n" | ||||||||||||||||||||||||||||
| 90 | 40404 | ▶ | if ($n < 0) { # negative | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 1 | return; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 40403 | ▶ | if (is_infinite($n)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | return ($n,$n); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 40403 | my $frac; | |||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 40403 | my $int = int($n); | |||||||||||||||||||||||||||
| 100 | 40403 | $frac = $n - $int; # inherit possible BigFloat | |||||||||||||||||||||||||||
| 101 | 40403 | $n = $int; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 40403 | my ($x,$y, $xk,$yk) = _n_to_xykk($self,$n); | |||||||||||||||||||||||||||
| 105 | ### xykk: "$x,$y $xk,$yk" | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 40403 | ▶ | return ($x + ($xk&1 ? 1-$frac : $frac), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 108 | $y + ($yk&1 ? 1-$frac : $frac)); | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub xy_to_n { | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | return scalar((shift->xy_to_n_list(@_))[0]); | ||||||||||||||||||||||||||
|
Subroutine
xy_to_n: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | sub xy_to_n_list { | ||||||||||||||||||||||||||||
| 115 | 40 | ▶ | my ($self, $x, $y) = @_; | ||||||||||||||||||||||||||
|
Subroutine
xy_to_n_list: called CC=7 SCAR=20.0
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | ### PeanoDiagonals xy_to_n(): "$x, $y" | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # For odd radix, if X is even then segments are NE or SW, so offset 0,0 or | ||||||||||||||||||||||||||||
| 119 | # 1,1 to go to "middle" points. Conversely if X is odd then segments are | ||||||||||||||||||||||||||||
| 120 | # NW or SE so offset 0,1 or 1,0. | ||||||||||||||||||||||||||||
| 121 | # | ||||||||||||||||||||||||||||
| 122 | # ENHANCE-ME: For odd radix, the two offsets are exactly the two visits. | ||||||||||||||||||||||||||||
| 123 | # Should be able to pay attention to the low 0s or 2s and so have the | ||||||||||||||||||||||||||||
| 124 | # digits of both N in one look. | ||||||||||||||||||||||||||||
| 125 | # | ||||||||||||||||||||||||||||
| 126 | # ENHANCE-ME: Is the offset rule for even radix found as easily? | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 40 | $x = round_nearest ($x); | |||||||||||||||||||||||||||
| 129 | 40 | $y = round_nearest ($y); | |||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 40 | ▶ | if ($x < 0 || $y < 0) { return; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$x < 0 or $y < 0
MC/DC50%1/2
$x < 0 or $y < 0
$x < 0
$y < 0
Truth table66%2/3
A or B
A= $x < 0B= $y < 0
| |||||||||||||||||||||||||||||
| 132 | 39 | ▶ | if (is_infinite($x)) { return $x; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 39 | ▶ | if (is_infinite($y)) { return $y; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | return | ||||||||||||||||||||||||||||
| 136 | 18 | sort {$a<=>$b} | |||||||||||||||||||||||||||
| 137 | 122 | ▶ | map {_xykk_to_n($self, $x,$y, @$_)} | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 138 | 39 | ($self->{'radix'}&1 | |||||||||||||||||||||||||||
| 139 | ? ($x&1 ? ([0,1],[1,0]) : ([0,0],[1,1])) | ||||||||||||||||||||||||||||
| 140 | : ([0,0],[1,1], [0,1],[1,0])); | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 145 | # not exact | ||||||||||||||||||||||||||||
| 146 | # block 0 .. 3^k-1 contains all | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | sub rect_to_n_range { | ||||||||||||||||||||||||||||
| 149 | 47 | ▶ | my ($self, $x1,$y1, $x2,$y2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rect_to_n_range: called CC=6 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 47 | $x1 = round_nearest ($x1); | |||||||||||||||||||||||||||
| 152 | 47 | $y1 = round_nearest ($y1); | |||||||||||||||||||||||||||
| 153 | 47 | $x2 = round_nearest ($x2); | |||||||||||||||||||||||||||
| 154 | 47 | $y2 = round_nearest ($y2); | |||||||||||||||||||||||||||
| 155 | 47 | ▶ | ($x1,$x2) = ($x2,$x1) if $x1 > $x2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | 47 | ▶ | ($y1,$y2) = ($y2,$y1) if $y1 > $y2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | ### rect_to_n_range(): "$x1,$y1 to $x2,$y2" | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 47 | ▶ | if ($x2 < 0 || $y2 < 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$x2 < 0 or $y2 < 0
MC/DC0%0/2
$x2 < 0 or $y2 < 0
$x2 < 0
$y2 < 0
Truth table33%1/3
A or B
A= $x2 < 0B= $y2 < 0
| |||||||||||||||||||||||||||||
| 160 | 0 | return (1, 0); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 47 | my $radix = $self->{'radix'}; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 47 | my ($power, $level) = round_down_pow (max($x2,$y2)*$radix, $radix); | |||||||||||||||||||||||||||
| 166 | 47 | ▶ | if (is_infinite($level)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | 0 | return (0, $level); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | 47 | return (0, $power*$power - 1); | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 173 | # levels | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | sub level_to_n_range { | ||||||||||||||||||||||||||||
| 176 | 3 | ▶ | my ($self, $level) = @_; | ||||||||||||||||||||||||||
|
Subroutine
level_to_n_range: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 177 | 3 | return (0, $self->{'radix'}**(2*$level)); | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | sub n_to_level { | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my ($self, $n) = @_; | ||||||||||||||||||||||||||
|
Subroutine
n_to_level: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if ($n < 0) { return undef; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 0 | $n = round_nearest($n); | |||||||||||||||||||||||||||
| 183 | 0 | my ($pow, $exp) = round_up_pow ($n, $self->{'radix'}*$self->{'radix'}); | |||||||||||||||||||||||||||
| 184 | 0 | return $exp; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # num low ternary 0s, and whether odd or even above there which is parity of | ||||||||||||||||||||||||||||
| 190 | # how many 1-digits | ||||||||||||||||||||||||||||
| 191 | # | ||||||||||||||||||||||||||||
| 192 | sub _UNDOCUMENTED__n_to_turn_LSR { | ||||||||||||||||||||||||||||
| 193 | 20180 | ▶ | my ($self, $n) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_UNDOCUMENTED__n_to_turn_LSR: called CC=6 SCAR=18.1
| |||||||||||||||||||||||||||||
| 194 | 20180 | ▶ | if ($n < 1 || is_infinite($n)) { return undef; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$n < 1 or is_infinite($n)
MC/DC50%1/2
$n < 1 or is_infinite($n)
$n < 1
is_infinite($n)
Truth table66%2/3
A or B
A= $n < 1B= is_infinite($n)
| |||||||||||||||||||||||||||||
| 195 | 20178 | my $radix = $self->{'radix'}; | |||||||||||||||||||||||||||
| 196 | 20178 | ▶ | if ($radix & 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | 20178 | my $turn = 1; | |||||||||||||||||||||||||||
| 198 | 20178 | ▶ | until ($n % $radix) { # parity of low 0s | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 3704 | $turn = -$turn; | |||||||||||||||||||||||||||
| 200 | 3704 | $n /= $radix; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | 20178 | ▶ | return ($n % 2 ? -$turn : $turn); # and flip again if odd | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | 0 | return $self->SUPER::_UNDOCUMENTED__n_to_turn_LSR($n); | |||||||||||||||||||||||||||
| 205 | } | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 1 | 1; | |||||||||||||||||||||||||||
| 210 | __END__ | ||||||||||||||||||||||||||||