| 1 | # Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 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 | package Math::PlanePath::DiagonalsAlternating; | ||
| 19 | use 5.004; | ||
| 20 | use strict; | ||
| 21 | |||
| 22 | use vars '$VERSION', '@ISA'; | ||
| 23 | $VERSION = 128; | ||
| 24 | use Math::PlanePath; | ||
| 25 | @ISA = ('Math::PlanePath'); | ||
| 26 | |||
| 27 | use Math::PlanePath::Base::Generic | ||
| 28 | 'round_nearest'; | ||
| 29 | *_sqrtint = \&Math::PlanePath::_sqrtint; | ||
| 30 | |||
| 31 | # uncomment this to run the ### lines | ||
| 32 | # use Smart::Comments; | ||
| 33 | |||
| 34 | use constant parameter_info_array => | ||
| 35 | [ Math::PlanePath::Base::Generic::parameter_info_nstart1() ]; | ||
| 36 | |||
| 37 | use constant class_x_negative => 0; | ||
| 38 | use constant class_y_negative => 0; | ||
| 39 | |||
| 40 | use constant dx_minimum => -1; | ||
| 41 | use constant dx_maximum => 1; | ||
| 42 | use constant dy_minimum => -1; | ||
| 43 | use constant dy_maximum => 1; | ||
| 44 | use constant 1.02 _UNDOCUMENTED__dxdy_list => (1,0, # E at N=3 in default n_start=1 | ||
| 45 | 0,1, # N at N=1 | ||
| 46 | -1,1, # NW at N=4 | ||
| 47 | 1,-1, # SE at N=2 | ||
| 48 | ); | ||
| 49 | sub _UNDOCUMENTED__dxdy_list_at_n { | ||
| 50 | my ($self) = @_; | ||
| 51 | return $self->n_start + 3; | ||
| 52 | } | ||
| 53 | use constant dsumxy_minimum => 0; # advancing diagonals | ||
| 54 | use constant dsumxy_maximum => 1; | ||
| 55 | use constant ddiffxy_minimum => -2; # NW diagonal | ||
| 56 | use constant ddiffxy_maximum => 2; # SE diagonal | ||
| 57 | use constant dir_maximum_dxdy => (1,-1); # South-East | ||
| 58 | |||
| 59 | |||
| 60 | #------------------------------------------------------------------------------ | ||
| 61 | |||
| 62 | sub new { | ||
| 63 | my $self = shift->SUPER::new(@_); | ||
| 64 | if (! defined $self->{'n_start'}) { | ||
| 65 | $self->{'n_start'} = $self->default_n_start; | ||
| 66 | } | ||
| 67 | $self->{'x_start'} ||= 0; | ||
| 68 | $self->{'y_start'} ||= 0; | ||
| 69 | return $self; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | # \ | ||
| 74 | # 15 26 | ||
| 75 | # |\ \ | ||
| 76 | # 14 16 25 | ||
| 77 | # \ \ | ||
| 78 | # . . 17 24 | ||
| 79 | # \ \ | ||
| 80 | # . . . 18 23 | ||
| 81 | # \ \ | ||
| 82 | # . . . . 19 22 | ||
| 83 | # \ \ | ||
| 84 | # . . . . . 20-21 | ||
| 85 | |||
| 86 | # Basis N=0 | ||
| 87 | # d = [ 0, 1, 2, 3 ] | ||
| 88 | # n = [ 0, 5, 14, 27 ] | ||
| 89 | # N = (2 d^2 + 3 d) | ||
| 90 | # = (2*$d**2 + 3*$d) | ||
| 91 | # = ((2*$d + 3)*$d) | ||
| 92 | # d = -3/4 + sqrt(1/2 * $n + 9/16) | ||
| 93 | # = (sqrt(8*$n + 9) - 3) / 4 | ||
| 94 | # | ||
| 95 | # Midpoint | ||
| 96 | # d = [ 0, 1, 2, 3 ] | ||
| 97 | # n = [ 2, 9, 20, 35 ] | ||
| 98 | # N = (2 d^2 + 5 d + 2) | ||
| 99 | # = (2*$d**2 + 5*$d + 2) | ||
| 100 | # = ((2*$d + 5)*$d + 2) | ||
| 101 | |||
| 102 | sub n_to_xy { | ||
| 103 | my ($self, $n) = @_; | ||
| 104 | ### DiagonalsAlternating n_to_xy(): "$n ".(ref $n || '') | ||
| 105 | |||
| 106 | # adjust to N=0 at origin X=0,Y=0 | ||
| 107 | $n = $n - $self->{'n_start'}; | ||
| 108 | if ($n < 0) { return; } | ||
| 109 | |||
| 110 | my $d = int( (_sqrtint(8*int($n)+9) - 3)/4 ); | ||
| 111 | $n -= (2*$d + 5)*$d + 3; | ||
| 112 | ### remainder: $n | ||
| 113 | |||
| 114 | my ($x,$y); | ||
| 115 | if ($n >= -1) { | ||
| 116 | if ($n < 0) { | ||
| 117 | ### horizontal X axis ... | ||
| 118 | $x = $n + 2*$d+2; | ||
| 119 | $y = 0; | ||
| 120 | } else { | ||
| 121 | ### diagonal upwards ... | ||
| 122 | $x = -$n + 2*$d+2; | ||
| 123 | $y = $n; | ||
| 124 | } | ||
| 125 | } else { | ||
| 126 | $n += 2*$d+2; # -1 <= $n < ... | ||
| 127 | ### added n: $n | ||
| 128 | ### assert: ! ($n < -1) | ||
| 129 | |||
| 130 | if ($n < 0) { | ||
| 131 | ### vertical Y axis ... | ||
| 132 | $x = 0; | ||
| 133 | $y = $n + 2*$d+1; | ||
| 134 | } else { | ||
| 135 | ### diagonal downwards ... | ||
| 136 | $x = $n; | ||
| 137 | $y = -$n + 2*$d+1; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | return ($x + $self->{'x_start'}, | ||
| 142 | $y + $self->{'y_start'}); | ||
| 143 | } | ||
| 144 | |||
| 145 | sub xy_to_n { | ||
| 146 | my ($self, $x, $y) = @_; | ||
| 147 | ### xy_to_n(): $x, $y | ||
| 148 | |||
| 149 | $x = $x - $self->{'x_start'}; # "-" operator to provoke warning if x==undef | ||
| 150 | $y = $y - $self->{'y_start'}; | ||
| 151 | $x = round_nearest ($x); | ||
| 152 | $y = round_nearest ($y); | ||
| 153 | if ($x < 0 || $y < 0) { | ||
| 154 | return undef; # outside first quadrant | ||
| 155 | } | ||
| 156 | |||
| 157 | my $d = $x + $y; | ||
| 158 | |||
| 159 | # odd, downwards ... | ||
| 160 | # d= [ 1,3,5 ] | ||
| 161 | # N= [ 2,7,16 ] | ||
| 162 | # N = ((1/2*$d + 1/2)*$d + 1) | ||
| 163 | # | ||
| 164 | # even, upwards | ||
| 165 | # d= [ 0,2,4 ] | ||
| 166 | # N= [ 1,4,11 ] | ||
| 167 | # N = ((1/2*$d + 1/2)*$d + 1) | ||
| 168 | # = ($d + 1)*$d/2 + 1 | ||
| 169 | |||
| 170 | my $n = ($d + 1)*$d/2 + $self->{'n_start'}; | ||
| 171 | if ($d % 2) { | ||
| 172 | return $n + $x; | ||
| 173 | } else { | ||
| 174 | return $n + $y; | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | use Math::PlanePath::Diagonals; | ||
| 179 | *rect_to_n_range = \&Math::PlanePath::Diagonals::rect_to_n_range; | ||
| 180 | |||
| 181 | 1; | ||
| 182 | __END__ |