PlanePath/WythoffPreliminaryTriangle.pm untested

filter: Statementstmt 0.0% 0 / 104 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 22 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 169 CC 11 SCAR 25.0
CC
11
Cov
0%
xy_to_n
30.0
n_to_xy
20.0
rect_to_n_range
20.0
CRAP 12.1
1 # Copyright 2013, 2014, 2015, 2016, 2017, 2018, 2019 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 # x=45,y=10 x=59,y=19 dx=14,dy=9 14/9=1.55
20 #
21 # x=42,y=8 x=113,y=52 dx=71,dy=44 71/44=1.613
22 #
23 # below
24 # 32,12 to 36,4 sqrt((32-36)^2+(12-4)^2) = 9
25 # 84,34 to 99,14 sqrt((84-99)^2+(34-14)^2) = 25
26 # 180,64 to 216,11 sqrt((180-216)^2+(64-11)^2) = 64
27 #
28 # above
29 # 14,20 to 5,32 sqrt((14-5)^2+(20-32)^2) = 15 = 9*1.618 3
30 # 34,50 to 14,85 sqrt((34-14)^2+(50-85)^2) = 40 = 25*1.618 5
31 # 132,158 to 77,247 sqrt((132-77)^2+(158-247)^2) = 104 = 64*1.618 8
32 # 8,525 to 133,280 sqrt((8-133)^2+(525-280)^2) = 275 = 169*1.618 13
33  
34  
35 package Math::PlanePath::WythoffPreliminaryTriangle;
36 use 5.004;
37 use strict;
38  
39 use vars '$VERSION', '@ISA';
40 $VERSION = 128;
41 use Math::PlanePath;
42 @ISA = ('Math::PlanePath');
43  
44 use Math::PlanePath::Base::Generic
45 'is_infinite',
46 'round_nearest';
47 use Math::PlanePath::Base::Digits
48 'bit_split_lowtohigh';
49  
50 # uncomment this to run the ### lines
51 # use Smart::Comments;
52  
53  
54 use constant class_x_negative => 0;
55 use constant class_y_negative => 0;
56 use constant y_minimum => 1;
57 use constant diffxy_maximum => -1; # Y>=X+1 so X-Y <= -1
58  
59 # Apparent minimum dx=F(i),dy=F(i+5)
60 # eg. N=57313 dx=377,dy=34 F(14),F(9)
61 # N=392835 dx=987,dy=89 F(16),F(11)
62 # N=2692537 dx=2584,dy=233 F(18),F(13)
63 # dy/dx -> 1/phi^5
64 use constant dir_minimum_dxdy => (((1+sqrt(5))/2)**5, 1);
65 use constant dir_maximum_dxdy => (1,-1); # SE at N=5
66  
67 use Math::PlanePath::WythoffArray;
68 my $wythoff = Math::PlanePath::WythoffArray->new;
69  
70 sub new {
71 my $self = shift->SUPER::new(@_);
72 $self->{'shift'} ||= 0;
73 return $self;
74 }
75  
76 sub n_to_xy {
77 my ($self, $n) = @_;
78 ### WythoffPreliminaryTriangle n_to_xy(): $n
79  
80 if ($n < 1) { return; }
81 if (is_infinite($n) || $n == 0) { return ($n,$n); }
82  
83 {
84 # fractions on straight line ?
85 my $int = int($n);
86 if ($n != $int) {
87 my $frac = $n - $int; # inherit possible BigFloat/BigRat
88 my ($x1,$y1) = $self->n_to_xy($int);
89 my ($x2,$y2) = $self->n_to_xy($int+1);
90 my $dx = $x2-$x1;
91 my $dy = $y2-$y1;
92 return ($frac*$dx + $x1, $frac*$dy + $y1);
93 }
94 $n = $int;
95 }
96  
97 # prev+y=x
98 # prev = x-y
99 $n -= 1;
100 my $y = $wythoff->xy_to_n(0,$n);
101 my $x = $wythoff->xy_to_n(1,$n);
102  
103 while ($y <= $x) {
104 ### at: "y=$y x=$x"
105 ($y,$x) = ($x-$y,$y);
106 }
107 ### reduction to: "y=$y x=$x"
108  
109 ### return: "y=$y x=$x"
110 return ($x, $y);
111 }
112  
113 sub xy_is_visited {
114 my ($self, $x, $y) = @_;
115 $x = round_nearest ($x);
116 $y = round_nearest ($y);
117 return $x >= 0 && $x < $y;
118 }
119  
120 sub xy_to_n {
121 my ($self, $x, $y) = @_;
122 ### WythoffPreliminaryTriangle xy_to_n(): "$x, $y"
123  
124 $x = round_nearest ($x);
125 $y = round_nearest ($y);
126 my $orig_x = $x;
127 my $orig_y = $y;
128 if (is_infinite($y)) { return $y; }
129  
130 unless ($x >= 0 && $x < $y) {
131 return undef;
132 }
133  
134 ($y,$x) = ($x,$x+$y);
135 foreach (0 .. 500) {
136 ($y,$x) = ($x,$x+$y);
137 ### at: "seek y=$y x=$x"
138 my ($c,$r) = $wythoff->n_to_xy($y) or next;
139 my $wx = $wythoff->xy_to_n($c+1,$r);
140 if (defined $wx && $wx == $x) {
141 ### found: "pair $y $x at c=$c r=$r"
142 my $n = $r+1;
143 my ($nx,$ny) = $self->n_to_xy($n);
144 ### nxy: "nx=$nx, ny=$ny"
145 if ($nx == $orig_x && $ny == $orig_y) {
146 return $n;
147 } else {
148 ### no match: "cf x=$x y=$y"
149 return undef;
150 }
151 }
152 }
153 ### not found ...
154 return undef;
155 }
156  
157 sub rect_to_n_range {
158 my ($self, $x1,$y1, $x2,$y2) = @_;
159 ### WythoffPreliminaryTriangle rect_to_n_range(): "$x1,$y1 $x2,$y2"
160  
161 $x1 = round_nearest ($x1);
162 $y1 = round_nearest ($y1);
163 $x2 = round_nearest ($x2);
164 $y2 = round_nearest ($y2);
165  
166 ($x1,$x2) = ($x2,$x1) if $x1 > $x2;
167 ($y1,$y2) = ($y2,$y1) if $y1 > $y2;
168  
169 if ($x2 < 0 || $y2 < 1) {
170 ### all outside first quadrant ...
171 return (1, 0);
172 }
173  
174 return (1,
175 $self->xy_to_n(0,2*abs($y2)));
176 }
177  
178 1;
179 __END__