| 1 | # Copyright 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 it | ||||||||||||||
| 6 | # under the terms of the GNU General Public License as published by the Free | ||||||||||||||
| 7 | # 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 | # arms begin at 0,0 or at 1 in ? | ||||||||||||||
| 20 | |||||||||||||||
| 21 | |||||||||||||||
| 22 | # math-image --path=GosperSide --lines --scale=10 | ||||||||||||||
| 23 | # math-image --path=GosperSide --output=numbers | ||||||||||||||
| 24 | |||||||||||||||
| 25 | |||||||||||||||
| 26 | package Math::PlanePath::GosperSide; | ||||||||||||||
| 27 | 1 | ▶ | use 5.004; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 1 | ▶ | use List::Util 'min','max'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | 1 | ▶ | use POSIX 'ceil'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 31 | 1 | ▶ | use Math::PlanePath::GosperIslands; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | 1 | ▶ | use Math::PlanePath::SacksSpiral; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 1 | ▶ | use vars '$VERSION', '@ISA', '@_xend','@_yend'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | 1 | $VERSION = 128; | |||||||||||||
| 36 | 1 | ▶ | use Math::PlanePath; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | 1 | @ISA = ('Math::PlanePath'); | |||||||||||||
| 38 | 1 | *_divrem_mutate = \&Math::PlanePath::_divrem_mutate; | |||||||||||||
| 39 | |||||||||||||||
| 40 | use Math::PlanePath::Base::Generic | ||||||||||||||
| 41 | 1 | 'is_infinite', | |||||||||||||
| 42 | 1 | ▶ | 'round_nearest'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | use Math::PlanePath::Base::Digits | ||||||||||||||
| 44 | 1 | ▶ | 'digit_split_lowtohigh'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 45 | |||||||||||||||
| 46 | # uncomment this to run the ### lines | ||||||||||||||
| 47 | #use Devel::Comments; | ||||||||||||||
| 48 | |||||||||||||||
| 49 | 1 | ▶ | use constant n_start => 0; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 50 | |||||||||||||||
| 51 | # secret experimental as yet ... | ||||||||||||||
| 52 | # | ||||||||||||||
| 53 | # use constant parameter_info_array => [ { name => 'arms', | ||||||||||||||
| 54 | # share_key => 'arms_6', | ||||||||||||||
| 55 | # type => 'integer', | ||||||||||||||
| 56 | # minimum => 1, | ||||||||||||||
| 57 | # maximum => 6, | ||||||||||||||
| 58 | # default => 1, | ||||||||||||||
| 59 | # width => 1, | ||||||||||||||
| 60 | # description => 'Arms', | ||||||||||||||
| 61 | # } ]; | ||||||||||||||
| 62 | |||||||||||||||
| 63 | 1 | ▶ | use constant x_negative_at_n => 113; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 64 | 1 | ▶ | use constant y_negative_at_n => 11357; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 65 | |||||||||||||||
| 66 | 1 | ▶ | use constant dx_minimum => -2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 67 | 1 | ▶ | use constant dx_maximum => 2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 68 | 1 | ▶ | use constant dy_minimum => -1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 69 | 1 | ▶ | use constant dy_maximum => 1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 70 | |||||||||||||||
| 71 | 1 | *_UNDOCUMENTED__dxdy_list = \&Math::PlanePath::_UNDOCUMENTED__dxdy_list_six; | |||||||||||||
| 72 | # 2,0, # E N=0 | ||||||||||||||
| 73 | # 1,1, # NE N=1 | ||||||||||||||
| 74 | # -1,1, # NW N=4 | ||||||||||||||
| 75 | # -2,0, # W N=13 | ||||||||||||||
| 76 | # -1,-1, # SW N=40 | ||||||||||||||
| 77 | # 1,-1, # SE N=121 | ||||||||||||||
| 78 | 1 | ▶ | use constant 1.02 _UNDOCUMENTED__dxdy_list_at_n => 121; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 79 | |||||||||||||||
| 80 | 1 | ▶ | use constant absdx_minimum => 1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 81 | 1 | ▶ | use constant dsumxy_minimum => -2; # diagonals | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 82 | 1 | ▶ | use constant dsumxy_maximum => 2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 83 | 1 | ▶ | use constant ddiffxy_minimum => -2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 84 | 1 | ▶ | use constant ddiffxy_maximum => 2; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 85 | 1 | ▶ | use constant dir_maximum_dxdy => (1,-1); # South-East | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 86 | 1 | ▶ | use constant turn_any_straight => 0; # never straight | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 87 | |||||||||||||||
| 88 | |||||||||||||||
| 89 | #------------------------------------------------------------------------------ | ||||||||||||||
| 90 | |||||||||||||||
| 91 | sub new { | ||||||||||||||
| 92 | 7 | ▶ | my $self = shift->SUPER::new(@_); | ||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||
| 93 | 7 | ▶ | $self->{'arms'} = max(1, min(6, $self->{'arms'} || 1)); | ||||||||||||
|
Condition50%1/2
$self->{'arms'} || 1
MC/DC0%0/1
$self->{'arms'} || 1
$self->{'arms'}
Truth table50%1/2
A
A= $self->{'arms'}
| |||||||||||||||
| 94 | 7 | return $self; | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | sub n_to_xy { | ||||||||||||||
| 98 | 2514 | ▶ | my ($self, $n) = @_; | ||||||||||||
|
Subroutine
n_to_xy: called CC=10 SCAR=29.5
Pod
documented
| |||||||||||||||
| 99 | ### GosperSide n_to_xy(): $n | ||||||||||||||
| 100 | 2514 | ▶ | if ($n < 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | 0 | return; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | 2514 | ▶ | if (is_infinite($n)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 104 | 0 | return ($n,$n); | |||||||||||||
| 105 | } | ||||||||||||||
| 106 | |||||||||||||||
| 107 | 2514 | my $x; | |||||||||||||
| 108 | 2514 | my $y = my $yend = ($n * 0); # inherit bignum 0 | |||||||||||||
| 109 | 2514 | my $xend = $y + 2; # inherit bignum 2 | |||||||||||||
| 110 | { | ||||||||||||||
| 111 | 2514 | my $int = int($n); | |||||||||||||
| 112 | 2514 | $x = 2 * ($n - $int); | |||||||||||||
| 113 | 2514 | $n = $int; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | |||||||||||||||
| 117 | 2514 | ▶ | if ((my $arms = $self->{'arms'}) > 1) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | 0 | my $rot = _divrem_mutate ($n, $arms); | |||||||||||||
| 119 | 0 | ▶ | if ($rot >= 3) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | 0 | $rot -= 3; | |||||||||||||
| 121 | 0 | $x = -$x; # rotate 180, knowing y=0,yend=0 | |||||||||||||
| 122 | 0 | $xend = -2; | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | 0 | ▶ | if ($rot == 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 125 | 0 | $x = $y = $x/2; # rotate +60, knowing y=0,yend=0 | |||||||||||||
| 126 | 0 | $xend = $yend = $xend/2; | |||||||||||||
| 127 | } elsif ($rot == 2) { | ||||||||||||||
| 128 | 0 | $y = $x/2; # rotate +120, knowing y=0,yend=0 | |||||||||||||
| 129 | 0 | $x = -$y; | |||||||||||||
| 130 | 0 | $yend = $xend/2; | |||||||||||||
| 131 | 0 | $xend = -$yend; | |||||||||||||
| 132 | } | ||||||||||||||
| 133 | } | ||||||||||||||
| 134 | |||||||||||||||
| 135 | 2514 | foreach my $digit (digit_split_lowtohigh($n,3)) { | |||||||||||||
| 136 | 15568 | my $xend_offset = 3*($xend-$yend)/2; # end and end +60 | |||||||||||||
| 137 | 15568 | my $yend_offset = ($xend+3*$yend)/2; | |||||||||||||
| 138 | |||||||||||||||
| 139 | ### at: "$x,$y" | ||||||||||||||
| 140 | ### $digit | ||||||||||||||
| 141 | ### $xend | ||||||||||||||
| 142 | ### $yend | ||||||||||||||
| 143 | ### $xend_offset | ||||||||||||||
| 144 | ### $yend_offset | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 15568 | ▶ | if ($digit == 1) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 147 | 5776 | ($x,$y) = (($x-3*$y)/2 + $xend, # rotate +60 | |||||||||||||
| 148 | ($x+$y)/2 + $yend); | ||||||||||||||
| 149 | } elsif ($digit == 2) { | ||||||||||||||
| 150 | 4950 | $x += $xend_offset; # offset and offset +60 | |||||||||||||
| 151 | 4950 | $y += $yend_offset; | |||||||||||||
| 152 | } | ||||||||||||||
| 153 | 15568 | $xend += $xend_offset; # offset and offset +60 | |||||||||||||
| 154 | 15568 | $yend += $yend_offset; | |||||||||||||
| 155 | } | ||||||||||||||
| 156 | |||||||||||||||
| 157 | ### final: "$x,$y" | ||||||||||||||
| 158 | 2514 | return ($x, $y); | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | |||||||||||||||
| 161 | # level = (log(hypot) + log(2*.99)) * 1/log(sqrt(7)) | ||||||||||||||
| 162 | # = (log(hypot^2)/2 + log(2*.99)) * 1/log(sqrt(7)) | ||||||||||||||
| 163 | # = (log(hypot^2) + 2*log(2*.99)) * 1/(2*log(sqrt(7))) | ||||||||||||||
| 164 | # | ||||||||||||||
| 165 | sub xy_to_n { | ||||||||||||||
| 166 | 510 | ▶ | my ($self, $x, $y) = @_; | ||||||||||||
|
Subroutine
xy_to_n: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||
| 167 | 510 | $x = round_nearest ($x); | |||||||||||||
| 168 | 510 | $y = round_nearest ($y); | |||||||||||||
| 169 | ### GosperSide xy_to_n(): "$x, $y" | ||||||||||||||
| 170 | |||||||||||||||
| 171 | 510 | ▶ | if (($x ^ $y) & 1) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 172 | 0 | return undef; | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | 510 | my $h2 = $x*$x + $y*$y*3 + 1; | |||||||||||||
| 176 | 510 | my $level = max (0, | |||||||||||||
| 177 | ceil ((log($h2) + 2*log(2*.99)) * (1/2*log(sqrt(7))))); | ||||||||||||||
| 178 | 510 | ▶ | if (is_infinite($level)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 179 | 0 | return $level; | |||||||||||||
| 180 | } | ||||||||||||||
| 181 | 510 | return Math::PlanePath::GosperIslands::_xy_to_n_in_level($x,$y,$level); | |||||||||||||
| 182 | } | ||||||||||||||
| 183 | |||||||||||||||
| 184 | |||||||||||||||
| 185 | # Points beyond N=3^level only go a small distance back before that N | ||||||||||||||
| 186 | # hypotenuse. | ||||||||||||||
| 187 | # hypot = .99 * 2 * sqrt(7)^level | ||||||||||||||
| 188 | # sqrt(7)^level = hypot / (2*.99) | ||||||||||||||
| 189 | # sqrt(7)^level = hypot / (2*.99) | ||||||||||||||
| 190 | # level = log(hypot / (2*.99)) / log(sqrt(7)) | ||||||||||||||
| 191 | # = (log(hypot) + log(2*.99)) * 1/log(sqrt(7)) | ||||||||||||||
| 192 | # | ||||||||||||||
| 193 | # not exact | ||||||||||||||
| 194 | sub rect_to_n_range { | ||||||||||||||
| 195 | 514 | ▶ | my ($self, $x1,$y1, $x2,$y2) = @_; | ||||||||||||
|
Subroutine
rect_to_n_range: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 196 | 514 | $y1 *= sqrt(3); | |||||||||||||
| 197 | 514 | $y2 *= sqrt(3); | |||||||||||||
| 198 | 514 | my ($r_lo, $r_hi) = Math::PlanePath::SacksSpiral::_rect_to_radius_range | |||||||||||||
| 199 | ($x1,$y1, $x2,$y2); | ||||||||||||||
| 200 | 514 | my $level = max (0, | |||||||||||||
| 201 | ceil ((log($r_hi+.1) + log(2*.99)) * (1/log(sqrt(7))))); | ||||||||||||||
| 202 | return (0, | ||||||||||||||
| 203 | 514 | $self->{'arms'} * 3 ** $level - 1); | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | |||||||||||||||
| 206 | #------------------------------------------------------------------------------ | ||||||||||||||
| 207 | # levels | ||||||||||||||
| 208 | |||||||||||||||
| 209 | 1 | ▶ | use Math::PlanePath::SierpinskiArrowhead; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 210 | 1 | *level_to_n_range = \&Math::PlanePath::SierpinskiArrowhead::level_to_n_range; | |||||||||||||
| 211 | 1 | *n_to_level = \&Math::PlanePath::SierpinskiArrowhead::n_to_level; | |||||||||||||
| 212 | |||||||||||||||
| 213 | #------------------------------------------------------------------------------ | ||||||||||||||
| 214 | 1 | 1; | |||||||||||||
| 215 | __END__ | ||||||||||||||