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=GreekKeySpiral --lines --scale=25 |
20
|
|
|
|
|
|
|
# http://gwydir.demon.co.uk/jo/greekkey/corners.htm |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
package Math::PlanePath::GreekKeySpiral; |
24
|
1
|
|
|
1
|
|
1410
|
use 5.004; |
|
1
|
|
|
|
|
5
|
|
25
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
26
|
|
|
|
|
|
|
|
27
|
1
|
|
|
1
|
|
5
|
use vars '$VERSION', '@ISA'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
57
|
|
28
|
|
|
|
|
|
|
$VERSION = 127; |
29
|
1
|
|
|
1
|
|
738
|
use Math::PlanePath; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
30
|
1
|
|
|
1
|
|
488
|
use Math::PlanePath::Base::NSEW; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
31
|
|
|
|
|
|
|
@ISA = ('Math::PlanePath::Base::NSEW', |
32
|
|
|
|
|
|
|
'Math::PlanePath'); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
use Math::PlanePath::Base::Generic |
35
|
1
|
|
|
|
|
70
|
'round_nearest', |
36
|
1
|
|
|
1
|
|
6
|
'floor'; |
|
1
|
|
|
|
|
2
|
|
37
|
|
|
|
|
|
|
*_divrem = \&Math::PlanePath::_divrem; |
38
|
|
|
|
|
|
|
*_divrem_mutate = \&Math::PlanePath::_divrem_mutate; |
39
|
|
|
|
|
|
|
*_sqrtint = \&Math::PlanePath::_sqrtint; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# uncomment this to run the ### lines |
42
|
|
|
|
|
|
|
# use Smart::Comments; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
|
45
|
1
|
|
|
1
|
|
6
|
use constant xy_is_visited => 1; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
60
|
|
46
|
1
|
|
|
|
|
1477
|
use constant parameter_info_array => |
47
|
|
|
|
|
|
|
[ { name => 'turns', |
48
|
|
|
|
|
|
|
share_key => 'turns_2', |
49
|
|
|
|
|
|
|
display => 'Turns', |
50
|
|
|
|
|
|
|
type => 'integer', |
51
|
|
|
|
|
|
|
minimum => 0, |
52
|
|
|
|
|
|
|
default => 2, |
53
|
|
|
|
|
|
|
width => 2, |
54
|
|
|
|
|
|
|
}, |
55
|
1
|
|
|
1
|
|
5
|
]; |
|
1
|
|
|
|
|
2
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub x_negative_at_n { |
58
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
59
|
0
|
|
|
|
|
0
|
return $self->n_start + 4*($self->{'turns'}+1)**2; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
sub y_negative_at_n { |
62
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
63
|
0
|
|
|
|
|
0
|
return $self->n_start + 6*($self->{'turns'}+1)**2; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# 17-- 18--19--20--21 |
67
|
|
|
|
|
|
|
# | |
68
|
|
|
|
|
|
|
# 16 3t-2 -- 8 -- 2t |
69
|
|
|
|
|
|
|
# | | | |
70
|
|
|
|
|
|
|
# 15 4t-5 ---11 6 |
71
|
|
|
|
|
|
|
# | | | |
72
|
|
|
|
|
|
|
# 14-- 13-----12 5 |
73
|
|
|
|
|
|
|
# | |
74
|
|
|
|
|
|
|
# 1---- 2----- 3-- t |
75
|
|
|
|
|
|
|
# |
76
|
|
|
|
|
|
|
sub _UNDOCUMENTED__dxdy_list_at_n { |
77
|
0
|
|
|
0
|
|
0
|
my ($self) = @_; |
78
|
0
|
|
|
|
|
0
|
my $turns = $self->{'turns'}; |
79
|
0
|
0
|
|
|
|
0
|
return $self->n_start + ($turns == 0 ? 4 # turns=0 |
|
|
0
|
|
|
|
|
|
80
|
|
|
|
|
|
|
: $turns <= 2 ? 6 # turns=1,2 |
81
|
|
|
|
|
|
|
: 3*$turns - 4); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub turn_any_right { |
85
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
86
|
0
|
|
|
|
|
0
|
return ($self->{'turns'} != 0); # SquareSpiral is left or straight only |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
sub _UNDOCUMENTED__turn_any_right_at_n { |
89
|
0
|
|
|
0
|
|
0
|
my ($self) = @_; |
90
|
|
|
|
|
|
|
# turns=1 2,4,7,11,22,29 |
91
|
|
|
|
|
|
|
return ($self->{'turns'} == 0 ? undef # SquareSpiral left or straight only |
92
|
0
|
0
|
|
|
|
0
|
: $self->n_start + $self->{'midpoint'}-1); |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
sub _UNDOCUMENTED__turn_any_left_at_n { |
95
|
0
|
|
|
0
|
|
0
|
my ($self) = @_; |
96
|
0
|
|
|
|
|
0
|
my $turns = $self->{'turns'}; |
97
|
|
|
|
|
|
|
# turns=1 2,4,7,11,22,29 |
98
|
0
|
0
|
|
|
|
0
|
return $self->n_start + ($turns==0 ? 1 |
|
|
0
|
|
|
|
|
|
99
|
|
|
|
|
|
|
: $turns==1 ? 3 |
100
|
|
|
|
|
|
|
: $turns-1); |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# turns=1 |
107
|
|
|
|
|
|
|
# 2---3 |
108
|
|
|
|
|
|
|
# | | |
109
|
|
|
|
|
|
|
# 0---1 4 |
110
|
|
|
|
|
|
|
# |
111
|
|
|
|
|
|
|
# turns=2 | |
112
|
|
|
|
|
|
|
# 5---6---7 18 15--14 |
113
|
|
|
|
|
|
|
# | | | | | |
114
|
|
|
|
|
|
|
# 4---3 8 17--16 13 x=1,y=1 |
115
|
|
|
|
|
|
|
# | | | |
116
|
|
|
|
|
|
|
# 0---1---2 9 10--11--12 |
117
|
|
|
|
|
|
|
# |
118
|
|
|
|
|
|
|
# turns=3 |
119
|
|
|
|
|
|
|
# 10--11--12--13 |
120
|
|
|
|
|
|
|
# | | |
121
|
|
|
|
|
|
|
# 9 6---5 14 x=2,y=1 |
122
|
|
|
|
|
|
|
# | | | | |
123
|
|
|
|
|
|
|
# 8---7 4 15 |
124
|
|
|
|
|
|
|
# | | |
125
|
|
|
|
|
|
|
# 0---1---2---3 16 |
126
|
|
|
|
|
|
|
# |
127
|
|
|
|
|
|
|
# turns=4 |
128
|
|
|
|
|
|
|
# 17--18--19--20--21 50 37--36--35--34 |
129
|
|
|
|
|
|
|
# | | | | | 3,3,2,1,1,1,2,3,4,down4 |
130
|
|
|
|
|
|
|
# 16 9---8---7 22 49 38 41--42 33 |
131
|
|
|
|
|
|
|
# | | | | | | | | | |
132
|
|
|
|
|
|
|
# 15 10--11 6 23 48 39--40 43 32 x=3,y=2 |
133
|
|
|
|
|
|
|
# | | | | | | | |
134
|
|
|
|
|
|
|
# 14--13--12 5 24 47--46--45--44 31 |
135
|
|
|
|
|
|
|
# | | | |
136
|
|
|
|
|
|
|
# 0---1---2---3---4 25--26--27--28--29--30 5,4,3,2,1,1,1,2,3,up3 |
137
|
|
|
|
|
|
|
# |
138
|
|
|
|
|
|
|
# turns=5 |
139
|
|
|
|
|
|
|
# 26--27--28--29--30--31 |
140
|
|
|
|
|
|
|
# | | 4,4,3,2,1,1,1,2,3,4,5,5 |
141
|
|
|
|
|
|
|
# 25 12--11--10---9 32 |
142
|
|
|
|
|
|
|
# | | | | |
143
|
|
|
|
|
|
|
# 24 13 16--17 8 33 5,4,3,2,1,1,1,2,3,4,5,rem |
144
|
|
|
|
|
|
|
# | | | | | | | |
145
|
|
|
|
|
|
|
# 35 23 14--15 18 7 34 |
146
|
|
|
|
|
|
|
# | | | | | x=3,y=3 |
147
|
|
|
|
|
|
|
# 36 22--21--20--19 6 35 |
148
|
|
|
|
|
|
|
# | | |
149
|
|
|
|
|
|
|
# 0---1---2---3---4---5 36- |
150
|
|
|
|
|
|
|
# |
151
|
|
|
|
|
|
|
# turns=6 |
152
|
|
|
|
|
|
|
# 37--38--39--40--41--42--43 |
153
|
|
|
|
|
|
|
# | | |
154
|
|
|
|
|
|
|
# 36 15--14--13--12--11 44 x=3,y=3 |
155
|
|
|
|
|
|
|
# | | | | |
156
|
|
|
|
|
|
|
# 35 16 23--24--25 10 45 |
157
|
|
|
|
|
|
|
# | | | | | | |
158
|
|
|
|
|
|
|
# 34 17 22--21 26 9 46 6,5,4,3,2,1,1,1,2,3,4,5,rem |
159
|
|
|
|
|
|
|
# | | | | | | |
160
|
|
|
|
|
|
|
# 33 18--19--20 27 8 47 |
161
|
|
|
|
|
|
|
# | | | | |
162
|
|
|
|
|
|
|
# 32--31--30--29--28 7 48 |
163
|
|
|
|
|
|
|
# | | |
164
|
|
|
|
|
|
|
# 0---1---2---3---4---5---6 49- |
165
|
|
|
|
|
|
|
# |
166
|
|
|
|
|
|
|
# turns=7 |
167
|
|
|
|
|
|
|
# 50--51--52--53--54--55--56--57 |
168
|
|
|
|
|
|
|
# | | |
169
|
|
|
|
|
|
|
# 49 18--17--16--15--14--13 58 |
170
|
|
|
|
|
|
|
# | | | | |
171
|
|
|
|
|
|
|
# 48 19 32--33--34--35 12 59 x=4,y=3 |
172
|
|
|
|
|
|
|
# | | | | | | |
173
|
|
|
|
|
|
|
# 47 20 31 28--27 36 11 60 |
174
|
|
|
|
|
|
|
# | | | | | | | | |
175
|
|
|
|
|
|
|
# 46 21 30--29 26 37 10 61 6,5,4,3,2,1,1,1,2,3,4,5,rem |
176
|
|
|
|
|
|
|
# | | | | | | |
177
|
|
|
|
|
|
|
# 45 22--23--24--25 38 9 62 |
178
|
|
|
|
|
|
|
# | | | | |
179
|
|
|
|
|
|
|
# 44--43--42--41--40--39 8 63 |
180
|
|
|
|
|
|
|
# | | |
181
|
|
|
|
|
|
|
# 0---1---2---3---4---5---6---7 64 |
182
|
|
|
|
|
|
|
# |
183
|
|
|
|
|
|
|
# turns=8 x=5,y=4 |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# centre |
187
|
|
|
|
|
|
|
# 2 1 1 |
188
|
|
|
|
|
|
|
# 3 2 1 |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# 4 3 2 |
191
|
|
|
|
|
|
|
# 5 3 3 |
192
|
|
|
|
|
|
|
# 6 3 3 |
193
|
|
|
|
|
|
|
# 7 4 3 |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
# 8 5 4 |
196
|
|
|
|
|
|
|
# 9 5 5 |
197
|
|
|
|
|
|
|
# 10 5 5 |
198
|
|
|
|
|
|
|
# 11 6 5 |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
# 12 7 6 |
201
|
|
|
|
|
|
|
# 13 7 7 |
202
|
|
|
|
|
|
|
# 14 7 7 |
203
|
|
|
|
|
|
|
# 15 8 7 |
204
|
|
|
|
|
|
|
# |
205
|
|
|
|
|
|
|
# turns 2, 3, 4, 5 |
206
|
|
|
|
|
|
|
# midp 4 6, 10, 15, 21 N = (1/2 d^2 + 1/2 d) |
207
|
|
|
|
|
|
|
# |
208
|
|
|
|
|
|
|
# 63, 189, 387, 657 |
209
|
|
|
|
|
|
|
# 9*7 9*21, 9*43, 9*73 |
210
|
|
|
|
|
|
|
# |
211
|
|
|
|
|
|
|
# 82 226 442 |
212
|
|
|
|
|
|
|
# 9*9+1 9*25+1 9*49+1 |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
sub new { |
215
|
26
|
|
|
26
|
1
|
16506
|
my $self = shift->SUPER::new (@_); |
216
|
|
|
|
|
|
|
|
217
|
26
|
|
|
|
|
87
|
my $turns = $self->{'turns'}; |
218
|
26
|
100
|
|
|
|
118
|
if (! defined $turns) { |
|
|
50
|
|
|
|
|
|
219
|
2
|
|
|
|
|
4
|
$turns = 2; |
220
|
|
|
|
|
|
|
} elsif ($turns < 0) { |
221
|
|
|
|
|
|
|
} |
222
|
26
|
|
|
|
|
65
|
$self->{'turns'} = $turns; |
223
|
26
|
|
|
|
|
47
|
my $t1 = $turns + 1; |
224
|
|
|
|
|
|
|
|
225
|
26
|
50
|
|
|
|
96
|
if (! defined $self->{'n_start'}) { |
226
|
26
|
|
|
|
|
121
|
$self->{'n_start'} = $self->default_n_start; |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
26
|
|
|
|
|
107
|
$self->{'centre_x'} = int($t1/2) + (($turns%4)==0); |
230
|
26
|
|
|
|
|
67
|
$self->{'centre_y'} = int($turns/2) + (($turns%4)==1); |
231
|
|
|
|
|
|
|
|
232
|
26
|
|
|
|
|
80
|
$self->{'midpoint'} = $turns*$t1/2 + 1; |
233
|
26
|
|
|
|
|
51
|
$self->{'side'} = $t1; |
234
|
26
|
|
|
|
|
57
|
$self->{'squared'} = $t1*$t1; |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
### turns : $self->{'turns'} |
237
|
|
|
|
|
|
|
### midpoint: $self->{'midpoint'} |
238
|
|
|
|
|
|
|
### side : $self->{'side'} |
239
|
|
|
|
|
|
|
### squared : $self->{'squared'} |
240
|
|
|
|
|
|
|
|
241
|
26
|
|
|
|
|
59
|
return $self; |
242
|
|
|
|
|
|
|
} |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
sub n_to_xy { |
245
|
75807
|
|
|
75807
|
1
|
361750
|
my ($self, $n) = @_; |
246
|
|
|
|
|
|
|
#### GreekKeySpiral n_to_xy: $n |
247
|
|
|
|
|
|
|
|
248
|
75807
|
|
|
|
|
114977
|
$n = $n - $self->{'n_start'}; |
249
|
|
|
|
|
|
|
### n zero based: $n |
250
|
75807
|
50
|
|
|
|
135429
|
if ($n < 0) { return; } |
|
0
|
|
|
|
|
0
|
|
251
|
|
|
|
|
|
|
|
252
|
75807
|
|
|
|
|
114422
|
my $turns = $self->{'turns'}; |
253
|
75807
|
|
|
|
|
110114
|
my $squared = $self->{'squared'}; |
254
|
75807
|
|
|
|
|
106984
|
my $side = $turns + 1; |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
### sqrt of: ($n-1) / $squared |
257
|
|
|
|
|
|
|
|
258
|
75807
|
|
|
|
|
152481
|
my $d = _sqrtint($n / $squared); |
259
|
75807
|
|
|
|
|
133773
|
$n -= $squared*$d*$d - 1; |
260
|
75807
|
|
|
|
|
120900
|
my $dhalf = int($d/2); |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
### $d |
263
|
|
|
|
|
|
|
### $dhalf |
264
|
|
|
|
|
|
|
### n remainder: $n |
265
|
|
|
|
|
|
|
|
266
|
75807
|
|
|
|
|
108322
|
my ($x,$y); |
267
|
75807
|
|
|
|
|
101939
|
my $square_rot = 0; |
268
|
75807
|
|
|
|
|
97149
|
my $frac; |
269
|
75807
|
|
|
|
|
96753
|
{ my $int = int($n); |
|
75807
|
|
|
|
|
106203
|
|
270
|
75807
|
|
|
|
|
108888
|
$frac = $n - int($n); |
271
|
75807
|
|
|
|
|
115303
|
$n = $int; |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
### $frac |
274
|
|
|
|
|
|
|
### $n |
275
|
|
|
|
|
|
|
|
276
|
75807
|
100
|
|
|
|
125767
|
if ($d % 2) { |
277
|
|
|
|
|
|
|
### odd d, right and top ... |
278
|
35654
|
100
|
|
|
|
61293
|
if ($n >= $squared*($d+1)) { |
279
|
|
|
|
|
|
|
### top ... |
280
|
15217
|
|
|
|
|
21761
|
$n -= $squared*2*$d; |
281
|
15217
|
|
|
|
|
27302
|
(my $q, $n) = _divrem ($n, $squared); |
282
|
15217
|
|
|
|
|
25240
|
$x = (-$dhalf-$q)*$side + 1; |
283
|
15217
|
|
|
|
|
20608
|
$y = ($dhalf+1)*$side; |
284
|
15217
|
|
|
|
|
20755
|
$square_rot = 2; |
285
|
|
|
|
|
|
|
} else { |
286
|
|
|
|
|
|
|
### right ... |
287
|
20437
|
|
|
|
|
40260
|
(my $q, $n) = _divrem ($n-$turns-1 + $squared, $squared); |
288
|
20437
|
|
|
|
|
32178
|
$x = ($dhalf+1)*$side; |
289
|
20437
|
|
|
|
|
29376
|
$y = ($q-$dhalf-1)*$side; |
290
|
20437
|
|
|
|
|
27843
|
$square_rot = 1; |
291
|
|
|
|
|
|
|
} |
292
|
|
|
|
|
|
|
} else { |
293
|
|
|
|
|
|
|
### even d, left and bottom ... |
294
|
40153
|
100
|
100
|
|
|
113048
|
if ($d == 0 || $n >= $squared*($d+1)) { |
295
|
|
|
|
|
|
|
### bottom ... |
296
|
18725
|
|
|
|
|
27659
|
$n -= $squared*2*$d; |
297
|
18725
|
|
|
|
|
33883
|
(my $q, $n) = _divrem ($n, $squared); |
298
|
18725
|
|
|
|
|
31498
|
$x = ($dhalf+$q)*$side-1; |
299
|
18725
|
|
|
|
|
25404
|
$y = -($dhalf)*$side; |
300
|
18725
|
|
|
|
|
26165
|
$square_rot = 0; |
301
|
|
|
|
|
|
|
} else { |
302
|
|
|
|
|
|
|
### left ... |
303
|
21428
|
|
|
|
|
44810
|
(my $q, $n) = _divrem ($n-$turns-1 + $squared, $squared); |
304
|
21428
|
|
|
|
|
33965
|
$x = -($dhalf)*$side; |
305
|
21428
|
|
|
|
|
30449
|
$y = -($q-$dhalf-1)*$side; |
306
|
21428
|
|
|
|
|
31923
|
$square_rot = 3; |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
} |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
### assert: ! ($n < 0) |
311
|
|
|
|
|
|
|
### assert: ! ($n >= $squared) |
312
|
|
|
|
|
|
|
|
313
|
75807
|
|
|
|
|
103657
|
my $rot = $turns; |
314
|
75807
|
|
|
|
|
98371
|
my $kx = 0; |
315
|
75807
|
|
|
|
|
98663
|
my $ky = 0; |
316
|
75807
|
|
|
|
|
97652
|
my $before; |
317
|
|
|
|
|
|
|
### n-midpoint: $n - $self->{'midpoint'} |
318
|
|
|
|
|
|
|
|
319
|
75807
|
100
|
|
|
|
154579
|
if (($n -= $self->{'midpoint'}) >= 0) { |
|
|
100
|
|
|
|
|
|
320
|
|
|
|
|
|
|
### after middle ... |
321
|
|
|
|
|
|
|
} elsif ($n += 1) { |
322
|
|
|
|
|
|
|
### before middle ... |
323
|
33092
|
|
|
|
|
45324
|
$n = -$n; |
324
|
33092
|
100
|
|
|
|
48893
|
if ($frac) { |
325
|
|
|
|
|
|
|
### fraction ... |
326
|
3510
|
|
|
|
|
4576
|
$frac = 1-$frac; |
327
|
3510
|
|
|
|
|
4672
|
$n -= 1; |
328
|
|
|
|
|
|
|
} else { |
329
|
|
|
|
|
|
|
### integer ... |
330
|
29582
|
|
|
|
|
38424
|
$n -= 0; |
331
|
|
|
|
|
|
|
} |
332
|
33092
|
|
|
|
|
42967
|
$rot += 2; |
333
|
33092
|
|
|
|
|
44717
|
$before = 1; |
334
|
|
|
|
|
|
|
} else { |
335
|
|
|
|
|
|
|
### centre segment ... |
336
|
4200
|
|
|
|
|
5479
|
$rot += 1; |
337
|
4200
|
|
|
|
|
5464
|
$before = 1; |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
### key n: $n |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
# d: [ 0, 1, 2 ] |
342
|
|
|
|
|
|
|
# n: [ 0, 3, 10 ] |
343
|
|
|
|
|
|
|
# d = -1/4 + sqrt(1/2 * $n + 1/16) |
344
|
|
|
|
|
|
|
# = (-1 + sqrt(8*$n + 1)) / 4 |
345
|
|
|
|
|
|
|
# N = (2*$d + 1)*$d |
346
|
|
|
|
|
|
|
# rel = (2*$d + 1)*$d + 2*$d+1 |
347
|
|
|
|
|
|
|
# = (2*$d + 3)*$d + 1 |
348
|
|
|
|
|
|
|
# |
349
|
75807
|
|
|
|
|
147377
|
$d = int( (_sqrtint(8*$n+1) - 1)/4 ); |
350
|
75807
|
|
|
|
|
122680
|
$n -= (2*$d+3)*$d + 1; |
351
|
|
|
|
|
|
|
### $d |
352
|
|
|
|
|
|
|
### key signed rem: $n |
353
|
|
|
|
|
|
|
|
354
|
75807
|
100
|
|
|
|
125980
|
if ($n < 0) { |
355
|
|
|
|
|
|
|
### key vertical ... |
356
|
40030
|
|
|
|
|
52364
|
$kx += $d; |
357
|
40030
|
|
|
|
|
63566
|
$ky = -$frac-$n-$d - 1 + $ky; |
358
|
40030
|
100
|
|
|
|
65310
|
if ($d % 2) { |
359
|
|
|
|
|
|
|
### key right ... |
360
|
17692
|
|
|
|
|
22682
|
$rot += 2; |
361
|
17692
|
|
|
|
|
23489
|
$kx += 1; |
362
|
|
|
|
|
|
|
} else { |
363
|
|
|
|
|
|
|
} |
364
|
|
|
|
|
|
|
} else { |
365
|
|
|
|
|
|
|
### key horizontal ... |
366
|
35777
|
|
|
|
|
52936
|
$kx = $frac+$n-$d + $kx; |
367
|
35777
|
|
|
|
|
47601
|
$ky += $d + 1; |
368
|
35777
|
|
|
|
|
45738
|
$rot += 2; |
369
|
35777
|
100
|
|
|
|
59497
|
if ($d % 2) { |
370
|
|
|
|
|
|
|
### key bottom ... |
371
|
18174
|
|
|
|
|
23104
|
$rot += 2; |
372
|
18174
|
|
|
|
|
24304
|
$kx += -1; |
373
|
|
|
|
|
|
|
} else { |
374
|
|
|
|
|
|
|
} |
375
|
|
|
|
|
|
|
} |
376
|
|
|
|
|
|
|
### kxy raw: "$kx, $ky" |
377
|
|
|
|
|
|
|
|
378
|
75807
|
100
|
|
|
|
132214
|
if ($rot & 2) { |
379
|
35963
|
|
|
|
|
50001
|
$kx = -$kx; |
380
|
35963
|
|
|
|
|
47680
|
$ky = -$ky; |
381
|
|
|
|
|
|
|
} |
382
|
75807
|
100
|
|
|
|
125115
|
if ($rot & 1) { |
383
|
14114
|
|
|
|
|
25559
|
($kx,$ky) = (-$ky,$kx); |
384
|
|
|
|
|
|
|
} |
385
|
|
|
|
|
|
|
### kxy rotated: "$kx,$ky" |
386
|
|
|
|
|
|
|
|
387
|
75807
|
100
|
|
|
|
125710
|
if ($before) { |
388
|
37292
|
100
|
|
|
|
69273
|
if (($turns % 4) == 0) { |
389
|
30323
|
|
|
|
|
40912
|
$kx -= 1; |
390
|
|
|
|
|
|
|
} |
391
|
37292
|
100
|
|
|
|
63364
|
if (($turns % 4) == 1) { |
392
|
3935
|
|
|
|
|
5460
|
$ky -= 1; |
393
|
|
|
|
|
|
|
} |
394
|
37292
|
100
|
|
|
|
61315
|
if (($turns % 4) == 2) { |
395
|
1388
|
|
|
|
|
1900
|
$kx += 1; |
396
|
|
|
|
|
|
|
} |
397
|
37292
|
100
|
|
|
|
64051
|
if (($turns % 4) == 3) { |
398
|
1646
|
|
|
|
|
2254
|
$ky += 1; |
399
|
|
|
|
|
|
|
} |
400
|
|
|
|
|
|
|
} |
401
|
|
|
|
|
|
|
|
402
|
75807
|
|
|
|
|
114311
|
$kx += $self->{'centre_x'}; |
403
|
75807
|
|
|
|
|
109684
|
$ky += $self->{'centre_y'}; |
404
|
|
|
|
|
|
|
|
405
|
75807
|
100
|
|
|
|
129861
|
if ($square_rot & 2) { |
406
|
36645
|
|
|
|
|
50751
|
$kx = $turns-$kx; |
407
|
36645
|
|
|
|
|
48478
|
$ky = $turns-$ky; |
408
|
|
|
|
|
|
|
} |
409
|
75807
|
100
|
|
|
|
122457
|
if ($square_rot & 1) { |
410
|
41865
|
|
|
|
|
77643
|
($kx,$ky) = ($turns-$ky,$kx); |
411
|
|
|
|
|
|
|
} |
412
|
|
|
|
|
|
|
|
413
|
|
|
|
|
|
|
# kx,ky first to inherit BigRat etc from $frac |
414
|
75807
|
|
|
|
|
198453
|
return ($kx + $x, |
415
|
|
|
|
|
|
|
$ky + $y); |
416
|
|
|
|
|
|
|
} |
417
|
|
|
|
|
|
|
|
418
|
|
|
|
|
|
|
|
419
|
|
|
|
|
|
|
# t+(t-1)+(t-2)+(t-3) = 4t-6 |
420
|
|
|
|
|
|
|
|
421
|
|
|
|
|
|
|
# y=0 0 |
422
|
|
|
|
|
|
|
# y=2 0+1+2+3 total 6 |
423
|
|
|
|
|
|
|
# y=4 4+5+6+7 total 28 |
424
|
|
|
|
|
|
|
# (2 d^2 - d) |
425
|
|
|
|
|
|
|
# N=4*t*y/2 - (2y-1)*y |
426
|
|
|
|
|
|
|
# =(2t - 2y + 1)*y |
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
# x=1 0+1+2 total 3 |
429
|
|
|
|
|
|
|
# x=3 3+4+5+6 total 21 |
430
|
|
|
|
|
|
|
# x=5 7+8+9+10 total 55 |
431
|
|
|
|
|
|
|
# (2 d^2 + d) |
432
|
|
|
|
|
|
|
# N = 4*t*(x-1)/2 + 3t-3 - (2x+1)*x |
433
|
|
|
|
|
|
|
# = 2*t*(x-1) + 3t-3 - (2x+1)*x |
434
|
|
|
|
|
|
|
# = 2tx-2t + 3t-3 - (2x+1)*x |
435
|
|
|
|
|
|
|
# = (2t-2x-1)x - 2t + 3t-3 |
436
|
|
|
|
|
|
|
# = (2t-2x-1)x + t-3 |
437
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
# y=0 squared-t-t total 0 |
439
|
|
|
|
|
|
|
# y=2 - (t-1)-(t-2)-(t-3)-(t-4) total 10 |
440
|
|
|
|
|
|
|
# y=4 - 5+6+7+8 total 36 |
441
|
|
|
|
|
|
|
# (2 d^2 + d) |
442
|
|
|
|
|
|
|
# N = squared - 4*t*y/2 - 2t - (2y+1)*y +(x-y) |
443
|
|
|
|
|
|
|
# = squared - (2t+2y+1)*y - 2t + x |
444
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
sub xy_to_n { |
446
|
59697
|
|
|
59697
|
1
|
285516
|
my ($self, $x, $y) = @_; |
447
|
|
|
|
|
|
|
|
448
|
59697
|
|
|
|
|
113758
|
$x = round_nearest ($x); |
449
|
59697
|
|
|
|
|
108659
|
$y = round_nearest ($y); |
450
|
|
|
|
|
|
|
### xy_to_n: "x=$x, y=$y" |
451
|
|
|
|
|
|
|
|
452
|
59697
|
|
|
|
|
100482
|
my $turns = $self->{'turns'}; |
453
|
59697
|
|
|
|
|
85002
|
my $side = $turns + 1; |
454
|
59697
|
|
|
|
|
87301
|
my $squared = $self->{'squared'}; |
455
|
|
|
|
|
|
|
|
456
|
59697
|
|
|
|
|
123062
|
my $xs = floor($x/$side); |
457
|
59697
|
|
|
|
|
124204
|
my $ys = floor($y/$side); |
458
|
59697
|
|
|
|
|
93162
|
$x %= $side; |
459
|
59697
|
|
|
|
|
79319
|
$y %= $side; |
460
|
59697
|
|
|
|
|
75488
|
my $n; |
461
|
59697
|
100
|
|
|
|
97330
|
if ($xs > -$ys) { |
462
|
|
|
|
|
|
|
### top or right |
463
|
28382
|
100
|
|
|
|
44012
|
if ($xs >= $ys) { |
464
|
|
|
|
|
|
|
### right going upwards |
465
|
16021
|
|
|
|
|
24661
|
$n = $squared*((4*$xs - 3)*$xs + $ys); |
466
|
16021
|
|
|
|
|
26494
|
($x,$y) = ($y,$turns-$x); # rotate -90 |
467
|
16021
|
100
|
|
|
|
25947
|
if ($x == 0) { |
468
|
1777
|
|
|
|
|
2186
|
$x = $turns; |
469
|
1777
|
|
|
|
|
2501
|
$n -= $side*$turns; # +$side modulo |
470
|
|
|
|
|
|
|
} else { |
471
|
14244
|
|
|
|
|
18753
|
$x -= 1; |
472
|
14244
|
|
|
|
|
19274
|
$n += $side; |
473
|
|
|
|
|
|
|
} |
474
|
|
|
|
|
|
|
} else { |
475
|
|
|
|
|
|
|
### top going leftwards |
476
|
12361
|
|
|
|
|
18682
|
$n = $squared*((4*$ys - 1)*$ys - $xs); |
477
|
12361
|
|
|
|
|
16007
|
$x = $turns-$x; # rotate 180 |
478
|
12361
|
|
|
|
|
16216
|
$y = $turns-$y; |
479
|
|
|
|
|
|
|
} |
480
|
|
|
|
|
|
|
} else { |
481
|
|
|
|
|
|
|
### bottom or left |
482
|
31315
|
100
|
66
|
|
|
82753
|
if ($xs > $ys || ($xs == 0 && $ys == 0)) { |
|
|
|
100
|
|
|
|
|
483
|
|
|
|
|
|
|
### bottom going rightwards: "$xs,$ys" |
484
|
13874
|
|
|
|
|
23635
|
$n = $squared*((4*$ys - 3)*$ys + $xs); |
485
|
|
|
|
|
|
|
} else { |
486
|
|
|
|
|
|
|
### left going downwards |
487
|
17441
|
|
|
|
|
28828
|
$n = $squared*((4*$xs - 1)*$xs - $ys); |
488
|
17441
|
|
|
|
|
30879
|
($x,$y) = ($turns-$y,$x); # rotate +90 |
489
|
17441
|
100
|
|
|
|
30664
|
if ($x == 0) { |
490
|
1159
|
|
|
|
|
1562
|
$x = $turns; |
491
|
1159
|
|
|
|
|
1640
|
$n -= $side*$turns; # +$side modulo |
492
|
|
|
|
|
|
|
} else { |
493
|
16282
|
|
|
|
|
21870
|
$x -= 1; |
494
|
16282
|
|
|
|
|
23092
|
$n += $side; |
495
|
|
|
|
|
|
|
} |
496
|
|
|
|
|
|
|
} |
497
|
|
|
|
|
|
|
} |
498
|
|
|
|
|
|
|
|
499
|
59697
|
100
|
|
|
|
96260
|
if ($x + $y >= $turns) { |
500
|
|
|
|
|
|
|
### key top or right ... |
501
|
32516
|
100
|
|
|
|
50849
|
if ($x > $y) { |
502
|
|
|
|
|
|
|
### key right ... |
503
|
15113
|
|
|
|
|
19968
|
$x = $turns-$x; |
504
|
15113
|
100
|
|
|
|
24742
|
if ($x % 2) { |
505
|
|
|
|
|
|
|
### forward ... |
506
|
6321
|
|
|
|
|
10365
|
$n += (2*$turns-2*$x+2)*$x + $y - $turns; |
507
|
|
|
|
|
|
|
} else { |
508
|
|
|
|
|
|
|
### backward ... |
509
|
8792
|
|
|
|
|
14803
|
$n += $squared - (2*$turns-2*$x+2)*$x - $y; |
510
|
|
|
|
|
|
|
} |
511
|
|
|
|
|
|
|
} else { |
512
|
|
|
|
|
|
|
### key top ... |
513
|
17403
|
|
|
|
|
22790
|
$y = $turns-$y; |
514
|
17403
|
100
|
|
|
|
26949
|
if ($y % 2) { |
515
|
|
|
|
|
|
|
### backward ... |
516
|
7192
|
|
|
|
|
11613
|
$n += (2*$turns-2*$y)*$y + $turns-$x; |
517
|
|
|
|
|
|
|
} else { |
518
|
|
|
|
|
|
|
### forward ... |
519
|
10211
|
|
|
|
|
17650
|
$n += $squared - (2*$turns - 2*$y)*$y - 2*$turns + $x; |
520
|
|
|
|
|
|
|
} |
521
|
|
|
|
|
|
|
} |
522
|
|
|
|
|
|
|
} else { |
523
|
|
|
|
|
|
|
### key bottom or left ... |
524
|
27181
|
100
|
|
|
|
41948
|
if ($x >= $y) { |
525
|
|
|
|
|
|
|
### key bottom ... |
526
|
14516
|
100
|
|
|
|
22687
|
if ($y % 2) { |
527
|
|
|
|
|
|
|
### backward ... |
528
|
6303
|
|
|
|
|
11415
|
$n += $squared - (2*$turns - 2*$y)*$y - $turns - $x - 1; |
529
|
|
|
|
|
|
|
} else { |
530
|
|
|
|
|
|
|
### forward ... |
531
|
8213
|
|
|
|
|
13172
|
$n += (2*$turns-2*$y)*$y + $x + 1; |
532
|
|
|
|
|
|
|
} |
533
|
|
|
|
|
|
|
} else { |
534
|
|
|
|
|
|
|
### key left ... |
535
|
12665
|
100
|
|
|
|
19958
|
if ($x % 2) { |
536
|
|
|
|
|
|
|
### forward ... |
537
|
5480
|
|
|
|
|
9271
|
$n += (2*$turns-2*$x-2)*$x + 2*$turns - $y; |
538
|
|
|
|
|
|
|
} else { |
539
|
|
|
|
|
|
|
### backward ... |
540
|
7185
|
|
|
|
|
11866
|
$n += $squared - (2*$turns - 2*$x - 2)*$x - 3*$turns + $y; |
541
|
|
|
|
|
|
|
} |
542
|
|
|
|
|
|
|
} |
543
|
|
|
|
|
|
|
} |
544
|
|
|
|
|
|
|
|
545
|
59697
|
|
|
|
|
125937
|
return $n + $self->{'n_start'}-1; |
546
|
|
|
|
|
|
|
} |
547
|
|
|
|
|
|
|
|
548
|
1
|
|
|
1
|
|
547
|
use Math::PlanePath::SquareArms; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
190
|
|
549
|
|
|
|
|
|
|
*_rect_square_range = \&Math::PlanePath::SquareArms::_rect_square_range; |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
# not exact |
552
|
|
|
|
|
|
|
sub rect_to_n_range { |
553
|
1400
|
|
|
1400
|
1
|
3506
|
my ($self, $x1,$y1, $x2,$y2) = @_; |
554
|
|
|
|
|
|
|
### rect_to_n_range(): "$x1,$y1 $x2,$y2" |
555
|
|
|
|
|
|
|
|
556
|
1400
|
|
|
|
|
2432
|
$x1 = round_nearest ($x1); |
557
|
1400
|
|
|
|
|
2134
|
$y1 = round_nearest ($y1); |
558
|
1400
|
|
|
|
|
2213
|
$x2 = round_nearest ($x2); |
559
|
1400
|
|
|
|
|
2076
|
$y2 = round_nearest ($y2); |
560
|
|
|
|
|
|
|
|
561
|
|
|
|
|
|
|
# floor divisions to square blocks |
562
|
|
|
|
|
|
|
{ |
563
|
1400
|
|
|
|
|
1779
|
my $side = $self->{'turns'} + 1; |
|
1400
|
|
|
|
|
1892
|
|
564
|
1400
|
|
|
|
|
2908
|
_divrem_mutate($x1,$side); |
565
|
1400
|
|
|
|
|
2546
|
_divrem_mutate($y1,$side); |
566
|
1400
|
|
|
|
|
2486
|
_divrem_mutate($x2,$side); |
567
|
1400
|
|
|
|
|
1913
|
_divrem_mutate($y2,$side); |
568
|
|
|
|
|
|
|
} |
569
|
1400
|
|
|
|
|
2529
|
my ($dlo, $dhi) = _rect_square_range ($x1, $y1, |
570
|
|
|
|
|
|
|
$x2, $y2); |
571
|
1400
|
|
|
|
|
2130
|
my $squared = $self->{'squared'}; |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
### d range sides: "$dlo, $dhi" |
574
|
|
|
|
|
|
|
### right start: ((4*$squared*$dlo - 4*$squared)*$dlo + 10) |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
return (($dlo == 0 ? 0 # special case Nlo=1 for innermost square |
577
|
|
|
|
|
|
|
# Nlo at right vertical start |
578
|
|
|
|
|
|
|
: ((4*$squared*$dlo - 4*$squared)*$dlo + $squared)) |
579
|
|
|
|
|
|
|
+ $self->{'n_start'}, |
580
|
|
|
|
|
|
|
|
581
|
|
|
|
|
|
|
# Nhi at bottom horizontal end |
582
|
|
|
|
|
|
|
(4*$squared*$dhi + 4*$squared)*$dhi |
583
|
|
|
|
|
|
|
+ $squared |
584
|
1400
|
100
|
|
|
|
4028
|
+ $self->{'n_start'}-1); |
585
|
|
|
|
|
|
|
} |
586
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
1; |
588
|
|
|
|
|
|
|
__END__ |