PlanePath/MPeaks.pm untested

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 162 CC 15 SCAR 31.1
CC
15
Cov
0%
rect_to_n_range
72.0
n_to_xy
20.0
xy_to_n
20.0
CRAP 22.3
1 # Copyright 2011, 2012, 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 package Math::PlanePath::MPeaks;
20 use 5.004;
21 use strict;
22 use List::Util 'min';
23 #use List::Util 'max';
24 *max = \&Math::PlanePath::_max;
25  
26 use vars '$VERSION', '@ISA';
27 $VERSION = 128;
28 use Math::PlanePath;
29 @ISA = ('Math::PlanePath');
30 *_sqrtint = \&Math::PlanePath::_sqrtint;
31  
32 use Math::PlanePath::Base::Generic
33 'round_nearest';
34  
35 # uncomment this to run the ### lines
36 #use Smart::Comments;
37  
38  
39 use constant class_y_negative => 0;
40 use constant n_frac_discontinuity => .5;
41 *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad12;
42  
43 sub x_negative_at_n {
44 my ($self) = @_;
45 return $self->n_start;
46 }
47 # dX jumps back unbounded negative, but forward only +1
48 use constant dx_maximum => 1;
49 use constant dy_minimum => -1;
50 use constant dy_maximum => 1;
51 use constant dsumxy_maximum => 2; # NE diagonal
52 use constant ddiffxy_maximum => 2; # SE diagonal
53 use constant dir_minimum_dxdy => (1,1); # North-East
54 use constant dir_maximum_dxdy => (1,-1); # South-East
55  
56 use constant parameter_info_array =>
57 [
58 Math::PlanePath::Base::Generic::parameter_info_nstart1(),
59 ];
60  
61  
62 #------------------------------------------------------------------------------
63  
64 sub new {
65 my $self = shift->SUPER::new(@_);
66 if (! defined $self->{'n_start'}) {
67 $self->{'n_start'} = $self->default_n_start;
68 }
69 return $self;
70 }
71  
72 # starting each left side at 0.5 before
73 # [ 1,2,3 ],
74 # [ 1-0.5, 6-0.5, 17-0.5 ]
75 # N = (3 d^2 - 4 d + 3/2)
76 # = (3*$d**2 - 4*$d + 3/2)
77 # = ((3*$d - 4)*$d + 3/2)
78 # d = 2/3 + sqrt(1/3 * $n + -1/18)
79 # = (2 + 3*sqrt(1/3 * $n - 1/18))/3
80 # = (2 + sqrt(3 * $n - 1/2))/3
81 # = (4 + 2*sqrt(3 * $n - 1/2))/6
82 # = (4 + sqrt(12*$n - 2))/6
83 # at n=1/2 d=(4+sqrt(12/2-2))/6 = (4+sqrt(4))/6 = 1
84 #
85 # base at Y=0
86 # [ 1, 6, 17 ]
87 # N = (3 d^2 - 4 d + 2)
88 # = (3*$d**2 - 4*$d + 2)
89 # = ((3*$d - 4)*$d + 2)
90 #
91 # centre
92 # [ 3,11,25 ]
93 # N = (3 d^2 - d + 1)
94 # = (3*$d**2 - $d + 1)
95 # = ((3*$d - 1)*$d + 1)
96 #
97 sub n_to_xy {
98 my ($self, $n) = @_;
99 ### MPeaks n_to_xy(): $n
100  
101 # adjust to N=0 at start X=-1,Y=0
102 $n = $n - $self->{'n_start'};
103  
104 my $d;
105 {
106 my $r = 12*$n + 10;
107 if ($r < 4) {
108 return; # N < -0.5, so before start of path
109 }
110 $d = int( (_sqrtint($r) + 4)/6 );
111 }
112 $n -= (3*$d - 1)*$d; # to $n==0 at centre
113 ### $d
114 ### remainder: $n
115  
116 if ($n >= $d) {
117 ### right vertical ...
118 # N-d is top of right peak
119 # N-(3d-1) = N-3d+1 is right Y=0
120 # Y=-(N-2d+1)= -N+3d-1
121 return ($d,
122 -$n + 3*$d - 1);
123 }
124 if ($n <= (my $neg_d = -$d)) {
125 ### left vertical ...
126 # N+(3d-1) is left Y=0
127 # Y=N+3d-1
128 return ($neg_d,
129 $n + 3*$d - 1);
130 }
131 ### centre diagonals ...
132 return ($n,
133 abs($n) + $d-1);
134 }
135  
136 sub xy_to_n {
137 my ($self, $x, $y) = @_;
138 ### MPeaks xy_to_n(): $x, $y
139  
140 $y = round_nearest ($y);
141 if ($y < 0) {
142 return undef;
143 }
144 $x = round_nearest ($x);
145  
146 {
147 my $two_x = 2*$x;
148 if ($two_x > $y) {
149 ### right vertical ...
150 # right end [ 5,16,33 ]
151 # N = (3 x^2 + 2 x)
152 return (3*$x+2)*$x - $y + $self->{'n_start'} - 1;
153 }
154 if ($two_x < -$y) {
155 ### left vertical ...
156 # Nleftend = (3 d^2 - 4 d + 2)
157 # = (3x+4)x + 2
158 return (3*$x+4)*$x + 1 + $y + $self->{'n_start'};
159 }
160 }
161  
162 ### centre diagonals ...
163 # d=Y+abs(x) with d=0 first (not d=1 as above), N=(3 d^2 + 5 d + 3)
164 my $d = $y - abs($x);
165 ### $d
166 return (3*$d+5)*$d + 2 + $x + $self->{'n_start'};
167 }
168  
169 # not exact
170 sub rect_to_n_range {
171 my ($self, $x1,$y1, $x2,$y2) = @_;
172  
173 $y1 = round_nearest ($y1);
174 $y2 = round_nearest ($y2);
175  
176 if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); } # swap to y1<=y2
177 if ($y2 < 0) {
178 return (1, 0); # rect all negative, no N
179 }
180 if ($y1 < 0) { $y1 *= 0; } # "*=" to preserve bigint y1
181  
182 $x1 = round_nearest ($x1);
183 $x2 = round_nearest ($x2);
184 if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); } # swap to x1<=x2
185  
186 my $zero = $x1 * 0 * $x2;
187  
188 # columns X<0 are increasing with increasing Y
189 # columns X>0 increase below Y=2*X
190 #
191 return ($self->{'n_start'},
192 max (
193 # left column
194 $self->xy_to_n($x1,
195 ($y2 >= 2*$x1 ? $y2 : $y1)),
196  
197 # right column
198 $self->xy_to_n($x2,
199 ($y2 >= 2*$x2 ? $y2 : $y1)),
200  
201 # top row centre X=0, if it's covered by x1,x2
202 ($x1 < 0 && $x2 > 0
203 ? $self->xy_to_n($zero,$y2)
204 : ())));
205 }
206  
207 # No, because N decreases in right hand columns
208 # return (1,
209 # max ($self->xy_to_n($x1,$y2),
210 # $self->xy_to_n($x2,$y2),
211 # # and at X=0 if it's covered by x1,x2
212 # ($x1 < 0 && $x2 > 0 ? $self->xy_to_n($zero,$y2) : ()));
213  
214 # my @n;
215 # if ($y1 <= 2*$x2) {
216 # # right vertical
217 # push @n, (3*$x2+2)*$x2 - $y1;
218 # }
219 # if (($x1 > 0) != ($x2 > 0)) {
220 # # centre vertical
221 # return (3*$y2+5)*$y2 + 3;
222 # }
223  
224 1;
225 __END__