| 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 | ||||||||||||||||||||||||||||||||
| 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 | # math-image --path=KochSnowflakes --lines --scale=10 | ||||||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||||||
| 21 | # area approaches sqrt(48)/10 | ||||||||||||||||||||||||||||||||
| 22 | # * height=sqrt(1-1/4)=sqrt(3)/2 | ||||||||||||||||||||||||||||||||
| 23 | # /|\ halfbase=1/2 | ||||||||||||||||||||||||||||||||
| 24 | # / | \ trianglearea = sqrt(3)/4 | ||||||||||||||||||||||||||||||||
| 25 | # *-----* | ||||||||||||||||||||||||||||||||
| 26 | # segments = 3*4^level = 3,12,48,192,... | ||||||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||||||
| 28 | # with initial triangle area=1 | ||||||||||||||||||||||||||||||||
| 29 | # add a new triangle onto each side | ||||||||||||||||||||||||||||||||
| 30 | # x,y scale by 3* so 9*area | ||||||||||||||||||||||||||||||||
| 31 | # | ||||||||||||||||||||||||||||||||
| 32 | # area[level+1] = 9*area[level] + segments | ||||||||||||||||||||||||||||||||
| 33 | # = 9*area[level] + 3*4^level | ||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||
| 35 | # area[0] = 1 | ||||||||||||||||||||||||||||||||
| 36 | # area[1] = 9*area[0] + 3 = 9 + 3 = 12 | ||||||||||||||||||||||||||||||||
| 37 | # area[2] = 9*area[1] + 3*4 | ||||||||||||||||||||||||||||||||
| 38 | # = 9*(9*1 + 3) + 3*4 | ||||||||||||||||||||||||||||||||
| 39 | # = 9*9 + 3*9 + 3*4 = 120 | ||||||||||||||||||||||||||||||||
| 40 | # area[3] = 9*area[2] + 3*4 | ||||||||||||||||||||||||||||||||
| 41 | # = 9*(9*9 + 3*9 + 3*4) + 3*4^2 | ||||||||||||||||||||||||||||||||
| 42 | # = 9^3 + 3*9^2 + 3*0*4 + 3*4^2 | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | # area[level+1] | ||||||||||||||||||||||||||||||||
| 45 | # = 9^(level+1) + (9^(level+1) - 4^(level+1)) * 3/5 | ||||||||||||||||||||||||||||||||
| 46 | # = (5*9^(level+1) + 3*9^(level+1) - 3*4^(level+1)) / 5 | ||||||||||||||||||||||||||||||||
| 47 | # = (8*9^(level+1) - 3*4^(level+1)) / 5 | ||||||||||||||||||||||||||||||||
| 48 | # | ||||||||||||||||||||||||||||||||
| 49 | # area[level] = (8*9^level - 3*4^level) / 5 | ||||||||||||||||||||||||||||||||
| 50 | # = 1,12,120,1128,10344,93864,847848 | ||||||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||||||
| 52 | # . | ||||||||||||||||||||||||||||||||
| 53 | # / \ area[0] = 1 | ||||||||||||||||||||||||||||||||
| 54 | # .---. | ||||||||||||||||||||||||||||||||
| 55 | # | ||||||||||||||||||||||||||||||||
| 56 | # . | ||||||||||||||||||||||||||||||||
| 57 | # / \ area=[1] = 12 = 9*area[0] + 3*4^0 | ||||||||||||||||||||||||||||||||
| 58 | # .---.---.---. | ||||||||||||||||||||||||||||||||
| 59 | # \ / \ / \ / | ||||||||||||||||||||||||||||||||
| 60 | # .---.---. | ||||||||||||||||||||||||||||||||
| 61 | # / \ / \ / \ | ||||||||||||||||||||||||||||||||
| 62 | # .---.---.---. | ||||||||||||||||||||||||||||||||
| 63 | # \ / | ||||||||||||||||||||||||||||||||
| 64 | # . | ||||||||||||||||||||||||||||||||
| 65 | # | ||||||||||||||||||||||||||||||||
| 66 | # area[level] / 9^level | ||||||||||||||||||||||||||||||||
| 67 | # = (8*9^level / 9^level - 3*4^level / 9^level) / 5 | ||||||||||||||||||||||||||||||||
| 68 | # = (8 - 3*(4/0)^level)/5 | ||||||||||||||||||||||||||||||||
| 69 | # -> 8/5 as level->infinity | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | # in integer coords | ||||||||||||||||||||||||||||||||
| 72 | # initial triangle area | ||||||||||||||||||||||||||||||||
| 73 | # * 2/3 1*2 / 2 = 1 unit | ||||||||||||||||||||||||||||||||
| 74 | # / \ | ||||||||||||||||||||||||||||||||
| 75 | # *---* -1/3 | ||||||||||||||||||||||||||||||||
| 76 | # -1 +1 | ||||||||||||||||||||||||||||||||
| 77 | # | ||||||||||||||||||||||||||||||||
| 78 | # so area[level] / (sqrt(3)/2) | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | package Math::PlanePath::KochSnowflakes; | ||||||||||||||||||||||||||||||||
| 83 | 1 | ▶ | use 5.004; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 85 | #use List::Util 'max'; | ||||||||||||||||||||||||||||||||
| 86 | 1 | *max = \&Math::PlanePath::_max; | |||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | use vars '$VERSION', '@ISA'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 89 | 1 | $VERSION = 128; | |||||||||||||||||||||||||||||||
| 90 | 1 | ▶ | use Math::PlanePath; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 91 | 1 | @ISA = ('Math::PlanePath'); | |||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | use Math::PlanePath::Base::Generic | ||||||||||||||||||||||||||||||||
| 94 | 1 | 'is_infinite', | |||||||||||||||||||||||||||||||
| 95 | 1 | ▶ | 'round_nearest'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 96 | use Math::PlanePath::Base::Digits | ||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | 'round_down_pow'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 98 | 1 | ▶ | use Math::PlanePath::KochCurve; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | # uncomment this to run the ### lines | ||||||||||||||||||||||||||||||||
| 101 | # use Smart::Comments; | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | use constant n_frac_discontinuity => 0; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | use constant x_negative_at_n => 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | use constant y_negative_at_n => 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 107 | 1 | ▶ | use constant sumabsxy_minimum => 2/3; # minimum X=0,Y=2/3 | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | use constant rsquared_minimum => 4/9; # minimum X=0,Y=2/3 | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 109 | # maybe: use constant radius_minimum => 2/3; # minimum X=0,Y=2/3 | ||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | # jump across rings is WSW slope 2, so following maximums | ||||||||||||||||||||||||||||||||
| 112 | 1 | ▶ | use constant dx_maximum => 2; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 113 | 1 | ▶ | use constant dy_maximum => 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 114 | 1 | ▶ | use constant dsumxy_maximum => 2; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 115 | 1 | ▶ | use constant ddiffxy_maximum => 2; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | use constant absdx_minimum => 1; # never vertical | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 118 | 1 | ▶ | use constant dir_maximum_dxdy => (1,-1); # South-East | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | # N=1 gcd(-1, -1/3) = 1/3 | ||||||||||||||||||||||||||||||||
| 121 | # N=2 gcd( 1, -1/3) = 1/3 | ||||||||||||||||||||||||||||||||
| 122 | # N=3 gcd( 0, 2/3) = 2/3 | ||||||||||||||||||||||||||||||||
| 123 | 1 | ▶ | use constant gcdxy_minimum => 1/3; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | use constant turn_any_straight => 0; # never straight | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 129 | sub new { | ||||||||||||||||||||||||||||||||
| 130 | 4 | ▶ | my $self = shift->SUPER::new (@_); | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 131 | 4 | ▶ | $self->{'sides'} ||= 3; # default | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"sides"} ||= 3
MC/DC0%0/1
$self->{"sides"} ||= 3
$self->{"sides"}
Truth table50%1/2
A
A= $self->{"sides"}
| |||||||||||||||||||||||||||||||||
| 132 | 4 | return $self; | |||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | # N=1 to 3 3 of, level=1 | ||||||||||||||||||||||||||||||||
| 136 | # N=4 to 15 12 of, level=2 | ||||||||||||||||||||||||||||||||
| 137 | # N=16 to .. 48 of, level=3 | ||||||||||||||||||||||||||||||||
| 138 | # | ||||||||||||||||||||||||||||||||
| 139 | # each loop = 3*4^level | ||||||||||||||||||||||||||||||||
| 140 | # | ||||||||||||||||||||||||||||||||
| 141 | # n_base = 1 + 3*4^0 + 3*4^1 + ... + 3*4^(level-1) | ||||||||||||||||||||||||||||||||
| 142 | # = 1 + 3*[ 4^0 + 4^1 + ... + 4^(level-1) ] | ||||||||||||||||||||||||||||||||
| 143 | # = 1 + 3*[ (4^level - 1)/3 ] | ||||||||||||||||||||||||||||||||
| 144 | # = 1 + (4^level - 1) | ||||||||||||||||||||||||||||||||
| 145 | # = 4^level | ||||||||||||||||||||||||||||||||
| 146 | # | ||||||||||||||||||||||||||||||||
| 147 | # each side = loop/3 | ||||||||||||||||||||||||||||||||
| 148 | # = 3*4^level / 3 | ||||||||||||||||||||||||||||||||
| 149 | # = 4^level | ||||||||||||||||||||||||||||||||
| 150 | # | ||||||||||||||||||||||||||||||||
| 151 | # 6 sides | ||||||||||||||||||||||||||||||||
| 152 | # n_base = 1 + 2*3*4^0 + ... | ||||||||||||||||||||||||||||||||
| 153 | # = 2*4^level - 1 | ||||||||||||||||||||||||||||||||
| 154 | # level = log4 (n+1)/2 | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | ### loop 1: 3* 4**1 | ||||||||||||||||||||||||||||||||
| 157 | ### loop 2: 3* 4**2 | ||||||||||||||||||||||||||||||||
| 158 | ### loop 3: 3* 4**3 | ||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | # sub _level_to_base { | ||||||||||||||||||||||||||||||||
| 161 | # my ($level) = @_; | ||||||||||||||||||||||||||||||||
| 162 | # return -3*$level + 4**($level+1) - 2; | ||||||||||||||||||||||||||||||||
| 163 | # } | ||||||||||||||||||||||||||||||||
| 164 | # ### level_to_base(1): _level_to_base(1) | ||||||||||||||||||||||||||||||||
| 165 | # ### level_to_base(2): _level_to_base(2) | ||||||||||||||||||||||||||||||||
| 166 | # ### level_to_base(3): _level_to_base(3) | ||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||
| 168 | sub n_to_xy { | ||||||||||||||||||||||||||||||||
| 169 | 18 | ▶ | my ($self, $n) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
n_to_xy: called CC=11 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 170 | ### KochSnowflakes n_to_xy(): $n | ||||||||||||||||||||||||||||||||
| 171 | 18 | ▶ | if ($n < 1) { return; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 172 | 18 | ▶ | if (is_infinite($n)) { return ($n,$n); } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||
| 174 | 18 | my $sides = $self->{'sides'}; | |||||||||||||||||||||||||||||||
| 175 | 18 | ▶ | my ($sidelen, $level) = round_down_pow (($sides == 6 ? ($n+1)/2 : $n), | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 176 | 4); | ||||||||||||||||||||||||||||||||
| 177 | 18 | ▶ | my $base = ($sides == 6 ? 2*$sidelen - 1 : $sidelen); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | 18 | my $rem = $n - $base; | |||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||
| 180 | ### $level | ||||||||||||||||||||||||||||||||
| 181 | ### $base | ||||||||||||||||||||||||||||||||
| 182 | ### $sidelen | ||||||||||||||||||||||||||||||||
| 183 | ### $rem | ||||||||||||||||||||||||||||||||
| 184 | ### assert: $n >= $base | ||||||||||||||||||||||||||||||||
| 185 | ### assert: $rem >= 0 | ||||||||||||||||||||||||||||||||
| 186 | ### assert: $rem < $sidelen * $sides | ||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||
| 188 | 18 | my $side = int($rem / $sidelen); | |||||||||||||||||||||||||||||||
| 189 | ### $side | ||||||||||||||||||||||||||||||||
| 190 | ### $rem | ||||||||||||||||||||||||||||||||
| 191 | ### subtract: $side*$sidelen | ||||||||||||||||||||||||||||||||
| 192 | 18 | $rem -= $side*$sidelen; | |||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||
| 194 | ### assert: $side >= 0 && $side < $sides | ||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | 18 | my ($x, $y) = Math::PlanePath::KochCurve->n_to_xy ($rem); | |||||||||||||||||||||||||||||||
| 197 | ### $x | ||||||||||||||||||||||||||||||||
| 198 | ### $y | ||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||
| 200 | 18 | ▶ | if ($sides == 3) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 201 | 18 | my $len = 3**($level-1); | |||||||||||||||||||||||||||||||
| 202 | 18 | ▶ | if ($side < 1) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 203 | ### horizontal rightwards | ||||||||||||||||||||||||||||||||
| 204 | 12 | return ($x - 3*$len, | |||||||||||||||||||||||||||||||
| 205 | -$y - $len); | ||||||||||||||||||||||||||||||||
| 206 | } elsif ($side < 2) { | ||||||||||||||||||||||||||||||||
| 207 | ### right slope upwards | ||||||||||||||||||||||||||||||||
| 208 | 0 | return (($x-3*$y)/-2 + 3*$len, # flip vert and rotate +120 | |||||||||||||||||||||||||||||||
| 209 | ($x+$y)/2 - $len); | ||||||||||||||||||||||||||||||||
| 210 | } else { | ||||||||||||||||||||||||||||||||
| 211 | ### left slope downwards | ||||||||||||||||||||||||||||||||
| 212 | 6 | return ((-3*$y-$x)/2, # flip vert and rotate -120 | |||||||||||||||||||||||||||||||
| 213 | ($y-$x)/2 + 2*$len); | ||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||
| 215 | } else { | ||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||
| 217 | # 3 | ||||||||||||||||||||||||||||||||
| 218 | # 5-----4 | ||||||||||||||||||||||||||||||||
| 219 | # 4 / \ | ||||||||||||||||||||||||||||||||
| 220 | # / \ 2 | ||||||||||||||||||||||||||||||||
| 221 | # 6 o 3 | ||||||||||||||||||||||||||||||||
| 222 | # 5 \ . . / | ||||||||||||||||||||||||||||||||
| 223 | # \ . . / 1 | ||||||||||||||||||||||||||||||||
| 224 | # 1-----2 | ||||||||||||||||||||||||||||||||
| 225 | # 0 | ||||||||||||||||||||||||||||||||
| 226 | # 7 | ||||||||||||||||||||||||||||||||
| 227 | # | ||||||||||||||||||||||||||||||||
| 228 | 0 | my $len = 3**$level; | |||||||||||||||||||||||||||||||
| 229 | 0 | $x -= $len; # -y flip vert and offset | |||||||||||||||||||||||||||||||
| 230 | 0 | $y = -$y - $len; | |||||||||||||||||||||||||||||||
| 231 | 0 | ▶ | if ($side >= 3) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 232 | ### rotate 180 ... | ||||||||||||||||||||||||||||||||
| 233 | 0 | $x = -$x; # rotate 180 | |||||||||||||||||||||||||||||||
| 234 | 0 | $y = -$y; | |||||||||||||||||||||||||||||||
| 235 | 0 | $side -= 3; | |||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | if ($side >= 2) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | ### upper right slope upwards ... | ||||||||||||||||||||||||||||||||
| 239 | 0 | return (($x+3*$y)/-2, # rotate +120 | |||||||||||||||||||||||||||||||
| 240 | ($x-$y)/2); | ||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||
| 242 | 0 | ▶ | if ($side >= 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 243 | ### lower right slope upwards ... | ||||||||||||||||||||||||||||||||
| 244 | 0 | return (($x-3*$y)/2, # rotate +60 | |||||||||||||||||||||||||||||||
| 245 | ($x+$y)/2); | ||||||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||||||
| 247 | ### horizontal ... | ||||||||||||||||||||||||||||||||
| 248 | 0 | return ($x,$y); | |||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | # N=1 overlaps N=5 | ||||||||||||||||||||||||||||||||
| 254 | # N=2 overlaps N=7 | ||||||||||||||||||||||||||||||||
| 255 | # +---------+ +---------+ Y=1.5 | ||||||||||||||||||||||||||||||||
| 256 | # | | | | | ||||||||||||||||||||||||||||||||
| 257 | # | +---------+ | Y=7/6 = 1.166 | ||||||||||||||||||||||||||||||||
| 258 | # | | | | | ||||||||||||||||||||||||||||||||
| 259 | # | * 13 | | * 11 | Y=1 | ||||||||||||||||||||||||||||||||
| 260 | # | | | | | ||||||||||||||||||||||||||||||||
| 261 | # | | * 3 | | Y=2/3 = 0.666 | ||||||||||||||||||||||||||||||||
| 262 | # | | | | | ||||||||||||||||||||||||||||||||
| 263 | # +---------+ +---------+ Y=0.5 | ||||||||||||||||||||||||||||||||
| 264 | # | | | ||||||||||||||||||||||||||||||||
| 265 | # +---------+---------+---------+ Y=1/6 = 0.166 | ||||||||||||||||||||||||||||||||
| 266 | # | | O | | --Y=0 | ||||||||||||||||||||||||||||||||
| 267 | # | | | | | ||||||||||||||||||||||||||||||||
| 268 | # | | | | | ||||||||||||||||||||||||||||||||
| 269 | # | * 1 | | * 2 | Y=-1/3 = -0.333 | ||||||||||||||||||||||||||||||||
| 270 | # | | | | | ||||||||||||||||||||||||||||||||
| 271 | # +---------+ +---------+ Y=-3/6 = -0.5 | ||||||||||||||||||||||||||||||||
| 272 | # | | | | | ||||||||||||||||||||||||||||||||
| 273 | # +---------+ +---------+ Y=-5/6 = -0.833 | ||||||||||||||||||||||||||||||||
| 274 | # | | | | | ||||||||||||||||||||||||||||||||
| 275 | # | * 5 | | * 7 | Y=-1 | ||||||||||||||||||||||||||||||||
| 276 | # | | | | | ||||||||||||||||||||||||||||||||
| 277 | # | | | | | ||||||||||||||||||||||||||||||||
| 278 | # +---------+ +---------+ Y=-1.5 | ||||||||||||||||||||||||||||||||
| 279 | # | ||||||||||||||||||||||||||||||||
| 280 | sub xy_to_n { | ||||||||||||||||||||||||||||||||
| 281 | 40 | ▶ | return scalar((shift->xy_to_n_list(@_))[0]); | ||||||||||||||||||||||||||||||
|
Subroutine
xy_to_n: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||
| 283 | sub xy_to_n_list { | ||||||||||||||||||||||||||||||||
| 284 | 58 | ▶ | my ($self, $x, $y) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
xy_to_n_list: called CC=17 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||||||
| 285 | ### KochSnowflakes xy_to_n(): "$x, $y" | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | 58 | $x = round_nearest ($x); | |||||||||||||||||||||||||||||||
| 288 | 58 | ▶ | if (abs($x) <= 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 289 | 44 | ▶ | if ($x == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 290 | 13 | my $y6 = 6*$y; | |||||||||||||||||||||||||||||||
| 291 | 13 | ▶ | if ($y6 >= 1 && $y6 < 7) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$y6 >= 1 and $y6 < 7
MC/DC50%1/2
$y6 >= 1 and $y6 < 7
$y6 >= 1
$y6 < 7
Truth table66%2/3
A and B
A= $y6 >= 1B= $y6 < 7
| |||||||||||||||||||||||||||||||||
| 292 | # Y = 2/3-1/2=1/6 to 2/3+1/2=7/6 | ||||||||||||||||||||||||||||||||
| 293 | 9 | return 3; | |||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||
| 295 | } else { | ||||||||||||||||||||||||||||||||
| 296 | 31 | my $y6 = 6*$y; | |||||||||||||||||||||||||||||||
| 297 | 31 | ▶ | if ($y6 >= -5 && $y6 < 1) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$y6 >= -5 and $y6 < 1
MC/DC50%1/2
$y6 >= -5 and $y6 < 1
$y6 >= -5
$y6 < 1
Truth table66%2/3
A and B
A= $y6 >= -5B= $y6 < 1
| |||||||||||||||||||||||||||||||||
| 298 | # Y = -1/3-1/2=-5/6 to -1/3+1/2=+1/6 | ||||||||||||||||||||||||||||||||
| 299 | 24 | ▶ | return (1 + ($x > 0), | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 300 | ($y6 < -3 ? (5+2*($x>0)) : ())); # 5 or 7 up to Y<-1/2 | ||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||
| 305 | 25 | $y = round_nearest ($y); | |||||||||||||||||||||||||||||||
| 306 | 25 | ▶ | if (($x % 2) != ($y % 2)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 307 | ### diff parity... | ||||||||||||||||||||||||||||||||
| 308 | 9 | return; | |||||||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||
| 311 | 16 | my $high; | |||||||||||||||||||||||||||||||
| 312 | 16 | ▶ | if ($x > 0 && $x >= -3*$y) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$x > 0 and $x >= -3 * $y
MC/DC100%2/2
$x > 0 and $x >= -3 * $y
$x > 0
$x >= -3 * $y
Truth table100%3/3
A and B
A= $x > 0B= $x >= -3 * $y
| |||||||||||||||||||||||||||||||||
| 313 | ### right upper third n=2 ... | ||||||||||||||||||||||||||||||||
| 314 | 3 | ($x,$y) = ((3*$y-$x)/2, # rotate -120 and flip vert | |||||||||||||||||||||||||||||||
| 315 | ($x+$y)/2); | ||||||||||||||||||||||||||||||||
| 316 | 3 | $high = 2; | |||||||||||||||||||||||||||||||
| 317 | ▶ | } elsif ($x <= 0 && 3*$y > $x) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$x <= 0 and 3 * $y > $x
MC/DC100%2/2
$x <= 0 and 3 * $y > $x
$x <= 0
3 * $y > $x
Truth table100%3/3
A and B
A= $x <= 0B= 3 * $y > $x
| |||||||||||||||||||||||||||||||||
| 318 | ### left upper third n=3 ... | ||||||||||||||||||||||||||||||||
| 319 | 3 | ($x,$y) = (($x+3*$y)/-2, # rotate +120 and flip vert | |||||||||||||||||||||||||||||||
| 320 | ($y-$x)/2); | ||||||||||||||||||||||||||||||||
| 321 | 3 | $high = 3; | |||||||||||||||||||||||||||||||
| 322 | } else { | ||||||||||||||||||||||||||||||||
| 323 | ### lower third n=1 ... | ||||||||||||||||||||||||||||||||
| 324 | 10 | $y = -$y; # flip vert | |||||||||||||||||||||||||||||||
| 325 | 10 | $high = 1; | |||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||
| 327 | ### rotate/flip is: "$x,$y" | ||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||
| 329 | 16 | ▶ | if ($y <= 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 330 | 0 | return; | |||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||
| 333 | 16 | my ($len,$level) = round_down_pow($y, 3); | |||||||||||||||||||||||||||||||
| 334 | 16 | $level += 1; | |||||||||||||||||||||||||||||||
| 335 | ### $level | ||||||||||||||||||||||||||||||||
| 336 | ### $len | ||||||||||||||||||||||||||||||||
| 337 | 16 | ▶ | if (is_infinite($level)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 338 | 0 | return $level; | |||||||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||
| 342 | 16 | $y -= $len; # shift to Y=0 basis | |||||||||||||||||||||||||||||||
| 343 | 16 | $len *= 3; | |||||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||||||
| 345 | ### compare for end: ($x+$y)." >= 3*len=".$len | ||||||||||||||||||||||||||||||||
| 346 | 16 | ▶ | if ($x + $y >= $len) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 347 | ### past end of this level, no points ... | ||||||||||||||||||||||||||||||||
| 348 | 3 | return; | |||||||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||||||
| 350 | 13 | $x += $len; # shift to X=0 basis | |||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | 13 | my $n = Math::PlanePath::KochCurve->xy_to_n($x, $y); | |||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||
| 354 | ### plain curve on: ($x+3*$len).",".($y-$len)." n=".(defined $n && $n) | ||||||||||||||||||||||||||||||||
| 355 | ### $high | ||||||||||||||||||||||||||||||||
| 356 | ### high: (4**$level)*$high | ||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||
| 358 | 13 | ▶ | if (defined $n) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 359 | 10 | return (4**$level)*$high + $n; | |||||||||||||||||||||||||||||||
| 360 | } else { | ||||||||||||||||||||||||||||||||
| 361 | 3 | return; | |||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||
| 365 | # level extends to x= +/- 3^level | ||||||||||||||||||||||||||||||||
| 366 | # y= +/- 2*3^(level-1) | ||||||||||||||||||||||||||||||||
| 367 | # = 2/3 * 3^level | ||||||||||||||||||||||||||||||||
| 368 | # 1.5*y = 3^level | ||||||||||||||||||||||||||||||||
| 369 | # | ||||||||||||||||||||||||||||||||
| 370 | # ENHANCE-ME: share KochCurve segment checker to find actual min/max | ||||||||||||||||||||||||||||||||
| 371 | # | ||||||||||||||||||||||||||||||||
| 372 | # not exact | ||||||||||||||||||||||||||||||||
| 373 | sub rect_to_n_range { | ||||||||||||||||||||||||||||||||
| 374 | 11 | ▶ | my ($self, $x1,$y1, $x2,$y2) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
rect_to_n_range: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 375 | ### KochSnowflakes rect_to_n_range(): "$x1,$y1 $x2,$y2" | ||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||
| 377 | 11 | $x1 = round_nearest ($x1); | |||||||||||||||||||||||||||||||
| 378 | 11 | $y1 = round_nearest ($y1); | |||||||||||||||||||||||||||||||
| 379 | 11 | $x2 = round_nearest ($x2); | |||||||||||||||||||||||||||||||
| 380 | 11 | $y2 = round_nearest ($y2); | |||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||
| 382 | 11 | ▶ | ($x1,$x2) = ($x2,$x1) if $x1 > $x2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 383 | 11 | ▶ | ($y1,$y2) = ($y2,$y1) if $y1 > $y2; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||
| 385 | # | ||||||||||||||||||||||||||||||||
| 386 | # | | ||||||||||||||||||||||||||||||||
| 387 | # +------ . -----+ | ||||||||||||||||||||||||||||||||
| 388 | # |x1,y2 /|\ x2,y2| | ||||||||||||||||||||||||||||||||
| 389 | # / | \ | ||||||||||||||||||||||||||||||||
| 390 | # / | \ | ||||||||||||||||||||||||||||||||
| 391 | # -----/---m---\----- | ||||||||||||||||||||||||||||||||
| 392 | # / | \ | ||||||||||||||||||||||||||||||||
| 393 | # .-----------. | ||||||||||||||||||||||||||||||||
| 394 | # | | ||||||||||||||||||||||||||||||||
| 395 | # y1 | ||||||||||||||||||||||||||||||||
| 396 | # ------- | ||||||||||||||||||||||||||||||||
| 397 | # | ||||||||||||||||||||||||||||||||
| 398 | # -y1 bottom horizontal | ||||||||||||||||||||||||||||||||
| 399 | # (x2+y2)/2 right side | ||||||||||||||||||||||||||||||||
| 400 | # (-x1+y2)/2 left side | ||||||||||||||||||||||||||||||||
| 401 | # each giving a power of 3 of the level | ||||||||||||||||||||||||||||||||
| 402 | # | ||||||||||||||||||||||||||||||||
| 403 | ### right: ($x2+$y2)/2 | ||||||||||||||||||||||||||||||||
| 404 | ### left: (-$x1+$y2)/2 | ||||||||||||||||||||||||||||||||
| 405 | ### bottom: -$y1 | ||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||
| 407 | 11 | my $sides = $self->{'sides'}; | |||||||||||||||||||||||||||||||
| 408 | 11 | ▶ | my ($pow, $level) = round_down_pow (max ($sides == 6 | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 409 | ? ($x1/-2, | ||||||||||||||||||||||||||||||||
| 410 | $x2/2, | ||||||||||||||||||||||||||||||||
| 411 | -$y1, | ||||||||||||||||||||||||||||||||
| 412 | $y2) | ||||||||||||||||||||||||||||||||
| 413 | : (int(($x2+$y2)/2), | ||||||||||||||||||||||||||||||||
| 414 | int((-$x1+$y2)/2), | ||||||||||||||||||||||||||||||||
| 415 | -$y1)), | ||||||||||||||||||||||||||||||||
| 416 | 3); | ||||||||||||||||||||||||||||||||
| 417 | ### $level | ||||||||||||||||||||||||||||||||
| 418 | # end of $level is 1 before base of $level+1 | ||||||||||||||||||||||||||||||||
| 419 | 11 | return (1, 4**($level+2) - 1); | |||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||
| 422 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 423 | # Nstart = 4^k | ||||||||||||||||||||||||||||||||
| 424 | # length = 3*4^k many points | ||||||||||||||||||||||||||||||||
| 425 | # Nend = Nstart + length-1 | ||||||||||||||||||||||||||||||||
| 426 | # = 4^k + 3*4^k - 1 | ||||||||||||||||||||||||||||||||
| 427 | # = 4*4^k - 1 | ||||||||||||||||||||||||||||||||
| 428 | # = Nstart(k+1) - 1 | ||||||||||||||||||||||||||||||||
| 429 | sub level_to_n_range { | ||||||||||||||||||||||||||||||||
| 430 | 9 | ▶ | my ($self, $level) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
level_to_n_range: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 431 | 9 | my $pow = 4**$level; | |||||||||||||||||||||||||||||||
| 432 | 9 | return ($pow, 4*$pow-1); | |||||||||||||||||||||||||||||||
| 433 | } | ||||||||||||||||||||||||||||||||
| 434 | sub n_to_level { | ||||||||||||||||||||||||||||||||
| 435 | 0 | ▶ | my ($self, $n) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
n_to_level: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 436 | 0 | ▶ | if ($n < 1) { return undef; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | if (is_infinite($n)) { return $n; } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 438 | 0 | $n = round_nearest($n); | |||||||||||||||||||||||||||||||
| 439 | 0 | ▶ | my ($sidelen, $level) = round_down_pow (($self->{'sides'} == 6 ? ($n+1)/2 : $n), | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 440 | 4); | ||||||||||||||||||||||||||||||||
| 441 | 0 | return $level; | |||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||
| 444 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||
| 445 | 1 | 1; | |||||||||||||||||||||||||||||||
| 446 | __END__ | ||||||||||||||||||||||||||||||||