line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Copyright 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 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 . |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
# math-image --path=GosperReplicate --lines --scale=10 |
20
|
|
|
|
|
|
|
# math-image --path=GosperReplicate --all --output=numbers_dash |
21
|
|
|
|
|
|
|
# math-image --path=GosperReplicate,numbering_type=rotate --all --output=numbers_dash |
22
|
|
|
|
|
|
|
# |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
package Math::PlanePath::GosperReplicate; |
25
|
1
|
|
|
1
|
|
1777
|
use 5.004; |
|
1
|
|
|
|
|
4
|
|
26
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
27
|
1
|
|
|
1
|
|
5
|
use List::Util qw(max); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
91
|
|
28
|
1
|
|
|
1
|
|
475
|
use POSIX 'ceil'; |
|
1
|
|
|
|
|
6106
|
|
|
1
|
|
|
|
|
5
|
|
29
|
1
|
|
|
1
|
|
1830
|
use Math::Libm 'hypot'; |
|
1
|
|
|
|
|
3217
|
|
|
1
|
|
|
|
|
64
|
|
30
|
1
|
|
|
1
|
|
630
|
use Math::PlanePath::SacksSpiral; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
35
|
|
31
|
|
|
|
|
|
|
|
32
|
1
|
|
|
1
|
|
6
|
use vars '$VERSION', '@ISA'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
51
|
|
33
|
|
|
|
|
|
|
$VERSION = 127; |
34
|
1
|
|
|
1
|
|
6
|
use Math::PlanePath; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
35
|
|
|
|
|
|
|
@ISA = ('Math::PlanePath'); |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
use Math::PlanePath::Base::Generic |
38
|
1
|
|
|
|
|
47
|
'is_infinite', |
39
|
1
|
|
|
1
|
|
5
|
'round_nearest'; |
|
1
|
|
|
|
|
1
|
|
40
|
|
|
|
|
|
|
use Math::PlanePath::Base::Digits |
41
|
1
|
|
|
|
|
84
|
'round_up_pow', |
42
|
|
|
|
|
|
|
'digit_split_lowtohigh', |
43
|
1
|
|
|
1
|
|
641
|
'digit_join_lowtohigh'; |
|
1
|
|
|
|
|
3
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
# uncomment this to run the ### lines |
46
|
|
|
|
|
|
|
#use Smart::Comments; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
56
|
use constant parameter_info_array => |
50
|
|
|
|
|
|
|
[ { name => 'numbering_type', |
51
|
|
|
|
|
|
|
display => 'Numbering', |
52
|
|
|
|
|
|
|
share_key => 'numbering_type_rotate', |
53
|
|
|
|
|
|
|
type => 'enum', |
54
|
|
|
|
|
|
|
default => 'fixed', |
55
|
|
|
|
|
|
|
choices => ['fixed','rotate'], |
56
|
|
|
|
|
|
|
choices_display => ['Fixed','Rotate'], |
57
|
|
|
|
|
|
|
description => 'Fixed or rotating sub-part numbering.', |
58
|
|
|
|
|
|
|
}, |
59
|
1
|
|
|
1
|
|
7
|
]; |
|
1
|
|
|
|
|
3
|
|
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
1
|
|
6
|
use constant n_start => 0; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
68
|
|
62
|
|
|
|
|
|
|
*xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_even; |
63
|
|
|
|
|
|
|
|
64
|
1
|
|
|
1
|
|
6
|
use constant x_negative_at_n => 3; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
44
|
|
65
|
1
|
|
|
1
|
|
6
|
use constant y_negative_at_n => 5; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
60
|
|
66
|
1
|
|
|
1
|
|
7
|
use constant absdx_minimum => 1; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
67
|
1
|
|
|
1
|
|
5
|
use constant dir_maximum_dxdy => (3,-1); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1125
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
70
|
|
|
|
|
|
|
sub new { |
71
|
6
|
|
|
6
|
1
|
1272
|
my $self = shift->SUPER::new (@_); |
72
|
6
|
|
100
|
|
|
35
|
$self->{'numbering_type'} ||= 'fixed'; # default |
73
|
6
|
|
|
|
|
15
|
return $self; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub _digits_rotate_lowtohigh { |
77
|
7626
|
|
|
7626
|
|
15004
|
my ($aref) = @_; |
78
|
7626
|
|
|
|
|
10224
|
my $rot = 0; |
79
|
7626
|
|
|
|
|
12624
|
foreach my $digit (reverse @$aref) { |
80
|
25123
|
100
|
|
|
|
41278
|
if ($digit) { |
81
|
22617
|
|
|
|
|
30234
|
$rot += $digit-1; |
82
|
22617
|
|
|
|
|
34249
|
$digit = ($rot % 6) + 1; # mutate $aref |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
sub _digits_unrotate_lowtohigh { |
87
|
16009
|
|
|
16009
|
|
37792
|
my ($aref) = @_; |
88
|
16009
|
|
|
|
|
22178
|
my $rot = 0; |
89
|
16009
|
|
|
|
|
26653
|
foreach my $digit (reverse @$aref) { |
90
|
58866
|
100
|
|
|
|
97264
|
if ($digit) { |
91
|
52553
|
|
|
|
|
70854
|
$digit = ($digit-1-$rot) % 6; # mutate $aref |
92
|
52553
|
|
|
|
|
65607
|
$rot += $digit; |
93
|
52553
|
|
|
|
|
75823
|
$digit++; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub n_to_xy { |
99
|
10690
|
|
|
10690
|
1
|
93824
|
my ($self, $n) = @_; |
100
|
|
|
|
|
|
|
### GosperReplicate n_to_xy(): $n |
101
|
|
|
|
|
|
|
|
102
|
10690
|
50
|
|
|
|
19936
|
if ($n < 0) { |
103
|
0
|
|
|
|
|
0
|
return; |
104
|
|
|
|
|
|
|
} |
105
|
10690
|
50
|
|
|
|
19787
|
if (is_infinite($n)) { |
106
|
0
|
|
|
|
|
0
|
return ($n,$n); |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
{ |
110
|
10690
|
|
|
|
|
17174
|
my $int = int($n); |
|
10690
|
|
|
|
|
14946
|
|
111
|
|
|
|
|
|
|
### $int |
112
|
|
|
|
|
|
|
### $n |
113
|
10690
|
50
|
|
|
|
20125
|
if ($n != $int) { |
114
|
0
|
|
|
|
|
0
|
my ($x1,$y1) = $self->n_to_xy($int); |
115
|
0
|
|
|
|
|
0
|
my ($x2,$y2) = $self->n_to_xy($int+1); |
116
|
0
|
|
|
|
|
0
|
my $frac = $n - $int; # inherit possible BigFloat |
117
|
0
|
|
|
|
|
0
|
my $dx = $x2-$x1; |
118
|
0
|
|
|
|
|
0
|
my $dy = $y2-$y1; |
119
|
0
|
|
|
|
|
0
|
return ($frac*$dx + $x1, $frac*$dy + $y1); |
120
|
|
|
|
|
|
|
} |
121
|
10690
|
|
|
|
|
14728
|
$n = $int; # BigFloat int() gives BigInt, use that |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
10690
|
|
|
|
|
15371
|
my $x = my $y = $n*0; # inherit bigint from $n |
125
|
10690
|
|
|
|
|
15188
|
my $sx = $x + 2; # 2 |
126
|
10690
|
|
|
|
|
14123
|
my $sy = $x; # 0 |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# digit |
129
|
|
|
|
|
|
|
# 3 2 |
130
|
|
|
|
|
|
|
# \ / |
131
|
|
|
|
|
|
|
# 4---0---1 |
132
|
|
|
|
|
|
|
# / \ |
133
|
|
|
|
|
|
|
# 5 6 |
134
|
|
|
|
|
|
|
|
135
|
10690
|
|
|
|
|
20203
|
my @digits = digit_split_lowtohigh($n,7); |
136
|
10690
|
100
|
|
|
|
22979
|
if ($self->{'numbering_type'} eq 'rotate') { |
137
|
7260
|
|
|
|
|
12295
|
_digits_rotate_lowtohigh(\@digits); |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
|
140
|
10690
|
|
|
|
|
17695
|
foreach my $digit (@digits) { |
141
|
|
|
|
|
|
|
### digit: "$digit $x,$y side $sx,$sy" |
142
|
|
|
|
|
|
|
|
143
|
33828
|
100
|
|
|
|
78377
|
if ($digit == 1) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
144
|
|
|
|
|
|
|
### right ... |
145
|
|
|
|
|
|
|
# $x = -$x; # rotate 180 |
146
|
|
|
|
|
|
|
# $y = -$y; |
147
|
5258
|
|
|
|
|
7535
|
$x += $sx; |
148
|
5258
|
|
|
|
|
7174
|
$y += $sy; |
149
|
|
|
|
|
|
|
} elsif ($digit == 2) { |
150
|
|
|
|
|
|
|
### up right ... |
151
|
|
|
|
|
|
|
# ($x,$y) = ((3*$y-$x)/2, # rotate -120 |
152
|
|
|
|
|
|
|
# ($x+$y)/-2); |
153
|
5258
|
|
|
|
|
8992
|
$x += ($sx - 3*$sy)/2; # at +60 |
154
|
5258
|
|
|
|
|
7783
|
$y += ($sx + $sy)/2; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
} elsif ($digit == 3) { |
157
|
|
|
|
|
|
|
### up left ... |
158
|
|
|
|
|
|
|
# ($x,$y) = (($x+3*$y)/2, # -60 |
159
|
|
|
|
|
|
|
# ($y-$x)/2); |
160
|
5258
|
|
|
|
|
8904
|
$x += ($sx + 3*$sy)/-2; # at +120 |
161
|
5258
|
|
|
|
|
7793
|
$y += ($sx - $sy)/2; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
} elsif ($digit == 4) { |
164
|
|
|
|
|
|
|
### left |
165
|
4915
|
|
|
|
|
6969
|
$x -= $sx; # at -180 |
166
|
4915
|
|
|
|
|
6636
|
$y -= $sy; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
} elsif ($digit == 5) { |
169
|
|
|
|
|
|
|
### down left |
170
|
|
|
|
|
|
|
# ($x,$y) = (($x-3*$y)/2, # rotate +60 |
171
|
|
|
|
|
|
|
# ($x+$y)/2); |
172
|
4915
|
|
|
|
|
8501
|
$x += (3*$sy - $sx)/2; # at -120 |
173
|
4915
|
|
|
|
|
7764
|
$y += ($sx + $sy)/-2; |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
} elsif ($digit == 6) { |
176
|
|
|
|
|
|
|
### down right |
177
|
|
|
|
|
|
|
# ($x,$y) = (($x+3*$y)/-2, # rotate +120 |
178
|
|
|
|
|
|
|
# ($x-$y)/2); |
179
|
4915
|
|
|
|
|
8334
|
$x += ($sx + 3*$sy)/2; # at -60 |
180
|
4915
|
|
|
|
|
7698
|
$y += ($sy - $sx)/2; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# 2*(sx,sy) + rot+60(sx,sy) |
184
|
33828
|
|
|
|
|
63602
|
($sx,$sy) = ((5*$sx - 3*$sy) / 2, |
185
|
|
|
|
|
|
|
($sx + 5*$sy) / 2); |
186
|
|
|
|
|
|
|
} |
187
|
10690
|
|
|
|
|
25811
|
return ($x,$y); |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# modulus |
191
|
|
|
|
|
|
|
# 1 3 |
192
|
|
|
|
|
|
|
# \ / |
193
|
|
|
|
|
|
|
# 5---0---2 |
194
|
|
|
|
|
|
|
# / \ |
195
|
|
|
|
|
|
|
# 4 6 |
196
|
|
|
|
|
|
|
# 0 1 2 3 4 5 6 |
197
|
|
|
|
|
|
|
my @modulus_to_x = (0,-1, 2, 1,-1,-2, 1); |
198
|
|
|
|
|
|
|
my @modulus_to_y = (0, 1, 0, 1,-1, 0,-1); |
199
|
|
|
|
|
|
|
my @modulus_to_digit = (0, 3, 1, 2, 5, 4, 6); |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub xy_to_n { |
202
|
15643
|
|
|
15643
|
1
|
79362
|
my ($self, $x, $y) = @_; |
203
|
|
|
|
|
|
|
### GosperReplicate xy_to_n(): "$x, $y" |
204
|
|
|
|
|
|
|
|
205
|
15643
|
|
|
|
|
29580
|
$x = round_nearest($x); |
206
|
15643
|
|
|
|
|
29164
|
$y = round_nearest($y); |
207
|
15643
|
50
|
|
|
|
30693
|
if (($x + $y) % 2) { |
208
|
0
|
|
|
|
|
0
|
return undef; |
209
|
|
|
|
|
|
|
} |
210
|
|
|
|
|
|
|
|
211
|
15643
|
|
|
|
|
25728
|
my $level = _xy_to_level_ceil($x,$y); |
212
|
15643
|
50
|
|
|
|
32831
|
if (is_infinite($level)) { |
213
|
0
|
|
|
|
|
0
|
return $level; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
15643
|
|
|
|
|
28032
|
my $zero = ($x * 0 * $y); # inherit bignum 0 |
217
|
15643
|
|
|
|
|
21130
|
my @n; # digits low to high |
218
|
|
|
|
|
|
|
|
219
|
15643
|
|
100
|
|
|
39394
|
while ($level-- >= 0 && ($x || $y)) { |
|
|
|
100
|
|
|
|
|
220
|
|
|
|
|
|
|
### at: "$x,$y m=".(($x + 2*$y) % 7) |
221
|
|
|
|
|
|
|
|
222
|
57851
|
|
|
|
|
93519
|
my $m = ($x + 2*$y) % 7; |
223
|
57851
|
|
|
|
|
88001
|
push @n, $modulus_to_digit[$m]; |
224
|
57851
|
|
|
|
|
78118
|
$x -= $modulus_to_x[$m]; |
225
|
57851
|
|
|
|
|
76594
|
$y -= $modulus_to_y[$m]; |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
### digit: "to $x,$y" |
228
|
|
|
|
|
|
|
### assert: (3 * $y + 5 * $x) % 14 == 0 |
229
|
|
|
|
|
|
|
### assert: (5 * $y - $x) % 14 == 0 |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
# shrink |
232
|
57851
|
|
|
|
|
207579
|
($x,$y) = ((3*$y + 5*$x) / 14, |
233
|
|
|
|
|
|
|
(5*$y - $x) / 14); |
234
|
|
|
|
|
|
|
} |
235
|
|
|
|
|
|
|
|
236
|
15643
|
50
|
|
|
|
32545
|
if ($self->{'numbering_type'} eq 'rotate') { |
237
|
15643
|
|
|
|
|
29569
|
_digits_unrotate_lowtohigh(\@n); |
238
|
|
|
|
|
|
|
} |
239
|
15643
|
|
|
|
|
36493
|
return digit_join_lowtohigh (\@n, 7, $zero); |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
# not exact |
244
|
|
|
|
|
|
|
sub rect_to_n_range { |
245
|
0
|
|
|
0
|
1
|
0
|
my ($self, $x1,$y1, $x2,$y2) = @_; |
246
|
0
|
|
|
|
|
0
|
$y1 *= sqrt(3); |
247
|
0
|
|
|
|
|
0
|
$y2 *= sqrt(3); |
248
|
0
|
|
|
|
|
0
|
my ($r_lo, $r_hi) = Math::PlanePath::SacksSpiral::_rect_to_radius_range |
249
|
|
|
|
|
|
|
($x1,$y1, $x2,$y2); |
250
|
0
|
|
|
|
|
0
|
$r_hi *= 2; |
251
|
0
|
|
|
|
|
0
|
my $level_plus_1 = ceil( log(max(1,$r_hi/4)) / log(sqrt(7)) ) + 2; |
252
|
0
|
|
|
|
|
0
|
return (0, 7**$level_plus_1 - 1); |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
sub _xy_to_level_ceil { |
256
|
15643
|
|
|
15643
|
|
24649
|
my ($x,$y) = @_; |
257
|
15643
|
|
|
|
|
32144
|
my $r = hypot($x,$y); |
258
|
15643
|
|
|
|
|
23603
|
$r *= 2; |
259
|
15643
|
|
|
|
|
52523
|
return ceil( log(max(1,$r/4)) / log(sqrt(7)) ) + 1; |
260
|
|
|
|
|
|
|
} |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
263
|
|
|
|
|
|
|
# levels |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
sub level_to_n_range { |
266
|
5
|
|
|
5
|
1
|
140
|
my ($self, $level) = @_; |
267
|
5
|
|
|
|
|
14
|
return (0, 7**$level - 1); |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
sub n_to_level { |
270
|
0
|
|
|
0
|
1
|
|
my ($self, $n) = @_; |
271
|
0
|
0
|
|
|
|
|
if ($n < 0) { return undef; } |
|
0
|
|
|
|
|
|
|
272
|
0
|
0
|
|
|
|
|
if (is_infinite($n)) { return $n; } |
|
0
|
|
|
|
|
|
|
273
|
0
|
|
|
|
|
|
$n = round_nearest($n); |
274
|
0
|
|
|
|
|
|
my ($pow, $exp) = round_up_pow ($n+1, 7); |
275
|
0
|
|
|
|
|
|
return $exp; |
276
|
|
|
|
|
|
|
} |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
280
|
|
|
|
|
|
|
1; |
281
|
|
|
|
|
|
|
__END__ |