/usr/share/perl5/Math/PlanePath/DiagonalsAlternating.pm is in libmath-planepath-perl 117-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 | # Copyright 2011, 2012, 2013, 2014 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::DiagonalsAlternating;
use 5.004;
use strict;
use vars '$VERSION', '@ISA';
$VERSION = 117;
use Math::PlanePath;
@ISA = ('Math::PlanePath');
use Math::PlanePath::Base::Generic
'round_nearest';
# uncomment this to run the ### lines
# use Smart::Comments;
use constant parameter_info_array =>
[ Math::PlanePath::Base::Generic::parameter_info_nstart1() ];
use constant class_x_negative => 0;
use constant class_y_negative => 0;
use constant dx_minimum => -1;
use constant dx_maximum => 1;
use constant dy_minimum => -1;
use constant dy_maximum => 1;
use constant 1.02 _UNDOCUMENTED__dxdy_list => (1,0, # E at N=3 in default n_start=1
0,1, # N at N=1
-1,1, # NW at N=4
1,-1, # SE at N=2
);
sub _UNDOCUMENTED__dxdy_list_at_n {
my ($self) = @_;
return $self->n_start + 3;
}
use constant dsumxy_minimum => 0; # advancing diagonals
use constant dsumxy_maximum => 1;
use constant ddiffxy_minimum => -2; # NW diagonal
use constant ddiffxy_maximum => 2; # SE diagonal
use constant dir_maximum_dxdy => (1,-1); # South-East
#------------------------------------------------------------------------------
sub new {
my $self = shift->SUPER::new(@_);
if (! defined $self->{'n_start'}) {
$self->{'n_start'} = $self->default_n_start;
}
$self->{'x_start'} ||= 0;
$self->{'y_start'} ||= 0;
return $self;
}
# \
# 15 26
# |\ \
# 14 16 25
# \ \
# . . 17 24
# \ \
# . . . 18 23
# \ \
# . . . . 19 22
# \ \
# . . . . . 20-21
# Basis N=0
# d = [ 0, 1, 2, 3 ]
# n = [ 0, 5, 14, 27 ]
# N = (2 d^2 + 3 d)
# = (2*$d**2 + 3*$d)
# = ((2*$d + 3)*$d)
# d = -3/4 + sqrt(1/2 * $n + 9/16)
# = (sqrt(8*$n + 9) - 3) / 4
#
# Midpoint
# d = [ 0, 1, 2, 3 ]
# n = [ 2, 9, 20, 35 ]
# N = (2 d^2 + 5 d + 2)
# = (2*$d**2 + 5*$d + 2)
# = ((2*$d + 5)*$d + 2)
sub n_to_xy {
my ($self, $n) = @_;
### DiagonalsAlternating n_to_xy(): "$n ".(ref $n || '')
# adjust to N=0 at origin X=0,Y=0
$n = $n - $self->{'n_start'};
if ($n < 0) { return; }
my $d = int( (sqrt(8*int($n)+9) - 3)/4 );
$n -= (2*$d + 5)*$d + 3;
### remainder: $n
my ($x,$y);
if ($n >= -1) {
if ($n < 0) {
### horizontal X axis ...
$x = $n + 2*$d+2;
$y = 0;
} else {
### diagonal upwards ...
$x = -$n + 2*$d+2;
$y = $n;
}
} else {
$n += 2*$d+2; # -1 <= $n < ...
### added n: $n
### assert: ! ($n < -1)
if ($n < 0) {
### vertical Y axis ...
$x = 0;
$y = $n + 2*$d+1;
} else {
### diagonal downwards ...
$x = $n;
$y = -$n + 2*$d+1;
}
}
return ($x + $self->{'x_start'},
$y + $self->{'y_start'});
}
sub xy_to_n {
my ($self, $x, $y) = @_;
### xy_to_n(): $x, $y
$x = $x - $self->{'x_start'}; # "-" operator to provoke warning if x==undef
$y = $y - $self->{'y_start'};
$x = round_nearest ($x);
$y = round_nearest ($y);
if ($x < 0 || $y < 0) {
return undef; # outside first quadrant
}
my $d = $x + $y;
# odd, downwards ...
# d= [ 1,3,5 ]
# N= [ 2,7,16 ]
# N = ((1/2*$d + 1/2)*$d + 1)
#
# even, upwards
# d= [ 0,2,4 ]
# N= [ 1,4,11 ]
# N = ((1/2*$d + 1/2)*$d + 1)
# = ($d + 1)*$d/2 + 1
my $n = ($d + 1)*$d/2 + $self->{'n_start'};
if ($d % 2) {
return $n + $x;
} else {
return $n + $y;
}
}
use Math::PlanePath::Diagonals;
*rect_to_n_range = \&Math::PlanePath::Diagonals::rect_to_n_range;
1;
__END__
=for stopwords PlanePath Ryde Math-PlanePath OEIS
=head1 NAME
Math::PlanePath::DiagonalsAlternating -- points in diagonal stripes of alternating directions
=head1 SYNOPSIS
use Math::PlanePath::DiagonalsAlternating;
my $path = Math::PlanePath::DiagonalsAlternating->new;
my ($x, $y) = $path->n_to_xy (123);
=head1 DESCRIPTION
This path follows successive diagonals going from the Y axis down to the X
axis and then back again,
=cut
# math-image --path=DiagonalsAlternating --expression='i<=31?i:0' --output=numbers
=pod
7 | 29
6 | 28 30
5 | 16 27 31
4 | 15 17 26 ...
3 | 7 14 18 25
2 | 6 8 13 19 24
1 | 2 5 9 12 20 23
Y=0 | 1 3 4 10 11 21 22
+----------------------------
X=0 1 2 3 4 5 6
X<Triangular numbers>The triangular numbers 1,3,6,10,etc k*(k+1)/2 are the
start of each run up or down alternately on the X axis and Y axis.
X<Hexagonal numbers>N=1,6,15,28,etc on the Y axis (Y even) are the hexagonal
numbers j*(2j-1). N=3,10,21,36,etc on the X axis (X odd) are the hexagonal
numbers of the second kind j*(2j+1).
=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=DiagonalsAlternating,n_start=0 --expression='i<=14?i:0' --output=numbers --size=35x5
=pod
n_start => 0
4 | 14
3 | 6 13
2 | 5 7 12
1 | 1 4 8 11
Y=0 | 0 2 3 9 10
+-----------------
X=0 1 2 3 4
=head1 FUNCTIONS
See L<Math::PlanePath/FUNCTIONS> for behaviour common to all path classes.
=over 4
=item C<$path = Math::PlanePath::DiagonalsAlternating-E<gt>new ()>
=item C<$path = Math::PlanePath::DiagonalsAlternating-E<gt>new (n_start =E<gt> $n)>
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 E<lt> 1> the return is an empty list, it being considered the path
begins at 1.
=back
=head1 FORMULAS
=head2 Rectangle to N Range
Within each row increasing X is increasing N, and in each column increasing
Y is increasing N. So in a rectangle the lower left corner is the minimum N
and the upper right is the maximum N.
| N max
| ----------+
| | ^ |
| | | |
| | ----> |
| +----------
| N min
+-------------------
=head1 OEIS
Entries in Sloane's Online Encyclopedia of Integer Sequences related to this
path include
=over
L<http://oeis.org/A131179> (etc)
=back
n_start=1
A131179 N on X axis (extra initial 0)
A128918 N on Y axis (extra initial 1)
A001844 N on X=Y diagonal
A038722 permutation N at transpose Y,X
n_start=0
A003056 X+Y
A004247 X*Y
A049581 abs(X-Y)
A048147 X^2+Y^2
A004198 X bit-and Y
A003986 X bit-or Y
A003987 X bit-xor Y
A004197 min(X,Y)
A003984 max(X,Y)
A101080 HammingDist(X,Y)
A023531 dSum = dX+dY, being 1 at N=triangular+1 (and 0)
A046092 N on X=Y diagonal
A061579 permutation N at transpose Y,X
A056011 permutation N at points by Diagonals,direction=up order
A056023 permutation N at points by Diagonals,direction=down
runs alternately up and down, both are self-inverse
The coordinates such as A003056 X+Y are the same here as in the Diagonals
path. C<DiagonalsAlternating> transposes X,Y -E<gt> Y,X in every second
diagonal but forms such as X+Y are unchanged by swapping to Y+X.
=head1 SEE ALSO
L<Math::PlanePath>,
L<Math::PlanePath::Diagonals>,
L<Math::PlanePath::DiagonalsOctant>
=head1 HOME PAGE
L<http://user42.tuxfamily.org/math-planepath/index.html>
=head1 LICENSE
Copyright 2010, 2011, 2012, 2013, 2014 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
|