| 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 | package Math::PlanePath::HexSpiralSkewed; | ||||||||||||
| 20 | 2 | ▶ | use 5.004; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | #use List::Util 'max'; | ||||||||||||
| 23 | 2 | *max = \&Math::PlanePath::_max; | |||||||||||
| 24 | |||||||||||||
| 25 | 2 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 2 | $VERSION = 128; | |||||||||||
| 27 | 2 | ▶ | use Math::PlanePath; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 28 | 2 | *_sqrtint = \&Math::PlanePath::_sqrtint; | |||||||||||
| 29 | 2 | @ISA = ('Math::PlanePath'); | |||||||||||
| 30 | |||||||||||||
| 31 | 2 | ▶ | use Math::PlanePath::HexSpiral; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | use Math::PlanePath::Base::Generic | ||||||||||||
| 33 | 2 | ▶ | 'round_nearest'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 34 | |||||||||||||
| 35 | # uncomment this to run the ### lines | ||||||||||||
| 36 | #use Devel::Comments; | ||||||||||||
| 37 | |||||||||||||
| 38 | |||||||||||||
| 39 | 2 | ▶ | use Math::PlanePath::SquareSpiral; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 40 | 2 | *parameter_info_array = \&Math::PlanePath::SquareSpiral::parameter_info_array; | |||||||||||
| 41 | 2 | ▶ | use constant xy_is_visited => 1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 42 | |||||||||||||
| 43 | 2 | ▶ | use constant dx_minimum => -1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 44 | 2 | ▶ | use constant dx_maximum => 1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | 2 | ▶ | use constant dy_minimum => -1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 46 | 2 | ▶ | use constant dy_maximum => 1; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 47 | |||||||||||||
| 48 | 2 | use constant 1.02 _UNDOCUMENTED__dxdy_list => (1,0, # E four plus | |||||||||||
| 49 | 0,1, # N NW and SE | ||||||||||||
| 50 | -1,1, # NW | ||||||||||||
| 51 | -1,0, # W | ||||||||||||
| 52 | 0,-1, # S | ||||||||||||
| 53 | 1,-1, # SE | ||||||||||||
| 54 | 2 | ▶ | ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 55 | 2 | *x_negative_at_n = \&Math::PlanePath::HexSpiral::x_negative_at_n; | |||||||||||
| 56 | 2 | *y_negative_at_n | |||||||||||
| 57 | = \&Math::PlanePath::HexSpiral::y_negative_at_n; | ||||||||||||
| 58 | 2 | *_UNDOCUMENTED__dxdy_list_at_n | |||||||||||
| 59 | = \&Math::PlanePath::HexSpiral::_UNDOCUMENTED__dxdy_list_at_n; | ||||||||||||
| 60 | |||||||||||||
| 61 | 2 | ▶ | use constant dsumxy_minimum => -1; # W,S straight | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 62 | 2 | ▶ | use constant dsumxy_maximum => 1; # N,E straight | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 63 | 2 | ▶ | use constant ddiffxy_minimum => -2; # NW diagonal | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 64 | 2 | ▶ | use constant ddiffxy_maximum => 2; # SE diagonal | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 65 | 2 | ▶ | use constant dir_maximum_dxdy => (1,-1); # South-East | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 66 | |||||||||||||
| 67 | 2 | ▶ | use constant turn_any_right => 0; # only left or straight | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 68 | sub _UNDOCUMENTED__turn_any_left_at_n { | ||||||||||||
| 69 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
_UNDOCUMENTED__turn_any_left_at_n: not called CC=1 SCAR=6.9
| |||||||||||||
| 70 | 0 | return $self->n_start + $self->{'wider'} + 1; | |||||||||||
| 71 | } | ||||||||||||
| 72 | |||||||||||||
| 73 | |||||||||||||
| 74 | #------------------------------------------------------------------------------ | ||||||||||||
| 75 | |||||||||||||
| 76 | sub new { | ||||||||||||
| 77 | 5 | ▶ | my $self = shift->SUPER::new (@_); | ||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 78 | |||||||||||||
| 79 | # parameters | ||||||||||||
| 80 | 5 | ▶ | $self->{'wider'} ||= 0; # default | ||||||||||
|
Condition50%1/2
$self->{"wider"} ||= 0
MC/DC0%0/1
$self->{"wider"} ||= 0
$self->{"wider"}
Truth table50%1/2
A
A= $self->{"wider"}
| |||||||||||||
| 81 | 5 | ▶ | if (! defined $self->{'n_start'}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 3 | $self->{'n_start'} = $self->default_n_start; | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 5 | return $self; | |||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | # Same as HexSpiral, but diagonal down and to the left is the downwards | ||||||||||||
| 89 | # vertical at x=-$w_left. | ||||||||||||
| 90 | |||||||||||||
| 91 | sub n_to_xy { | ||||||||||||
| 92 | 46 | ▶ | my ($self, $n) = @_; | ||||||||||
|
Subroutine
n_to_xy: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 93 | ### HexSpiralSkewed n_to_xy(): $n | ||||||||||||
| 94 | |||||||||||||
| 95 | 46 | $n = $n - $self->{'n_start'}; # N=0 basis | |||||||||||
| 96 | 46 | ▶ | if ($n < 0) { return; } | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | |||||||||||||
| 98 | 46 | my $w = $self->{'wider'}; | |||||||||||
| 99 | 46 | my $w_right = int($w/2); | |||||||||||
| 100 | 46 | my $w_left = $w - $w_right; | |||||||||||
| 101 | #### $w | ||||||||||||
| 102 | #### $w_left | ||||||||||||
| 103 | #### $w_right | ||||||||||||
| 104 | |||||||||||||
| 105 | 46 | my $d = int((_sqrtint(3*$n + ($w+2)*$w + 1) - 1 - $w) / 3); | |||||||||||
| 106 | #### d frac: (_sqrtint(3*$n + ($w+2)*$w + 1) - 1 - $w) / 3 | ||||||||||||
| 107 | #### $d | ||||||||||||
| 108 | 46 | $n -= (3*$d + 2 + 2*$w)*$d + 1; | |||||||||||
| 109 | #### remainder: $n | ||||||||||||
| 110 | |||||||||||||
| 111 | 46 | $n += 1; # N=1 basis | |||||||||||
| 112 | |||||||||||||
| 113 | 46 | ▶ | if ($n <= $d+1+$w) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | #### bottom horizontal | ||||||||||||
| 115 | 22 | return ($n - $w_left, | |||||||||||
| 116 | -$d); | ||||||||||||
| 117 | } | ||||||||||||
| 118 | 24 | $n -= $d+1+$w; | |||||||||||
| 119 | 24 | ▶ | if ($n <= $d) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | #### right lower vertical, being 1 shorter: $n | ||||||||||||
| 121 | 4 | return ($d + 1 + $w_right, | |||||||||||
| 122 | $n - $d); | ||||||||||||
| 123 | } | ||||||||||||
| 124 | 20 | $n -= $d; | |||||||||||
| 125 | 20 | ▶ | if ($n <= $d+1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 126 | #### right upper diagonal: $n | ||||||||||||
| 127 | 6 | return (-$n + $d + 1 + $w_right, | |||||||||||
| 128 | $n); | ||||||||||||
| 129 | } | ||||||||||||
| 130 | 14 | $d = $d + 1; # no warnings if $d==infinity | |||||||||||
| 131 | 14 | $n -= $d; | |||||||||||
| 132 | 14 | ▶ | if ($n <= $d+$w) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 133 | #### top horizontal | ||||||||||||
| 134 | 6 | return (-$n + $w_right, | |||||||||||
| 135 | $d); | ||||||||||||
| 136 | } | ||||||||||||
| 137 | 8 | $n -= $d+$w; | |||||||||||
| 138 | 8 | ▶ | if ($n <= $d) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | #### left upper vertical | ||||||||||||
| 140 | 6 | return (-$d - $w_left, | |||||||||||
| 141 | -$n + $d); | ||||||||||||
| 142 | } | ||||||||||||
| 143 | #### left lower diagonal | ||||||||||||
| 144 | 2 | $n -= $d; | |||||||||||
| 145 | 2 | return ($n - $d - $w_left, | |||||||||||
| 146 | -$n); | ||||||||||||
| 147 | } | ||||||||||||
| 148 | |||||||||||||
| 149 | sub xy_to_n { | ||||||||||||
| 150 | 4 | ▶ | my ($self, $x, $y) = @_; | ||||||||||
|
Subroutine
xy_to_n: called CC=4 SCAR=15.6
Pod
documented
| |||||||||||||
| 151 | ### xy_to_n(): "$x, $y" | ||||||||||||
| 152 | |||||||||||||
| 153 | 4 | $x = round_nearest ($x); | |||||||||||
| 154 | 4 | $y = round_nearest ($y); | |||||||||||
| 155 | |||||||||||||
| 156 | 4 | my $w = $self->{'wider'}; | |||||||||||
| 157 | 4 | my $w_right = int($w/2); | |||||||||||
| 158 | 4 | my $w_left = $w - $w_right; | |||||||||||
| 159 | |||||||||||||
| 160 | 4 | ▶ | if ($y > 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 161 | 0 | $x -= $w_right; | |||||||||||
| 162 | 0 | ▶ | if ($x < -$y-$w) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | ### left upper vertical | ||||||||||||
| 164 | 0 | my $d = -$x - $w; | |||||||||||
| 165 | ### $d | ||||||||||||
| 166 | ### base: (3*$d + 1 + 2*$w)*$d | ||||||||||||
| 167 | return ((3*$d + 1 + 2*$w)*$d | ||||||||||||
| 168 | - $y | ||||||||||||
| 169 | 0 | + $self->{'n_start'}); | |||||||||||
| 170 | } else { | ||||||||||||
| 171 | 0 | my $d = $y + max($x,0); | |||||||||||
| 172 | ### right upper diagonal and top horizontal | ||||||||||||
| 173 | ### $d | ||||||||||||
| 174 | ### base: (3*$d - 1 + 2*$w)*$d - $w | ||||||||||||
| 175 | return ((3*$d - 1 + 2*$w)*$d - $w | ||||||||||||
| 176 | - $x | ||||||||||||
| 177 | 0 | + $self->{'n_start'}); | |||||||||||
| 178 | } | ||||||||||||
| 179 | |||||||||||||
| 180 | } else { | ||||||||||||
| 181 | # $y < 0 | ||||||||||||
| 182 | 4 | $x += $w_left; | |||||||||||
| 183 | 4 | ▶ | if ($x-$w <= -$y) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 184 | 2 | my $d = -$y + max(-$x,0); | |||||||||||
| 185 | ### left lower diagonal and bottom horizontal | ||||||||||||
| 186 | ### $d | ||||||||||||
| 187 | ### base: (3*$d + 2 + 2*$w)*$d + 1 | ||||||||||||
| 188 | return ((3*$d + 2 + 2*$w)*$d | ||||||||||||
| 189 | + $x | ||||||||||||
| 190 | 2 | + $self->{'n_start'}); | |||||||||||
| 191 | } else { | ||||||||||||
| 192 | ### right lower vertical | ||||||||||||
| 193 | 2 | my $d = $x - $w; | |||||||||||
| 194 | ### $d | ||||||||||||
| 195 | ### base: (3*$d - 2 + 2*$w)*$d + 1 - $w | ||||||||||||
| 196 | return ((3*$d - 2 + 2*$w)*$d - $w | ||||||||||||
| 197 | + $y | ||||||||||||
| 198 | 2 | + $self->{'n_start'}); | |||||||||||
| 199 | } | ||||||||||||
| 200 | } | ||||||||||||
| 201 | } | ||||||||||||
| 202 | |||||||||||||
| 203 | # not exact | ||||||||||||
| 204 | sub rect_to_n_range { | ||||||||||||
| 205 | 0 | ▶ | my ($self, $x1,$y1, $x2,$y2) = @_; | ||||||||||
|
Subroutine
rect_to_n_range: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 206 | ### HexSpiralSkewed rect_to_n_range(): $x1,$y1, $x2,$y2 | ||||||||||||
| 207 | |||||||||||||
| 208 | 0 | $x1 = round_nearest ($x1); | |||||||||||
| 209 | 0 | $y1 = round_nearest ($y1); | |||||||||||
| 210 | 0 | $x2 = round_nearest ($x2); | |||||||||||
| 211 | 0 | $y2 = round_nearest ($y2); | |||||||||||
| 212 | |||||||||||||
| 213 | 0 | my $w = $self->{'wider'}; | |||||||||||
| 214 | 0 | my $w_right = int($w/2); | |||||||||||
| 215 | 0 | my $w_left = $w - $w_right; | |||||||||||
| 216 | |||||||||||||
| 217 | 0 | my $d = 0; | |||||||||||
| 218 | 0 | foreach my $x ($x1, $x2) { | |||||||||||
| 219 | 0 | $x += $w_left; | |||||||||||
| 220 | 0 | ▶ | if ($x >= $w) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 221 | 0 | $x -= $w; | |||||||||||
| 222 | } | ||||||||||||
| 223 | 0 | foreach my $y ($y1, $y2) { | |||||||||||
| 224 | 0 | ▶ | $d = max ($d, | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 225 | (($y > 0) == ($x > 0) | ||||||||||||
| 226 | ? abs($x) + abs($y) # top right or bottom left diagonals | ||||||||||||
| 227 | : max(abs($x),abs($y)))); # top left or bottom right squares | ||||||||||||
| 228 | } | ||||||||||||
| 229 | } | ||||||||||||
| 230 | 0 | $d += 1; | |||||||||||
| 231 | |||||||||||||
| 232 | # diagonal downwards bottom right being the end of a revolution | ||||||||||||
| 233 | # s=0 | ||||||||||||
| 234 | # s=1 n=7 | ||||||||||||
| 235 | # s=2 n=19 | ||||||||||||
| 236 | # s=3 n=37 | ||||||||||||
| 237 | # s=4 n=61 | ||||||||||||
| 238 | # n = 3*$d*$d + 3*$d + 1 | ||||||||||||
| 239 | # | ||||||||||||
| 240 | ### gives: "sum $d is " . (3*$d*$d + 3*$d + 1) | ||||||||||||
| 241 | |||||||||||||
| 242 | # ENHANCE-ME: find actual minimum if rect doesn't cover 0,0 | ||||||||||||
| 243 | return ($self->{'n_start'}, | ||||||||||||
| 244 | 0 | (3*$d + 3 + 2*$self->{'wider'})*$d + $self->{'n_start'}); | |||||||||||
| 245 | } | ||||||||||||
| 246 | |||||||||||||
| 247 | 2 | 1; | |||||||||||
| 248 | __END__ | ||||||||||||