| 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 | # math-image --path=SquareArms --lines --scale=10 | ||||||||||||||||||||||||||||
| 20 | # math-image --path=SquareArms --all --output=numbers_dash | ||||||||||||||||||||||||||||
| 21 | # math-image --path=SquareArms --values=Polygonal,polygonal=8 | ||||||||||||||||||||||||||||
| 22 | # | ||||||||||||||||||||||||||||
| 23 | # RepdigitsAnyBase fall on 14 or 15 lines ... | ||||||||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | package Math::PlanePath::SquareArms; | ||||||||||||||||||||||||||||
| 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 | #use List::Util 'max'; | ||||||||||||||||||||||||||||
| 30 | 1 | *max = \&Math::PlanePath::_max; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 1 | $VERSION = 128; | |||||||||||||||||||||||||||
| 34 | 1 | ▶ | use Math::PlanePath; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | use Math::PlanePath::Base::NSEW; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | @ISA = ('Math::PlanePath::Base::NSEW', | |||||||||||||||||||||||||||
| 37 | 'Math::PlanePath'); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | use Math::PlanePath::Base::Generic | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | 'round_nearest'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | *_divrem_mutate = \&Math::PlanePath::_divrem_mutate; | |||||||||||||||||||||||||||
| 42 | 1 | *_sqrtint = \&Math::PlanePath::_sqrtint; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||
| 45 | #use Smart::Comments '###'; | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 1 | ▶ | use constant arms_count => 4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | 1 | ▶ | use constant xy_is_visited => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | use constant x_negative_at_n => 4; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use constant y_negative_at_n => 5; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 1 | ▶ | use constant turn_any_right => 0; # only left or straight | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # 28 | ||||||||||||||||||||||||||||
| 58 | # 172 +144 | ||||||||||||||||||||||||||||
| 59 | # 444 +272 +128 | ||||||||||||||||||||||||||||
| 60 | # 844 +400 +128 | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # [ 0, 1, 2, 3,], | ||||||||||||||||||||||||||||
| 63 | # [ 0, 2, 6, 12 ], | ||||||||||||||||||||||||||||
| 64 | # N = (d^2 + d) | ||||||||||||||||||||||||||||
| 65 | # d = -1/2 + sqrt(1 * $n + 1/4) | ||||||||||||||||||||||||||||
| 66 | # = (-1 + 2*sqrt($n + 1/4)) / 2 | ||||||||||||||||||||||||||||
| 67 | # = (-1 + sqrt(4*$n + 1)) / 2 | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub n_to_xy { | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | my ($self, $n) = @_; | ||||||||||||||||||||||||||
|
Subroutine
n_to_xy: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | ### SquareArms n_to_xy(): $n | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ($n < 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | ▶ | if ($n < 1) { return; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | ### centre | ||||||||||||||||||||||||||||
| 76 | 0 | return (0, 1-$n); # from n=1 towards n=5 at x=0,y=-1 | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 0 | $n -= 2; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $frac; | |||||||||||||||||||||||||||
| 81 | 0 | { my $int = int($n); | |||||||||||||||||||||||||||
| 82 | 0 | $frac = $n - $int; | |||||||||||||||||||||||||||
| 83 | 0 | $n = $int; # BigFloat int() gives BigInt, use that | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | # arm as initial rotation | ||||||||||||||||||||||||||||
| 87 | 0 | my $rot = _divrem_mutate($n,4); | |||||||||||||||||||||||||||
| 88 | ### $n | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | my $d = int( (-1 + _sqrtint(4*$n+1)) / 2 ); | |||||||||||||||||||||||||||
| 91 | ### d frac: ((-1 + sqrt(4*$n + 1)) / 2) | ||||||||||||||||||||||||||||
| 92 | ### $d | ||||||||||||||||||||||||||||
| 93 | ### base: $d*($d+1) | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 0 | $n -= $d*($d+1); | |||||||||||||||||||||||||||
| 96 | ### remainder: $n | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | $rot += ($d % 4); | |||||||||||||||||||||||||||
| 99 | 0 | my $x = $d + 1; | |||||||||||||||||||||||||||
| 100 | 0 | my $y = $frac + $n - $d; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | $rot %= 4; | |||||||||||||||||||||||||||
| 103 | 0 | ▶ | if ($rot & 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | $x = -$x; # rotate 180 | |||||||||||||||||||||||||||
| 105 | 0 | $y = -$y; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ($rot & 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 0 | return (-$y,$x); # rotate +90 | |||||||||||||||||||||||||||
| 109 | } else { | ||||||||||||||||||||||||||||
| 110 | 0 | return ($x,$y); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | sub xy_to_n { | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my ($self, $x, $y) = @_; | ||||||||||||||||||||||||||
|
Subroutine
xy_to_n: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | 0 | $x = round_nearest ($x); | |||||||||||||||||||||||||||
| 117 | 0 | $y = round_nearest ($y); | |||||||||||||||||||||||||||
| 118 | ### SquareArms xy_to_n: "$x,$y" | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | if ($x == 0 && $y == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$x == 0 and $y == 0
MC/DC0%0/2
$x == 0 and $y == 0
$x == 0
$y == 0
Truth table0%0/3
A and B
A= $x == 0B= $y == 0
| |||||||||||||||||||||||||||||
| 121 | 0 | return 1; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my $rot = 0; | |||||||||||||||||||||||||||
| 125 | # eg. y=2 have (0<=>$y)-$y == -1-2 == -3 | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if ($y <= ($x <=> 0) - $x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | ### below diagonal, rot 180 ... | ||||||||||||||||||||||||||||
| 128 | 0 | $rot = 2; | |||||||||||||||||||||||||||
| 129 | 0 | $x = -$x; # rotate 180 | |||||||||||||||||||||||||||
| 130 | 0 | $y = -$y; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ($x < $y) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | ### left of diagonal, rot -90 ... | ||||||||||||||||||||||||||||
| 134 | 0 | $rot++; | |||||||||||||||||||||||||||
| 135 | 0 | ($x,$y) = ($y,-$x); # rotate -90 | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # diagonal down from N=2 | ||||||||||||||||||||||||||||
| 139 | # x=1 d=0 n=2 | ||||||||||||||||||||||||||||
| 140 | # x=5 d=4 n=82 | ||||||||||||||||||||||||||||
| 141 | # x=9 d=8 n=290 | ||||||||||||||||||||||||||||
| 142 | # x=13 d=12 n=626 | ||||||||||||||||||||||||||||
| 143 | # N = (4 d^2 + 4 d + 2) | ||||||||||||||||||||||||||||
| 144 | # = (4 x^2 - 4 x + 2) | ||||||||||||||||||||||||||||
| 145 | # offset = y + x-1 upwards from diagonal | ||||||||||||||||||||||||||||
| 146 | # N + 4*offset | ||||||||||||||||||||||||||||
| 147 | # = (4*x^2 - 4*x + 2) + 4*(y + x-1) | ||||||||||||||||||||||||||||
| 148 | # = 4*x^2 - 4*x + 2 + 4*y + 4*x - 4 | ||||||||||||||||||||||||||||
| 149 | # = 4*x^2 + 4*y - 2 | ||||||||||||||||||||||||||||
| 150 | # cf N=4*x^2 is on the X or Y axis, which is X axis after rotation | ||||||||||||||||||||||||||||
| 151 | # | ||||||||||||||||||||||||||||
| 152 | ### xy: "$x,$y" | ||||||||||||||||||||||||||||
| 153 | ### $rot | ||||||||||||||||||||||||||||
| 154 | ### x offset: $x-1 + $y | ||||||||||||||||||||||||||||
| 155 | ### d mod: $d % 4 | ||||||||||||||||||||||||||||
| 156 | ### rot d mod: (($rot-$d) % 4) | ||||||||||||||||||||||||||||
| 157 | 0 | return ($x*$x + $y)*4 - 2 + (($rot-$x+1) % 4); | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # d = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ], | ||||||||||||||||||||||||||||
| 161 | # Nmax = [ 9, 25, 49, 81, 121, 169, 225, 289, 361 ] | ||||||||||||||||||||||||||||
| 162 | # being the N=5 arm one spot before the corner of each run | ||||||||||||||||||||||||||||
| 163 | # N = (4 d^2 + 4 d + 1) | ||||||||||||||||||||||||||||
| 164 | # = (2d+1)^2 | ||||||||||||||||||||||||||||
| 165 | # = ((4*$d + 4)*$d + 1) | ||||||||||||||||||||||||||||
| 166 | # or for d-1 | ||||||||||||||||||||||||||||
| 167 | # N = (4 d^2 - 4 d + 1) | ||||||||||||||||||||||||||||
| 168 | # = (2d-1)^2 | ||||||||||||||||||||||||||||
| 169 | # = ((4*$d - 4)*$d + 1) | ||||||||||||||||||||||||||||
| 170 | # | ||||||||||||||||||||||||||||
| 171 | # not exact | ||||||||||||||||||||||||||||
| 172 | sub rect_to_n_range { | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | my ($self, $x1,$y1, $x2,$y2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rect_to_n_range: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 174 | 0 | my ($d_lo, $d_hi) = _rect_square_range ($x1,$y1, $x2,$y2); | |||||||||||||||||||||||||||
| 175 | 0 | return (((4*$d_lo - 4)*$d_lo + 1), | |||||||||||||||||||||||||||
| 176 | max ($self->xy_to_n($x1,$y1), | ||||||||||||||||||||||||||||
| 177 | $self->xy_to_n($x1,$y2), | ||||||||||||||||||||||||||||
| 178 | $self->xy_to_n($x2,$y1), | ||||||||||||||||||||||||||||
| 179 | $self->xy_to_n($x2,$y2))); | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | sub _rect_square_range { | ||||||||||||||||||||||||||||
| 183 | 1400 | ▶ | my ($x1,$y1, $x2,$y2) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_rect_square_range: called CC=8 SCAR=22.2
| |||||||||||||||||||||||||||||
| 184 | ### _rect_square_range(): "$x1,$y1 $x2,$y2" | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 1400 | $x1 = round_nearest ($x1); | |||||||||||||||||||||||||||
| 187 | 1400 | $y1 = round_nearest ($y1); | |||||||||||||||||||||||||||
| 188 | 1400 | $x2 = round_nearest ($x2); | |||||||||||||||||||||||||||
| 189 | 1400 | $y2 = round_nearest ($y2); | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # if x1,x2 opposite signs then origin x=0 covered, similarly y | ||||||||||||||||||||||||||||
| 192 | 1400 | my $x_zero_uncovered = ($x1<0) == ($x2<0); | |||||||||||||||||||||||||||
| 193 | 1400 | my $y_zero_uncovered = ($y1<0) == ($y2<0); | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 1400 | foreach ($x1,$y1, $x2,$y2) { | |||||||||||||||||||||||||||
| 196 | 5600 | $_ = abs($_); | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | ### abs rect: "x=$x1 to $x2, y=$y1 to $y2" | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 1400 | ▶ | if ($x2 < $x1) { ($x1,$x2) = ($x2,$x1) } # swap to x1<x2 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 1400 | ▶ | if ($y2 < $y1) { ($y1,$y2) = ($y2,$y1) } # swap to y1<y2 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 1400 | ▶ | my $dlo = ($x_zero_uncovered ? $x1 : 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 204 | 1400 | ▶ | if ($y_zero_uncovered && $dlo < $y1) { $dlo = $y1 } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$y_zero_uncovered and $dlo < $y1
MC/DC0%0/2
$y_zero_uncovered and $dlo < $y1
$y_zero_uncovered
$dlo < $y1
Truth table66%2/3
A and B
A= $y_zero_uncoveredB= $dlo < $y1
| |||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 1400 | ▶ | return ($dlo, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | ($x2 > $y2 ? $x2 : $y2)); | ||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1 | 1; | |||||||||||||||||||||||||||
| 211 | __END__ | ||||||||||||||||||||||||||||