| 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 | package Math::PlanePath::ComplexRevolving; | ||
| 21 | use 5.004; | ||
| 22 | use strict; | ||
| 23 | #use List::Util 'max'; | ||
| 24 | *max = \&Math::PlanePath::_max; | ||
| 25 | |||
| 26 | use vars '$VERSION', '@ISA'; | ||
| 27 | $VERSION = 128; | ||
| 28 | use Math::PlanePath; | ||
| 29 | @ISA = ('Math::PlanePath'); | ||
| 30 | |||
| 31 | use Math::PlanePath::Base::Generic | ||
| 32 | 'is_infinite', | ||
| 33 | 'round_nearest'; | ||
| 34 | use Math::PlanePath::Base::Digits | ||
| 35 | 'round_up_pow', | ||
| 36 | 'bit_split_lowtohigh', | ||
| 37 | 'digit_join_lowtohigh'; | ||
| 38 | |||
| 39 | # uncomment this to run the ### lines | ||
| 40 | #use Smart::Comments; | ||
| 41 | |||
| 42 | |||
| 43 | use constant n_start => 0; | ||
| 44 | use constant xy_is_visited => 1; | ||
| 45 | use constant x_negative_at_n => 5; | ||
| 46 | use constant y_negative_at_n => 7; | ||
| 47 | # use constant dir_maximum_dxdy => (0,0); # supremum, almost full way | ||
| 48 | use constant turn_any_straight => 0; # never straight | ||
| 49 | |||
| 50 | |||
| 51 | #------------------------------------------------------------------------------ | ||
| 52 | # b=i+1 | ||
| 53 | # X+iY = b^e0 + i*b^e1 + ... + i^t * b^et | ||
| 54 | # | ||
| 55 | sub n_to_xy { | ||
| 56 | my ($self, $n) = @_; | ||
| 57 | ### ComplexRevolving n_to_xy(): $n | ||
| 58 | |||
| 59 | if ($n < 0) { return; } | ||
| 60 | if (is_infinite($n)) { return ($n,$n); } | ||
| 61 | |||
| 62 | { | ||
| 63 | my $int = int($n); | ||
| 64 | ### $int | ||
| 65 | ### $n | ||
| 66 | if ($n != $int) { | ||
| 67 | my ($x1,$y1) = $self->n_to_xy($int); | ||
| 68 | my ($x2,$y2) = $self->n_to_xy($int+1); | ||
| 69 | my $frac = $n - $int; # inherit possible BigFloat | ||
| 70 | my $dx = $x2-$x1; | ||
| 71 | my $dy = $y2-$y1; | ||
| 72 | return ($frac*$dx + $x1, $frac*$dy + $y1); | ||
| 73 | } | ||
| 74 | $n = $int; # BigFloat int() gives BigInt, use that | ||
| 75 | } | ||
| 76 | |||
| 77 | my $x = my $y = ($n * 0); # inherit bignum 0 | ||
| 78 | |||
| 79 | if (my @digits = bit_split_lowtohigh($n)) { | ||
| 80 | my $bx = $x + 1; # inherit bignum 1 | ||
| 81 | my $by = $x; # 0 | ||
| 82 | for (;;) { | ||
| 83 | if (shift @digits) { # low to high | ||
| 84 | $x += $bx; | ||
| 85 | $y += $by; | ||
| 86 | ($bx,$by) = (-$by,$bx); # (bx+by*i)*i = bx*i - by, is rotate +90 | ||
| 87 | } | ||
| 88 | @digits || last; | ||
| 89 | |||
| 90 | # (bx+by*i) * (i+1) | ||
| 91 | # = bx*i+bx + -by + by*i | ||
| 92 | # = (bx-by) + i*(bx+by) | ||
| 93 | ($bx,$by) = ($bx - $by, | ||
| 94 | $bx + $by); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | ### final: "$x,$y" | ||
| 99 | return ($x,$y); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub xy_to_n { | ||
| 103 | my ($self, $x, $y) = @_; | ||
| 104 | ### ComplexRevolving xy_to_n(): "$x, $y" | ||
| 105 | |||
| 106 | $x = round_nearest ($x); | ||
| 107 | $y = round_nearest ($y); | ||
| 108 | |||
| 109 | foreach my $overflow ($x+$y, $x-$y) { | ||
| 110 | if (is_infinite($overflow)) { return $overflow; } | ||
| 111 | } | ||
| 112 | |||
| 113 | my $zero = $x * 0 * $y; # inherit bignum 0 | ||
| 114 | |||
| 115 | my @n; | ||
| 116 | while ($x || $y) { | ||
| 117 | ### at: "$x,$y power=$power n=$n" | ||
| 118 | |||
| 119 | # (a+bi)*(i+1) = (a-b)+(a+b)i | ||
| 120 | # | ||
| 121 | if (($x % 2) == ($y % 2)) { # x+y even | ||
| 122 | push @n, 0; | ||
| 123 | } else { | ||
| 124 | ### not multiple of 1+i, take e0=0 for b^e0=1 | ||
| 125 | # [(x+iy)-1]/i | ||
| 126 | # = [(x-1)+yi]/i | ||
| 127 | # = y + (x-1)/i | ||
| 128 | # = y + (1-x)*i # rotate -90 | ||
| 129 | |||
| 130 | push @n, 1; | ||
| 131 | ($x,$y) = ($y, 1-$x); | ||
| 132 | |||
| 133 | ### sub and div to: "$x,$y" | ||
| 134 | } | ||
| 135 | |||
| 136 | # divide i+1 = mul (i-1)/(i^2 - 1^2) | ||
| 137 | # = mul (i-1)/-2 | ||
| 138 | # is (i*y + x) * (i-1)/-2 | ||
| 139 | # x = (-x - y)/-2 = (x + y)/2 | ||
| 140 | # y = (-y + x)/-2 = (y - x)/2 | ||
| 141 | # | ||
| 142 | ### assert: (($x+$y)%2)==0 | ||
| 143 | ($x,$y) = (($x+$y)/2, ($y-$x)/2); | ||
| 144 | } | ||
| 145 | |||
| 146 | return digit_join_lowtohigh(\@n,2,$zero); | ||
| 147 | } | ||
| 148 | |||
| 149 | # not exact | ||
| 150 | sub rect_to_n_range { | ||
| 151 | my ($self, $x1,$y1, $x2,$y2) = @_; | ||
| 152 | ### ComplexRevolving rect_to_n_range(): "$x1,$y1 $x2,$y2" | ||
| 153 | |||
| 154 | my $xm = max(abs($x1),abs($x2)); | ||
| 155 | my $ym = max(abs($y1),abs($y2)); | ||
| 156 | |||
| 157 | return (0, int (32*($xm*$xm + $ym*$ym))); | ||
| 158 | } | ||
| 159 | |||
| 160 | #------------------------------------------------------------------------------ | ||
| 161 | # levels | ||
| 162 | |||
| 163 | sub level_to_n_range { | ||
| 164 | my ($self, $level) = @_; | ||
| 165 | return (0, 2**$level - 1); | ||
| 166 | } | ||
| 167 | sub n_to_level { | ||
| 168 | my ($self, $n) = @_; | ||
| 169 | if ($n < 0) { return undef; } | ||
| 170 | if (is_infinite($n)) { return $n; } | ||
| 171 | $n = round_nearest($n); | ||
| 172 | my ($pow, $exp) = round_up_pow ($n+1, 2); | ||
| 173 | return $exp; | ||
| 174 | } | ||
| 175 | |||
| 176 | #------------------------------------------------------------------------------ | ||
| 177 | 1; | ||
| 178 | __END__ |