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
|
# Copyright 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/>.
# much overlap
package Math::PlanePath::Z2DragonCurve;
use 5.004;
use strict;
use List::Util 'min'; # 'max'
*max = \&Math::PlanePath::_max;
*_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
use Math::PlanePath;
*_divrem_mutate = \&Math::PlanePath::_divrem_mutate;
use Math::PlanePath::Base::Generic
'is_infinite',
'round_nearest',
'xy_is_even';
use Math::PlanePath::Base::Digits
'digit_split_lowtohigh';
use vars '$VERSION', '@ISA';
$VERSION = 117;
@ISA = ('Math::PlanePath');
# uncomment this to run the ### lines
# use Smart::Comments;
use constant n_start => 0;
#------------------------------------------------------------------------------
#
# .
# h
# .
# .........
# .
# ....g...
# . .
# . . .
# . .
# .. f..10---d--11
# . |
# 7...|....
# | | .
# 8---c---9 e
# | .
# 6-------5 3
# |
# 2---b---3 2
# | |
# | 4 1
# |
# 0---a---1 0
#
# 0 1 2 3 4
# 10---*--11
# |
# 7 |
# | |
# 8---*---9
# |
# 6-------5
# \ / | \
# 2--/*---3
# /|\ |/ \
# | 4
# \ / \|/ /
# 0---*---1
# \ / / \
sub n_to_xy {
my ($self, $n) = @_;
### Z2DragonCurve n_to_xy(): $n
if ($n < 0) { return; }
if (is_infinite($n)) { return ($n, $n); }
my $zero = ($n * 0); # inherit bignum 0
{
# high to low
my $x = 0;
my $y = 0;
my $dx = 1;
my $dy = 0;
# return if $n >=9;
my $lowdigit = _divrem_mutate($n, 4);
my @digits = digit_split_lowtohigh($n,3);
foreach my $digit (reverse(@digits), $lowdigit) {
### at: "$x,$y digit=$digit"
($x,$y) = ($x-$y,$x+$y); # rotate +45
$x += 1;
### rotate to: "$x,$y"
if ($digit == 0) {
$x -= $dx;
$y -= $dy;
} elsif ($digit == 1) {
$x += $dx;
$y += $dy;
($dx,$dy) = (-$dy,$dx); # rotate +90
} elsif ($digit == 2) {
$x += $dx - 2*$dy; # across then at +90
$y += $dy + 2*$dx;
} elsif ($digit == 3) {
$x += 3*$dx - 2*$dy; # across then at +90, for $lowdigit
$y += 3*$dy + 2*$dx;
}
}
### return: "$x,$y"
return ($x,$y);
}
{
# low to high
my $x = 0;
my $y = 0;
my $dx = 1 + $zero;
my $dy = $zero;
return if $n >=16;
my $lowdigit = _divrem_mutate($n, 3);
if ($lowdigit == 0) {
} elsif ($lowdigit == 1) {
$x = 2;
} elsif ($lowdigit == 2) {
$x = 2;
$y = 2;
} elsif ($lowdigit == 3) {
$x = 4;
$y = 2;
}
foreach my $digit (digit_split_lowtohigh($n,3)) {
# $dx *= 2;
# $dy *= 2;
($dx,$dy) = ($dx+$dy,$dy-$dx); # rotate 45
# ($dx,$dy) = (-$dy,$dx); # rotate +90
if ($digit == 0) {
} elsif ($digit == 1) {
($x,$y) = (-$y,$x); # rotate +90
$x += 3/2*$dx;
$y += 3/2*$dy;
($dx,$dy) = (-$dy,$dx); # rotate +90
$x += 1/2*$dx;
$y += 1/2*$dy;
} elsif ($digit == 2) {
$x -= 4/2*$dy;
$y += 4/2*$dx;
}
}
return ($x,$y);
}
}
sub xy_to_n {
my ($self, $x, $y) = @_;
return undef;
}
# minimum -- no, not quite right
#
# *----------*
# \
# \ *
# * \
# \
# *----------*
#
# width = side/2
# minimum = side*sqrt(3)/2 - width
# = side*(sqrt(3)/2 - 1)
#
# minimum 4/9 * 2.9^level roughly
# h = 4/9 * 2.9^level
# 2.9^level = h*9/4
# level = log(h*9/4)/log(2.9)
# 3^level = 3^(log(h*9/4)/log(2.9))
# = h*9/4, but big bigger for log
#
# not exact
sub rect_to_n_range {
my ($self, $x1,$y1, $x2,$y2) = @_;
### Z2DragonCurve rect_to_n_range(): "$x1,$y1 $x2,$y2"
my $xmax = int(max(abs($x1),abs($x2)));
my $ymax = int(max(abs($y1),abs($y2)));
return (0,
($xmax*$xmax + $ymax*$ymax + 1));
}
1;
__END__
|