| 1 | # Copyright 2010, 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 | # ENHANCE-ME: n_to_xy() might be done with some rotates etc around its | ||
| 20 | # symmetry instead of 8 or 16 cases. | ||
| 21 | # | ||
| 22 | |||
| 23 | |||
| 24 | package Math::PlanePath::OctagramSpiral; | ||
| 25 | use 5.004; | ||
| 26 | use strict; | ||
| 27 | #use List::Util 'max'; | ||
| 28 | *max = \&Math::PlanePath::_max; | ||
| 29 | |||
| 30 | use vars '$VERSION', '@ISA'; | ||
| 31 | $VERSION = 128; | ||
| 32 | use Math::PlanePath; | ||
| 33 | @ISA = ('Math::PlanePath'); | ||
| 34 | *_sqrtint = \&Math::PlanePath::_sqrtint; | ||
| 35 | |||
| 36 | use Math::PlanePath::Base::Generic | ||
| 37 | 'round_nearest'; | ||
| 38 | |||
| 39 | # uncomment this to run the ### lines | ||
| 40 | #use Smart::Comments; | ||
| 41 | |||
| 42 | |||
| 43 | use constant xy_is_visited => 1; | ||
| 44 | use constant dx_minimum => -1; | ||
| 45 | use constant dx_maximum => 1; | ||
| 46 | use constant dy_minimum => -1; | ||
| 47 | use constant dy_maximum => 1; | ||
| 48 | *_UNDOCUMENTED__dxdy_list = \&Math::PlanePath::_UNDOCUMENTED__dxdy_list_eight; | ||
| 49 | sub x_negative_at_n { | ||
| 50 | my ($self) = @_; | ||
| 51 | return $self->n_start + 6; | ||
| 52 | } | ||
| 53 | sub y_negative_at_n { | ||
| 54 | my ($self) = @_; | ||
| 55 | return $self->n_start + 10; | ||
| 56 | } | ||
| 57 | sub _UNDOCUMENTED__dxdy_list_at_n { | ||
| 58 | my ($self) = @_; | ||
| 59 | return $self->n_start + 8; | ||
| 60 | } | ||
| 61 | use constant dsumxy_minimum => -2; # diagonals | ||
| 62 | use constant dsumxy_maximum => 2; | ||
| 63 | use constant ddiffxy_minimum => -2; | ||
| 64 | use constant ddiffxy_maximum => 2; | ||
| 65 | use constant dir_maximum_dxdy => (1,-1); # South-East | ||
| 66 | |||
| 67 | use constant parameter_info_array => | ||
| 68 | [ | ||
| 69 | Math::PlanePath::Base::Generic::parameter_info_nstart1(), | ||
| 70 | ]; | ||
| 71 | |||
| 72 | |||
| 73 | #------------------------------------------------------------------------------ | ||
| 74 | |||
| 75 | sub new { | ||
| 76 | my $self = shift->SUPER::new(@_); | ||
| 77 | if (! defined $self->{'n_start'}) { | ||
| 78 | $self->{'n_start'} = $self->default_n_start; | ||
| 79 | } | ||
| 80 | return $self; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub n_to_xy { | ||
| 84 | my ($self, $n) = @_; | ||
| 85 | #### OctagramSpiral n_to_xy: $n | ||
| 86 | |||
| 87 | # adjust to N=1 at origin X=0,Y=0 | ||
| 88 | $n = $n - $self->{'n_start'} + 1; | ||
| 89 | |||
| 90 | if ($n <= 2) { | ||
| 91 | if ($n < 1) { | ||
| 92 | return; | ||
| 93 | } else { | ||
| 94 | return ($n-1, 0); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | # sqrt() done in integers to avoid limited precision from Math::BigRat sqrt() | ||
| 99 | # | ||
| 100 | my $d = int ((_sqrtint(32*$n+17) + 7) / 16); | ||
| 101 | #### d frac: ((sqrt(32*$n+17) + 7) / 16) | ||
| 102 | #### $d | ||
| 103 | |||
| 104 | #### base: ((8*$d - 7)*$d + 1) | ||
| 105 | $n -= ((8*$d - 7)*$d + 1); | ||
| 106 | #### remainder: $n | ||
| 107 | |||
| 108 | if ($n < $d) { | ||
| 109 | return ($d + $n, $n); | ||
| 110 | } | ||
| 111 | $n -= 2*$d; | ||
| 112 | if ($n < $d) { | ||
| 113 | if ($n < 0) { | ||
| 114 | return (- $n + $d, | ||
| 115 | $d); | ||
| 116 | } else { | ||
| 117 | return ($d, | ||
| 118 | $n + $d); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | $n -= 2*$d; | ||
| 122 | |||
| 123 | if ($n < $d) { | ||
| 124 | return (-$n, | ||
| 125 | abs($n) + $d); | ||
| 126 | } | ||
| 127 | $n -= 2*$d; | ||
| 128 | |||
| 129 | if ($n < $d) { | ||
| 130 | if ($n < 0) { | ||
| 131 | return (-$d, | ||
| 132 | -$n + $d); | ||
| 133 | } else { | ||
| 134 | return (-$n - $d, | ||
| 135 | $d); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | $n -= 2*$d; | ||
| 139 | |||
| 140 | if ($n < $d) { | ||
| 141 | return (-$d-abs($n), -$n); | ||
| 142 | } | ||
| 143 | $n -= 2*$d; | ||
| 144 | |||
| 145 | if ($n < $d) { | ||
| 146 | if ($n < 0) { | ||
| 147 | return (-$d + $n, | ||
| 148 | -$d); | ||
| 149 | } else { | ||
| 150 | return (-$d, | ||
| 151 | -$d - $n); | ||
| 152 | } | ||
| 153 | } | ||
| 154 | $n -= 2*$d; | ||
| 155 | |||
| 156 | if ($n < $d) { | ||
| 157 | return ($n, | ||
| 158 | - abs($n) - $d); | ||
| 159 | } | ||
| 160 | $n -= 2*$d; | ||
| 161 | |||
| 162 | if ($n < $d+1) { | ||
| 163 | if ($n < 0) { | ||
| 164 | return ($d, | ||
| 165 | $n - $d); | ||
| 166 | } else { | ||
| 167 | return ($n + $d, | ||
| 168 | -$d); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | # $n >= $d+1 through to 2*$d+1 | ||
| 173 | return (-$n + 3*$d+2, $n - 2*$d-1); | ||
| 174 | } | ||
| 175 | |||
| 176 | sub xy_to_n { | ||
| 177 | my ($self, $x, $y) = @_; | ||
| 178 | |||
| 179 | $x = round_nearest ($x); | ||
| 180 | $y = round_nearest ($y); | ||
| 181 | ### xy_to_n: "x=$x, y=$y" | ||
| 182 | |||
| 183 | my $n; | ||
| 184 | if ($x > 0 && $y < 0 && -2*$y < $x) { | ||
| 185 | ### longer bottom right horiz | ||
| 186 | $x--; | ||
| 187 | $n = 1; | ||
| 188 | } else { | ||
| 189 | $n = 0; | ||
| 190 | } | ||
| 191 | |||
| 192 | my $d_offset = 0; | ||
| 193 | if ($y < 0) { | ||
| 194 | $y = -$y; | ||
| 195 | $x = -$x; | ||
| 196 | $d_offset = 8; | ||
| 197 | ### rotate 180 back: "$x, $y" | ||
| 198 | } | ||
| 199 | if ($x < 0) { | ||
| 200 | ($x, $y) = ($y, -$x); | ||
| 201 | $d_offset += 4; | ||
| 202 | ### rotate 90 back: "$x, $y" | ||
| 203 | } | ||
| 204 | if ($y > $x) { | ||
| 205 | ($x, $y) = ($y, $y-$x); | ||
| 206 | $d_offset += 2; | ||
| 207 | ### rotate 45 back: "$x, $y" | ||
| 208 | } | ||
| 209 | |||
| 210 | my $d; | ||
| 211 | if (2*$y < $x) { | ||
| 212 | ### diag up | ||
| 213 | $d = $x - $y; | ||
| 214 | $n += $y; | ||
| 215 | } else { | ||
| 216 | ### horiz back | ||
| 217 | $d = $y; | ||
| 218 | $n -= $x; | ||
| 219 | $d_offset += 3; | ||
| 220 | } | ||
| 221 | ### final | ||
| 222 | ### $d | ||
| 223 | ### $d_offset | ||
| 224 | ### $n | ||
| 225 | |||
| 226 | # horiz base 2,19,54,... | ||
| 227 | return $n + (8*$d - 7 + $d_offset)*$d + $self->{'n_start'}; | ||
| 228 | } | ||
| 229 | |||
| 230 | # not exact | ||
| 231 | sub rect_to_n_range { | ||
| 232 | my ($self, $x1,$y1, $x2,$y2) = @_; | ||
| 233 | |||
| 234 | my $d = max (1, map {abs(round_nearest($_))} $x1,$y1,$x2,$y2); | ||
| 235 | ### $d | ||
| 236 | |||
| 237 | # ENHANCE-ME: find actual minimum if rect doesn't cover 0,0 | ||
| 238 | return ($self->{'n_start'}, | ||
| 239 | |||
| 240 | # bottom-right inner corner 16,47,94,... | ||
| 241 | $self->{'n_start'} + (8*$d + 7)*$d); | ||
| 242 | } | ||
| 243 | |||
| 244 | 1; | ||
| 245 | __END__ |