| 1 | # Copyright 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 | |||
| 20 | |||
| 21 | # math-image --path=R5DragonMidpoint --lines --scale=40 | ||
| 22 | # | ||
| 23 | # math-image --path=R5DragonMidpoint --all --output=numbers_dash --size=78x60 | ||
| 24 | # math-image --path=R5DragonMidpoint,arms=6 --all --output=numbers_dash --size=78x60 | ||
| 25 | |||
| 26 | |||
| 27 | package Math::PlanePath::R5DragonMidpoint; | ||
| 28 | use 5.004; | ||
| 29 | use strict; | ||
| 30 | use List::Util 'min'; # 'max' | ||
| 31 | *max = \&Math::PlanePath::_max; | ||
| 32 | |||
| 33 | use vars '$VERSION', '@ISA'; | ||
| 34 | $VERSION = 128; | ||
| 35 | |||
| 36 | use Math::PlanePath; | ||
| 37 | *_divrem_mutate = \&Math::PlanePath::_divrem_mutate; | ||
| 38 | use Math::PlanePath::Base::NSEW; | ||
| 39 | @ISA = ('Math::PlanePath::Base::NSEW', | ||
| 40 | 'Math::PlanePath'); | ||
| 41 | |||
| 42 | use Math::PlanePath::Base::Generic | ||
| 43 | 'is_infinite', | ||
| 44 | 'round_nearest'; | ||
| 45 | use Math::PlanePath::Base::Digits | ||
| 46 | 'round_up_pow', | ||
| 47 | 'digit_join_lowtohigh'; | ||
| 48 | |||
| 49 | # uncomment this to run the ### lines | ||
| 50 | # use Smart::Comments; | ||
| 51 | |||
| 52 | |||
| 53 | use constant n_start => 0; | ||
| 54 | use constant parameter_info_array => [ { name => 'arms', | ||
| 55 | share_key => 'arms_4', | ||
| 56 | display => 'Arms', | ||
| 57 | type => 'integer', | ||
| 58 | minimum => 1, | ||
| 59 | maximum => 4, | ||
| 60 | default => 1, | ||
| 61 | width => 1, | ||
| 62 | description => 'Arms', | ||
| 63 | } ]; | ||
| 64 | |||
| 65 | { | ||
| 66 | my @x_negative_at_n = (undef, 14,5,2,2); | ||
| 67 | sub x_negative_at_n { | ||
| 68 | my ($self) = @_; | ||
| 69 | return $x_negative_at_n[$self->{'arms'}]; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | { | ||
| 73 | my @y_negative_at_n = (undef, 93,35,11,7); | ||
| 74 | sub y_negative_at_n { | ||
| 75 | my ($self) = @_; | ||
| 76 | return $y_negative_at_n[$self->{'arms'}]; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | #------------------------------------------------------------------------------ | ||
| 82 | |||
| 83 | sub new { | ||
| 84 | my $self = shift->SUPER::new(@_); | ||
| 85 | $self->{'arms'} = max(1, min(4, $self->{'arms'} || 1)); | ||
| 86 | return $self; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub n_to_xy { | ||
| 90 | my ($self, $n) = @_; | ||
| 91 | ### R5DragonMidpoint n_to_xy(): $n | ||
| 92 | |||
| 93 | if ($n < 0) { return; } | ||
| 94 | if (is_infinite($n)) { return ($n, $n); } | ||
| 95 | |||
| 96 | { | ||
| 97 | my $int = int($n); | ||
| 98 | if ($n != $int) { | ||
| 99 | my ($x1,$y1) = $self->n_to_xy($int); | ||
| 100 | my ($x2,$y2) = $self->n_to_xy($int+$self->{'arms'}); | ||
| 101 | my $frac = $n - $int; # inherit possible BigFloat | ||
| 102 | my $dx = $x2-$x1; | ||
| 103 | my $dy = $y2-$y1; | ||
| 104 | return ($frac*$dx + $x1, $frac*$dy + $y1); | ||
| 105 | } | ||
| 106 | $n = $int; # BigFloat int() gives BigInt, use that | ||
| 107 | } | ||
| 108 | |||
| 109 | # ENHANCE-ME: own code ... | ||
| 110 | # | ||
| 111 | require Math::PlanePath::R5DragonCurve; | ||
| 112 | my ($x1,$y1) = $self->Math::PlanePath::R5DragonCurve::n_to_xy($n); | ||
| 113 | my ($x2,$y2) = $self->Math::PlanePath::R5DragonCurve::n_to_xy($n+$self->{'arms'}); | ||
| 114 | |||
| 115 | # x = x1+x2 | ||
| 116 | # y = y1+y2 | ||
| 117 | # rotx = x+y = (y1+y2)+(x1+x2) | ||
| 118 | # roty = y-x = (y1+y2)-(x1+x2) | ||
| 119 | # cx = (rotx-1)/2 | ||
| 120 | # cy = (rotx+1)/2 | ||
| 121 | |||
| 122 | $x1 += $x2; | ||
| 123 | $y1 += $y2; | ||
| 124 | return (($x1+$y1-1)/2, | ||
| 125 | ($y1-$x1+1)/2); | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | # table of triplets $ndigit,$dx,$dy for ($y%10)*10+($x%10), total 300 entries | ||
| 130 | my @yx_to_digdxdy # 30 each row | ||
| 131 | = (0,0,0, 1,-1,0, 1,0,-1, 2,-1,-1, 3,-2,-1, | ||
| 132 | 3,-1,-2, 4,-2,-2, 0,-2,0, 2,-1,-1, 4,0,-2, | ||
| 133 | 4,-2,0, 2,-1,-1, 0,0,-2, 4,0,0, 3,-1,0, | ||
| 134 | 3,0,-1, 2,-1,-1, 1,-2,-1, 1,-1,-2, 0,-2,-2, | ||
| 135 | 3,-2,-1, 3,-1,-2, 4,-2,-2, 0,-2,0, 2,-1,-1, | ||
| 136 | 4,0,-2, 0,0,0, 1,-1,0, 1,0,-1, 2,-1,-1, | ||
| 137 | 3,-1,0, 3,0,-1, 2,-1,-1, 1,-2,-1, 1,-1,-2, | ||
| 138 | 0,-2,-2, 4,-2,0, 2,-1,-1, 0,0,-2, 4,0,0, | ||
| 139 | 2,-1,-1, 4,0,-2, 0,0,0, 1,-1,0, 1,0,-1, | ||
| 140 | 2,-1,-1, 3,-2,-1, 3,-1,-2, 4,-2,-2, 0,-2,0, | ||
| 141 | 1,-1,-2, 0,-2,-2, 4,-2,0, 2,-1,-1, 0,0,-2, | ||
| 142 | 4,0,0, 3,-1,0, 3,0,-1, 2,-1,-1, 1,-2,-1, | ||
| 143 | 1,0,-1, 2,-1,-1, 3,-2,-1, 3,-1,-2, 4,-2,-2, | ||
| 144 | 0,-2,0, 2,-1,-1, 4,0,-2, 0,0,0, 1,-1,0, | ||
| 145 | 0,0,-2, 4,0,0, 3,-1,0, 3,0,-1, 2,-1,-1, | ||
| 146 | 1,-2,-1, 1,-1,-2, 0,-2,-2, 4,-2,0, 2,-1,-1, | ||
| 147 | 4,-2,-2, 0,-2,0, 2,-1,-1, 4,0,-2, 0,0,0, | ||
| 148 | 1,-1,0, 1,0,-1, 2,-1,-1, 3,-2,-1, 3,-1,-2, | ||
| 149 | 2,-1,-1, 1,-2,-1, 1,-1,-2, 0,-2,-2, 4,-2,0, | ||
| 150 | 2,-1,-1, 0,0,-2, 4,0,0, 3,-1,0, 3,0,-1, | ||
| 151 | ); | ||
| 152 | |||
| 153 | # arm $x $y 2 | 1 Y=1 | ||
| 154 | # 0 0 0 3 | 0 Y=0 | ||
| 155 | # 1 0 1 ----+---- | ||
| 156 | # 2 -1 1 X=-1 X=0 | ||
| 157 | # 3 -1 0 | ||
| 158 | my @xy_to_arm = ([0, # x=0,y=0 | ||
| 159 | 1], # x=0,y=1 | ||
| 160 | [3, # x=-1,y=0 | ||
| 161 | 2]); # x=-1,y=1 | ||
| 162 | |||
| 163 | sub xy_to_n { | ||
| 164 | my ($self, $x, $y) = @_; | ||
| 165 | ### R5DragonMidpoint xy_to_n(): "$x, $y" | ||
| 166 | |||
| 167 | $x = round_nearest($x); | ||
| 168 | $y = round_nearest($y); | ||
| 169 | |||
| 170 | foreach my $overflow (2*$x + 2*$y, 2*$x - 2*$y) { | ||
| 171 | if (is_infinite($overflow)) { return $overflow; } | ||
| 172 | } | ||
| 173 | my $zero = ($x * 0 * $y); # inherit bignum 0 | ||
| 174 | my @ndigits; # low to high; | ||
| 175 | |||
| 176 | for (;;) { | ||
| 177 | last if ($x <= 0 && $x >= -1 && $y <= 1 && $y >= 0); | ||
| 178 | |||
| 179 | my $k = 3*(10*($y%10) + ($x%10)); | ||
| 180 | |||
| 181 | ### at: "x=$x,y=$y (k=$k) ndigits=".join(',',@ndigits)." digit=$yx_to_digdxdy[$k] offset=$yx_to_digdxdy[$k+1],$yx_to_digdxdy[$k+2] to ".($x+$yx_to_digdxdy[$k+1]).",".($y+$yx_to_digdxdy[$k+2]) | ||
| 182 | |||
| 183 | push @ndigits, $yx_to_digdxdy[$k++]; # ndigit | ||
| 184 | $x += $yx_to_digdxdy[$k++]; # dx | ||
| 185 | $y += $yx_to_digdxdy[$k]; # dy | ||
| 186 | |||
| 187 | # (x+iy)/(1+2i) | ||
| 188 | # = (x+iy)*(1-2i) / (1+4) | ||
| 189 | # = (x+iy)*(1-2i) / 5 | ||
| 190 | # = (x+2y +i(y-2x)) / 5 | ||
| 191 | # | ||
| 192 | ### assert: abs($x + 2 * $y) % 5 == 0 | ||
| 193 | ### assert: abs($y - 2 * $x) % 5 == 0 | ||
| 194 | |||
| 195 | ($x,$y) = (($x+2*$y) / 5, # divide 1+2i | ||
| 196 | ($y-2*$x) / 5); | ||
| 197 | ### divide down to: "$x,$y" | ||
| 198 | } | ||
| 199 | |||
| 200 | ### final: "xy=$x,$y" | ||
| 201 | my $arm = $xy_to_arm[$x]->[$y]; | ||
| 202 | ### $arm | ||
| 203 | |||
| 204 | my $arms_count = $self->arms_count; | ||
| 205 | if ($arm >= $arms_count) { | ||
| 206 | return undef; | ||
| 207 | } | ||
| 208 | if ($arm & 1) { | ||
| 209 | ### flip ... | ||
| 210 | @ndigits = map {4-$_} @ndigits; | ||
| 211 | } | ||
| 212 | |||
| 213 | return digit_join_lowtohigh(\@ndigits, 5, $zero) * $arms_count + $arm; | ||
| 214 | } | ||
| 215 | |||
| 216 | #------------------------------------------------------------------------------ | ||
| 217 | |||
| 218 | # whole plane covered when arms==4 | ||
| 219 | sub xy_is_visited { | ||
| 220 | my ($self, $x, $y) = @_; | ||
| 221 | return ($self->{'arms'} == 4 | ||
| 222 | || defined($self->xy_to_n($x,$y))); | ||
| 223 | } | ||
| 224 | |||
| 225 | #------------------------------------------------------------------------------ | ||
| 226 | |||
| 227 | # FIXME: half size of R5DragonCurve ? | ||
| 228 | # | ||
| 229 | # not exact | ||
| 230 | sub rect_to_n_range { | ||
| 231 | my ($self, $x1,$y1, $x2,$y2) = @_; | ||
| 232 | ### R5DragonCurve rect_to_n_range(): "$x1,$y1 $x2,$y2" | ||
| 233 | |||
| 234 | my $xmax = int(max(abs($x1),abs($x2))); | ||
| 235 | my $ymax = int(max(abs($y1),abs($y2))); | ||
| 236 | return (0, | ||
| 237 | ($xmax*$xmax + $ymax*$ymax) | ||
| 238 | * 6 | ||
| 239 | * $self->{'arms'}); | ||
| 240 | } | ||
| 241 | |||
| 242 | #----------------------------------------------------------------------------- | ||
| 243 | # level_to_n_range() | ||
| 244 | |||
| 245 | # arms=1 arms=2 | ||
| 246 | # level 0 0..0 = 1 0..1 = 2 | ||
| 247 | # level 1 0..4 = 5 0..9 = 10 | ||
| 248 | # level 2 0..24 = 25 0..49 = 50 | ||
| 249 | |||
| 250 | sub level_to_n_range { | ||
| 251 | my ($self, $level) = @_; | ||
| 252 | return (0, 5**$level * $self->{'arms'} - 1); | ||
| 253 | } | ||
| 254 | sub n_to_level { | ||
| 255 | my ($self, $n) = @_; | ||
| 256 | if ($n < 0) { return undef; } | ||
| 257 | if (is_infinite($n)) { return $n; } | ||
| 258 | $n = round_nearest($n); | ||
| 259 | _divrem_mutate ($n, $self->{'arms'}); | ||
| 260 | my ($pow, $exp) = round_up_pow ($n+1, 5); | ||
| 261 | return $exp; | ||
| 262 | } | ||
| 263 | |||
| 264 | #----------------------------------------------------------------------------- | ||
| 265 | 1; | ||
| 266 | __END__ |