/usr/share/perl5/Math/PlanePath/MPeaks.pm is in libmath-planepath-perl 122-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | # Copyright 2011, 2012, 2013, 2014, 2015 Kevin Ryde
# This file is part of Math-PlanePath.
#
# Math-PlanePath is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-PlanePath is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
package Math::PlanePath::MPeaks;
use 5.004;
use strict;
use List::Util 'min';
#use List::Util 'max';
*max = \&Math::PlanePath::_max;
use vars '$VERSION', '@ISA';
$VERSION = 122;
use Math::PlanePath;
@ISA = ('Math::PlanePath');
use Math::PlanePath::Base::Generic
'round_nearest';
# uncomment this to run the ### lines
#use Smart::Comments;
use constant class_y_negative => 0;
use constant n_frac_discontinuity => .5;
*xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad12;
sub x_negative_at_n {
my ($self) = @_;
return $self->n_start;
}
# dX jumps back unbounded negative, but forward only +1
use constant dx_maximum => 1;
use constant dy_minimum => -1;
use constant dy_maximum => 1;
use constant dsumxy_maximum => 2; # NE diagonal
use constant ddiffxy_maximum => 2; # SE diagonal
use constant dir_minimum_dxdy => (1,1); # North-East
use constant dir_maximum_dxdy => (1,-1); # South-East
use constant parameter_info_array =>
[
Math::PlanePath::Base::Generic::parameter_info_nstart1(),
];
#------------------------------------------------------------------------------
sub new {
my $self = shift->SUPER::new(@_);
if (! defined $self->{'n_start'}) {
$self->{'n_start'} = $self->default_n_start;
}
return $self;
}
# starting each left side at 0.5 before
# [ 1,2,3 ],
# [ 1-0.5, 6-0.5, 17-0.5 ]
# N = (3 d^2 - 4 d + 3/2)
# = (3*$d**2 - 4*$d + 3/2)
# = ((3*$d - 4)*$d + 3/2)
# d = 2/3 + sqrt(1/3 * $n + -1/18)
# = (2 + 3*sqrt(1/3 * $n - 1/18))/3
# = (2 + sqrt(3 * $n - 1/2))/3
# = (4 + 2*sqrt(3 * $n - 1/2))/6
# = (4 + sqrt(12*$n - 2))/6
# at n=1/2 d=(4+sqrt(12/2-2))/6 = (4+sqrt(4))/6 = 1
#
# base at Y=0
# [ 1, 6, 17 ]
# N = (3 d^2 - 4 d + 2)
# = (3*$d**2 - 4*$d + 2)
# = ((3*$d - 4)*$d + 2)
#
# centre
# [ 3,11,25 ]
# N = (3 d^2 - d + 1)
# = (3*$d**2 - $d + 1)
# = ((3*$d - 1)*$d + 1)
#
sub n_to_xy {
my ($self, $n) = @_;
### MPeaks n_to_xy(): $n
# adjust to N=0 at start X=-1,Y=0
$n = $n - $self->{'n_start'};
my $d;
{
my $r = 12*$n + 10;
if ($r < 4) {
return; # N < -0.5, so before start of path
}
$d = int( (sqrt(int($r)) + 4)/6 );
}
$n -= (3*$d - 1)*$d; # to $n==0 at centre
### $d
### remainder: $n
if ($n >= $d) {
### right vertical ...
# N-d is top of right peak
# N-(3d-1) = N-3d+1 is right Y=0
# Y=-(N-2d+1)= -N+3d-1
return ($d,
-$n + 3*$d - 1);
}
if ($n <= (my $neg_d = -$d)) {
### left vertical ...
# N+(3d-1) is left Y=0
# Y=N+3d-1
return ($neg_d,
$n + 3*$d - 1);
}
### centre diagonals ...
return ($n,
abs($n) + $d-1);
}
sub xy_to_n {
my ($self, $x, $y) = @_;
### MPeaks xy_to_n(): $x, $y
$y = round_nearest ($y);
if ($y < 0) {
return undef;
}
$x = round_nearest ($x);
{
my $two_x = 2*$x;
if ($two_x > $y) {
### right vertical ...
# right end [ 5,16,33 ]
# N = (3 x^2 + 2 x)
return (3*$x+2)*$x - $y + $self->{'n_start'} - 1;
}
if ($two_x < -$y) {
### left vertical ...
# Nleftend = (3 d^2 - 4 d + 2)
# = (3x+4)x + 2
return (3*$x+4)*$x + 1 + $y + $self->{'n_start'};
}
}
### centre diagonals ...
# d=Y+abs(x) with d=0 first (not d=1 as above), N=(3 d^2 + 5 d + 3)
my $d = $y - abs($x);
### $d
return (3*$d+5)*$d + 2 + $x + $self->{'n_start'};
}
# not exact
sub rect_to_n_range {
my ($self, $x1,$y1, $x2,$y2) = @_;
$y1 = round_nearest ($y1);
$y2 = round_nearest ($y2);
if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); } # swap to y1<=y2
if ($y2 < 0) {
return (1, 0); # rect all negative, no N
}
if ($y1 < 0) { $y1 *= 0; } # "*=" to preserve bigint y1
$x1 = round_nearest ($x1);
$x2 = round_nearest ($x2);
if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); } # swap to x1<=x2
my $zero = $x1 * 0 * $x2;
# columns X<0 are increasing with increasing Y
# columns X>0 increase below Y=2*X
#
return ($self->{'n_start'},
max (
# left column
$self->xy_to_n($x1,
($y2 >= 2*$x1 ? $y2 : $y1)),
# right column
$self->xy_to_n($x2,
($y2 >= 2*$x2 ? $y2 : $y1)),
# top row centre X=0, if it's covered by x1,x2
($x1 < 0 && $x2 > 0
? $self->xy_to_n($zero,$y2)
: ())));
}
# No, because N decreases in right hand columns
# return (1,
# max ($self->xy_to_n($x1,$y2),
# $self->xy_to_n($x2,$y2),
# # and at X=0 if it's covered by x1,x2
# ($x1 < 0 && $x2 > 0 ? $self->xy_to_n($zero,$y2) : ()));
# my @n;
# if ($y1 <= 2*$x2) {
# # right vertical
# push @n, (3*$x2+2)*$x2 - $y1;
# }
# if (($x1 > 0) != ($x2 > 0)) {
# # centre vertical
# return (3*$y2+5)*$y2 + 3;
# }
1;
__END__
=for stopwords Ryde Math-PlanePath ie OEIS
=head1 NAME
Math::PlanePath::MPeaks -- points in expanding M shape
=head1 SYNOPSIS
use Math::PlanePath::MPeaks;
my $path = Math::PlanePath::MPeaks->new;
my ($x, $y) = $path->n_to_xy (123);
=head1 DESCRIPTION
This path puts points in layers of an "M" shape
=cut
# math-image --path=MPeaks --expression='i<=56?i:0' --output=numbers --size=50x10
=pod
41 49 7
40 42 48 50 6
39 22 43 47 28 51 5
38 21 23 44 46 27 29 52 4
37 20 9 24 45 26 13 30 53 3
36 19 8 10 25 12 14 31 54 2
35 18 7 2 11 4 15 32 55 1
34 17 6 1 3 5 16 33 56 <- Y=0
^
-4 -3 -2 -1 X=0 1 2 3 4
N=1 to N=5 is the first "M" shape, then N=6 to N=16 on top of that, etc.
The centre goes half way down. Reckoning the N=1 to N=5 as layer d=1 then
Xleft = -d
Xright = d
Ypeak = 2*d - 1
Ycentre = d - 1
Each "M" is 6 points longer than the preceding. The verticals are each 2
longer, and the centre diagonals each 1 longer. This step 6 is similar to
the C<HexSpiral>.
The octagonal numbers N=1,8,21,40,65,etc k*(3k-2) are a straight line
of slope 2 going up to the left. The octagonal numbers of the second
kind N=5,16,33,56,etc k*(3k+2) are along the X axis to the right.
=head2 N Start
The default is to number points starting N=1 as shown above. An optional
C<n_start> can give a different start, in the same pattern. For example to
start at 0,
=cut
# math-image --path=MPeaks,n_start=0 --expression='i<=55?i:0' --output=numbers --size=50x10
=pod
n_start => 0
40 48
39 41 47 49
38 21 42 46 27 50
37 20 22 43 45 26 28 51
36 19 8 23 44 25 12 29 52
35 18 7 9 24 11 13 30 53
34 17 6 1 10 3 14 31 54
33 16 5 0 2 4 15 32 55
=head1 FUNCTIONS
See L<Math::PlanePath/FUNCTIONS> for behaviour common to all path classes.
=over 4
=item C<$path = Math::PlanePath::MPeaks-E<gt>new ()>
Create and return a new path object.
=item C<($x,$y) = $path-E<gt>n_to_xy ($n)>
Return the X,Y coordinates of point number C<$n> on the path.
For C<$n < 0.5> the return is an empty list, it being considered there are
no negative points.
=item C<$n = $path-E<gt>xy_to_n ($x,$y)>
Return the point number for coordinates C<$x,$y>. C<$x> and C<$y> are each
rounded to the nearest integer which has the effect of treating points as a
squares of side 1, so the half-plane y>=-0.5 is entirely covered.
=back
=head1 OEIS
Entries in Sloane's Online Encyclopedia of Integer Sequences related to this
path include
=over
L<http://oeis.org/A045944> (etc)
=back
n_start=1 (the default)
A045944 N on X axis >= 1, extra initial 0
being octagonal numbers second kind
A056106 N on Y axis, extra initial 1
A056109 N on X negative axis <= -1
n_start=0
A049450 N on Y axis, extra initial 0, 2*pentagonal
n_start=2
A027599 N on Y axis, extra initial 6,2
=head1 SEE ALSO
L<Math::PlanePath>,
L<Math::PlanePath::PyramidSides>
=head1 HOME PAGE
L<http://user42.tuxfamily.org/math-planepath/index.html>
=head1 LICENSE
Copyright 2011, 2012, 2013, 2014, 2015 Kevin Ryde
This file is part of Math-PlanePath.
Math-PlanePath is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
Math-PlanePath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along with
Math-PlanePath. If not, see <http://www.gnu.org/licenses/>.
=cut
# Local variables:
# compile-command: "math-image --path=MPeaks --lines --scale=20"
# End:
#
# math-image --path=MPeaks --all --output=numbers
|