line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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 . |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
package Math::PlanePath::KnightSpiral; |
20
|
1
|
|
|
1
|
|
902
|
use 5.004; |
|
1
|
|
|
|
|
4
|
|
21
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
34
|
|
22
|
|
|
|
|
|
|
#use List::Util 'max'; |
23
|
|
|
|
|
|
|
*max = \&Math::PlanePath::_max; |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
1
|
|
4
|
use vars '$VERSION', '@ISA'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
64
|
|
26
|
|
|
|
|
|
|
$VERSION = 127; |
27
|
1
|
|
|
1
|
|
607
|
use Math::PlanePath; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
28
|
|
|
|
|
|
|
@ISA = ('Math::PlanePath'); |
29
|
|
|
|
|
|
|
*_sqrtint = \&Math::PlanePath::_sqrtint; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
use Math::PlanePath::Base::Generic |
32
|
1
|
|
|
1
|
|
5
|
'round_nearest'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
32
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# uncomment this to run the ### lines |
35
|
|
|
|
|
|
|
#use Smart::Comments; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
1
|
|
|
1
|
|
5
|
use constant xy_is_visited => 1; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
98
|
|
39
|
|
|
|
|
|
|
sub x_negative_at_n { |
40
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
41
|
0
|
|
|
|
|
0
|
return $self->n_start + 3; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
sub y_negative_at_n { |
44
|
0
|
|
|
0
|
1
|
0
|
my ($self) = @_; |
45
|
0
|
|
|
|
|
0
|
return $self->n_start + 1; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
sub _UNDOCUMENTED__dxdy_list_at_n { |
48
|
0
|
|
|
0
|
|
0
|
my ($self) = @_; |
49
|
0
|
|
|
|
|
0
|
return $self->n_start + 8; |
50
|
|
|
|
|
|
|
} |
51
|
1
|
|
|
1
|
|
5
|
use constant dx_minimum => -2; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
52
|
1
|
|
|
1
|
|
5
|
use constant dx_maximum => 2; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
33
|
|
53
|
1
|
|
|
1
|
|
4
|
use constant dy_minimum => -2; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
41
|
|
54
|
1
|
|
|
1
|
|
5
|
use constant dy_maximum => 2; |
|
1
|
|
|
|
|
9
|
|
|
1
|
|
|
|
|
62
|
|
55
|
1
|
|
|
|
|
41
|
use constant _UNDOCUMENTED__dxdy_list => (2,1, # ENE |
56
|
|
|
|
|
|
|
1,2, # NNE |
57
|
|
|
|
|
|
|
-1,2, # NNW |
58
|
|
|
|
|
|
|
-2,1, # WNW |
59
|
|
|
|
|
|
|
-2,-1, # WSW |
60
|
|
|
|
|
|
|
-1,-2, # SSW |
61
|
|
|
|
|
|
|
1,-2, # SSE |
62
|
|
|
|
|
|
|
2,-1, # ESE |
63
|
1
|
|
|
1
|
|
5
|
); |
|
1
|
|
|
|
|
2
|
|
64
|
1
|
|
|
1
|
|
4
|
use constant absdx_minimum => 1; |
|
1
|
|
|
|
|
9
|
|
|
1
|
|
|
|
|
55
|
|
65
|
1
|
|
|
1
|
|
5
|
use constant absdy_minimum => 1; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
46
|
|
66
|
1
|
|
|
1
|
|
5
|
use constant dsumxy_minimum => -3; # -2,-1 |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
67
|
1
|
|
|
1
|
|
4
|
use constant dsumxy_maximum => 3; # +2,+1 |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
40
|
|
68
|
1
|
|
|
1
|
|
6
|
use constant ddiffxy_minimum => -3; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
42
|
|
69
|
1
|
|
|
1
|
|
5
|
use constant ddiffxy_maximum => 3; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
36
|
|
70
|
1
|
|
|
1
|
|
4
|
use constant dir_minimum_dxdy => (2,1); # X=2,Y=1 angle |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
71
|
1
|
|
|
1
|
|
6
|
use constant dir_maximum_dxdy => (2,-1); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1073
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Maybe ... |
74
|
|
|
|
|
|
|
# use constant parameter_info_array => |
75
|
|
|
|
|
|
|
# [ |
76
|
|
|
|
|
|
|
# Math::PlanePath::Base::Generic::parameter_info_nstart1(), |
77
|
|
|
|
|
|
|
# ]; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub new { |
82
|
3
|
|
|
3
|
1
|
481
|
my $self = shift->SUPER::new(@_); |
83
|
3
|
50
|
|
|
|
11
|
if (! defined $self->{'n_start'}) { |
84
|
3
|
|
|
|
|
14
|
$self->{'n_start'} = $self->default_n_start; |
85
|
|
|
|
|
|
|
} |
86
|
3
|
|
|
|
|
7
|
return $self; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub _odd { |
90
|
985
|
|
|
985
|
|
1242
|
my ($n) = @_; |
91
|
|
|
|
|
|
|
### _odd(): $n |
92
|
985
|
|
|
|
|
1345
|
$n -= 2*int($n/2); |
93
|
|
|
|
|
|
|
### rem: "$n" |
94
|
985
|
50
|
|
|
|
1442
|
if ($n > 1) { |
95
|
0
|
|
|
|
|
0
|
return 2-$n; |
96
|
|
|
|
|
|
|
} else { |
97
|
985
|
|
|
|
|
2589
|
return $n; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
# return (int($n) % 2); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub n_to_xy { |
103
|
1000
|
|
|
1000
|
1
|
49490
|
my ($self, $n) = @_; |
104
|
|
|
|
|
|
|
#### KnightSpiral n_to_xy: $n |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# adjust to N=1 at origin X=0,Y=0 |
107
|
1000
|
|
|
|
|
1596
|
$n = $n - $self->{'n_start'} + 1; |
108
|
|
|
|
|
|
|
|
109
|
1000
|
100
|
|
|
|
1751
|
if ($n < 2) { |
110
|
1
|
50
|
|
|
|
3
|
if ($n < 1) { return; } |
|
0
|
|
|
|
|
0
|
|
111
|
1
|
|
|
|
|
2
|
$n--; |
112
|
1
|
|
|
|
|
5
|
return (2*$n, -$n); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
999
|
|
|
|
|
1939
|
my $d = int ((7 + _sqrtint($n-1)) / 4); |
116
|
999
|
|
|
|
|
1315
|
my $d1 = $d-1; |
117
|
999
|
|
|
|
|
1249
|
my $outer = 2*$d1; |
118
|
999
|
|
|
|
|
1078
|
my $inner = $outer - 1; |
119
|
999
|
|
|
|
|
1097
|
my $p = 2*$d1; |
120
|
999
|
|
|
|
|
1166
|
my $p1 = $p - 1; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# use Smart::Comments; |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
#### s frac: .25 * (7 + sqrt($n - 1)) |
125
|
|
|
|
|
|
|
#### $d |
126
|
|
|
|
|
|
|
#### $d1 |
127
|
|
|
|
|
|
|
#### $inner |
128
|
|
|
|
|
|
|
#### $outer |
129
|
|
|
|
|
|
|
#### $p |
130
|
|
|
|
|
|
|
#### $p1 |
131
|
|
|
|
|
|
|
|
132
|
999
|
|
|
|
|
1203
|
$n -= $d*(16*$d - 56) + 50; |
133
|
|
|
|
|
|
|
#### remainder: $n |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
# one |
136
|
|
|
|
|
|
|
# |
137
|
999
|
100
|
|
|
|
1495
|
if ($n < $p1) { |
138
|
|
|
|
|
|
|
#### right upwards, eg 2 ... |
139
|
64
|
|
|
|
|
89
|
return (- _odd($n) + $outer, |
140
|
|
|
|
|
|
|
2*$n - $inner); |
141
|
|
|
|
|
|
|
} |
142
|
935
|
|
|
|
|
1038
|
$n -= $p1; |
143
|
|
|
|
|
|
|
|
144
|
935
|
100
|
|
|
|
1385
|
if ($n < $p1) { |
145
|
|
|
|
|
|
|
#### top leftwards, eg 3 ... |
146
|
64
|
|
|
|
|
90
|
return (-2*$n + $inner, |
147
|
|
|
|
|
|
|
_odd($n) + $inner); |
148
|
|
|
|
|
|
|
} |
149
|
871
|
|
|
|
|
1006
|
$n -= $p1; |
150
|
|
|
|
|
|
|
|
151
|
871
|
100
|
|
|
|
1187
|
if ($n < $p) { |
152
|
|
|
|
|
|
|
#### left downwards ... |
153
|
72
|
|
|
|
|
103
|
return ( - _odd($n) - $inner, |
154
|
|
|
|
|
|
|
-2*$n + $outer); |
155
|
|
|
|
|
|
|
} |
156
|
799
|
|
|
|
|
874
|
$n -= $p; |
157
|
|
|
|
|
|
|
|
158
|
799
|
100
|
|
|
|
1087
|
if ($n < $p1) { |
159
|
|
|
|
|
|
|
#### bottom rightwards: $n |
160
|
64
|
|
|
|
|
100
|
return (2*$n - $inner, |
161
|
|
|
|
|
|
|
_odd($n) - $outer); |
162
|
|
|
|
|
|
|
} |
163
|
735
|
|
|
|
|
781
|
$n -= $p1; |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
### two ... |
168
|
|
|
|
|
|
|
# |
169
|
735
|
100
|
|
|
|
991
|
if ($n < $p1) { |
170
|
|
|
|
|
|
|
### right upwards ... |
171
|
64
|
|
|
|
|
93
|
return (_odd($n) + $inner, |
172
|
|
|
|
|
|
|
2*$n - $inner); |
173
|
|
|
|
|
|
|
} |
174
|
671
|
|
|
|
|
697
|
$n -= $p1; |
175
|
|
|
|
|
|
|
|
176
|
671
|
100
|
|
|
|
950
|
if ($n < $p) { |
177
|
|
|
|
|
|
|
#### top leftwards |
178
|
72
|
|
|
|
|
109
|
return (-2*$n + $outer, |
179
|
|
|
|
|
|
|
_odd($n) + $inner); |
180
|
|
|
|
|
|
|
} |
181
|
599
|
|
|
|
|
620
|
$n -= $p; |
182
|
|
|
|
|
|
|
|
183
|
599
|
100
|
|
|
|
823
|
if ($n < $p1) { |
184
|
|
|
|
|
|
|
#### left downwards |
185
|
64
|
|
|
|
|
101
|
return (_odd($n) - $outer, |
186
|
|
|
|
|
|
|
-2*$n + $inner); |
187
|
|
|
|
|
|
|
} |
188
|
535
|
|
|
|
|
560
|
$n -= $p1; |
189
|
|
|
|
|
|
|
|
190
|
535
|
100
|
|
|
|
692
|
if ($n < $p1) { |
191
|
|
|
|
|
|
|
#### bottom rightwards: $n |
192
|
64
|
|
|
|
|
115
|
return (2*$n - $inner, |
193
|
|
|
|
|
|
|
- _odd($n) - $inner); |
194
|
|
|
|
|
|
|
} |
195
|
471
|
|
|
|
|
488
|
$n -= $p1; |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
### three ... |
200
|
|
|
|
|
|
|
# |
201
|
471
|
100
|
|
|
|
633
|
if ($n < $p) { |
202
|
|
|
|
|
|
|
### right upwards, eg 12 ... |
203
|
72
|
|
|
|
|
98
|
return (_odd($n) + $inner, |
204
|
|
|
|
|
|
|
2*$n - $outer); |
205
|
|
|
|
|
|
|
} |
206
|
399
|
|
|
|
|
416
|
$n -= $p; |
207
|
|
|
|
|
|
|
|
208
|
399
|
100
|
|
|
|
579
|
if ($n < $p1) { |
209
|
|
|
|
|
|
|
### top leftwards, eg 14 ... |
210
|
64
|
|
|
|
|
94
|
return (-2*$n + $inner, |
211
|
|
|
|
|
|
|
- _odd($n) + $outer); |
212
|
|
|
|
|
|
|
} |
213
|
335
|
|
|
|
|
362
|
$n -= $p1; |
214
|
|
|
|
|
|
|
|
215
|
335
|
100
|
|
|
|
455
|
if ($n < $p1) { |
216
|
|
|
|
|
|
|
### left downwards, eg 15 ... |
217
|
55
|
|
|
|
|
82
|
return (- _odd($n) - $inner, |
218
|
|
|
|
|
|
|
-2*$n + $inner); |
219
|
|
|
|
|
|
|
} |
220
|
280
|
|
|
|
|
305
|
$n -= $p1; |
221
|
|
|
|
|
|
|
|
222
|
280
|
100
|
|
|
|
375
|
if ($n < $p1) { |
223
|
|
|
|
|
|
|
### bottom rightwards, eg 16 ... |
224
|
49
|
|
|
|
|
72
|
return (2*$n - $outer, |
225
|
|
|
|
|
|
|
- _odd($n) - $inner); |
226
|
|
|
|
|
|
|
} |
227
|
231
|
|
|
|
|
241
|
$n -= $p1; |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
### four ... |
231
|
|
|
|
|
|
|
# |
232
|
231
|
100
|
|
|
|
357
|
if ($n <= 1) { |
233
|
|
|
|
|
|
|
### special 17 upwards ... |
234
|
14
|
|
|
|
|
34
|
return ($n + $outer - 2, |
235
|
|
|
|
|
|
|
2*$n - $outer); |
236
|
|
|
|
|
|
|
} |
237
|
217
|
100
|
|
|
|
293
|
if ($n < $p) { |
238
|
|
|
|
|
|
|
### right upwards ... |
239
|
42
|
|
|
|
|
67
|
return (- _odd($n) + $outer, |
240
|
|
|
|
|
|
|
2*$n - $outer); |
241
|
|
|
|
|
|
|
} |
242
|
175
|
|
|
|
|
200
|
$n -= $p; |
243
|
|
|
|
|
|
|
|
244
|
175
|
100
|
|
|
|
257
|
if ($n < $p) { |
245
|
|
|
|
|
|
|
### top leftwards, eg 19 ... |
246
|
56
|
|
|
|
|
77
|
return (-2*$n + $outer, |
247
|
|
|
|
|
|
|
- _odd($n) + $outer); |
248
|
|
|
|
|
|
|
} |
249
|
119
|
|
|
|
|
123
|
$n -= $p; |
250
|
|
|
|
|
|
|
|
251
|
119
|
100
|
|
|
|
166
|
if ($n < $p) { |
252
|
|
|
|
|
|
|
### left downwards, eg 21 ... |
253
|
56
|
|
|
|
|
89
|
return (_odd($n) - $outer, |
254
|
|
|
|
|
|
|
-2*$n + $outer); |
255
|
|
|
|
|
|
|
} |
256
|
63
|
|
|
|
|
72
|
$n -= $p; |
257
|
|
|
|
|
|
|
|
258
|
63
|
100
|
|
|
|
94
|
if ($n < $p) { |
259
|
|
|
|
|
|
|
### bottom rightwards, eg 23 ... |
260
|
56
|
|
|
|
|
85
|
return (2*$n - $outer, |
261
|
|
|
|
|
|
|
_odd($n) - $outer); |
262
|
|
|
|
|
|
|
} |
263
|
7
|
|
|
|
|
8
|
$n -= $p; |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
### step outwards, eg 25 ... |
266
|
7
|
|
|
|
|
22
|
return (2*$n + $outer, |
267
|
|
|
|
|
|
|
- _odd($n) - $outer); |
268
|
|
|
|
|
|
|
} |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
# 157 92 113 134 155 90 111 132 153 88 109 130 151 |
272
|
|
|
|
|
|
|
# 114 135 156 91 112 133 154 89 110 131 152 87 108 |
273
|
|
|
|
|
|
|
# 93 158 73 32 45 58 71 30 43 56 69 150 129 |
274
|
|
|
|
|
|
|
# 136 115 46 59 72 31 44 57 70 29 42 107 86 |
275
|
|
|
|
|
|
|
# 159 94 33 74 21 4 9 14 19 68 55 128 149 |
276
|
|
|
|
|
|
|
# 116 137 60 47 10 15 20 3 8 41 28 85 106 |
277
|
|
|
|
|
|
|
# 95 160| 75 34 | 5 22 1 18 13 | 54 67| 148 127 |
278
|
|
|
|
|
|
|
# 138 117 48 61 16 11 24 7 2 27 40 105 84 |
279
|
|
|
|
|
|
|
# 161 96 35 76 23 6 17 12 25 66 53 126 147 |
280
|
|
|
|
|
|
|
# 118 139 62 49 78 37 64 51 80 39 26 83 104 |
281
|
|
|
|
|
|
|
# 97 162 77 36 63 50 79 38 65 52 81 146 125 |
282
|
|
|
|
|
|
|
# 140 119 164 99 142 121 166 101 144 123 168 103 82 |
283
|
|
|
|
|
|
|
# 163 98 141 120 165 100 143 122 167 102 145 124 169 |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
sub xy_to_n { |
286
|
0
|
|
|
0
|
1
|
|
my ($self, $x, $y) = @_; |
287
|
0
|
|
|
|
|
|
$x = round_nearest ($x); |
288
|
0
|
|
|
|
|
|
$y = round_nearest ($y); |
289
|
0
|
0
|
0
|
|
|
|
if ($x == 0 && $y == 0) { |
290
|
0
|
|
|
|
|
|
return $self->{'n_start'}; |
291
|
|
|
|
|
|
|
} |
292
|
|
|
|
|
|
|
|
293
|
0
|
|
|
|
|
|
my $r = max(abs($x),abs($y)); |
294
|
0
|
|
|
|
|
|
my $d = int (($r+1)/2); # ring number, counting $x=1,2 as $d==1 |
295
|
0
|
|
|
|
|
|
$r -= (~$r & 1); # next lower odd number |
296
|
|
|
|
|
|
|
### $d |
297
|
|
|
|
|
|
|
### $r |
298
|
|
|
|
|
|
|
|
299
|
0
|
0
|
|
|
|
|
if ($y >= $r) { |
300
|
|
|
|
|
|
|
### top horizontal |
301
|
0
|
|
|
|
|
|
my $xodd = ($x & 1); |
302
|
0
|
|
|
|
|
|
$x = ($x - $xodd) / 2; |
303
|
|
|
|
|
|
|
### $xodd |
304
|
|
|
|
|
|
|
### $x |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
# x odd |
307
|
|
|
|
|
|
|
# [3,30,89,180,303] (16*$d**2 + -21*$d + 8) |
308
|
|
|
|
|
|
|
# [14,57,132,239,378,549] (16*$d**2 + -5*$d + 3) |
309
|
|
|
|
|
|
|
# |
310
|
|
|
|
|
|
|
# [9,44,111,210,341,504] (16*$d**2 + -13*$d + 6) |
311
|
|
|
|
|
|
|
# [20,71,154,269,416] (16*$d**2 + 3*$d + 1) |
312
|
|
|
|
|
|
|
|
313
|
0
|
|
|
|
|
|
my $n = 16*$d*$d - $x; |
314
|
0
|
0
|
|
|
|
|
if (($x ^ $y ^ $d) & 1) { |
315
|
0
|
0
|
|
|
|
|
if ($xodd) { |
316
|
0
|
|
|
|
|
|
return $n -5*$d + 2 + $self->{'n_start'}; |
317
|
|
|
|
|
|
|
} else { |
318
|
0
|
|
|
|
|
|
return $n -13*$d + 5 + $self->{'n_start'}; |
319
|
|
|
|
|
|
|
} |
320
|
|
|
|
|
|
|
} else { |
321
|
0
|
0
|
|
|
|
|
if ($xodd) { |
322
|
0
|
|
|
|
|
|
return $n -21*$d + 7 + $self->{'n_start'}; |
323
|
|
|
|
|
|
|
} else { |
324
|
0
|
|
|
|
|
|
return $n + 3*$d + $self->{'n_start'}; |
325
|
|
|
|
|
|
|
} |
326
|
|
|
|
|
|
|
} |
327
|
|
|
|
|
|
|
} |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
# the lower left outer corner 25,81,169,etc belongs on the bottom |
330
|
|
|
|
|
|
|
# horizontal, it's not an extension downwards from the right vertical |
331
|
|
|
|
|
|
|
# (positions N=18,66,146,etc), hence $x!=-$y |
332
|
|
|
|
|
|
|
# |
333
|
0
|
0
|
0
|
|
|
|
if ($x >= $r && $x != -$y) { |
334
|
|
|
|
|
|
|
### right vertical |
335
|
0
|
|
|
|
|
|
my $yodd = ($y & 1); |
336
|
0
|
|
|
|
|
|
$y = ($y - $yodd) / 2; |
337
|
|
|
|
|
|
|
### $yodd |
338
|
|
|
|
|
|
|
### $y |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
# y odd |
341
|
|
|
|
|
|
|
# [3, 28,85, 174,295, 448,633] (16*$d**2 + -23*$d + 10) |
342
|
|
|
|
|
|
|
# [8,41, 106,203, 332,493] (16*$d**2 + -15*$d + 7) |
343
|
|
|
|
|
|
|
# |
344
|
|
|
|
|
|
|
# y even |
345
|
|
|
|
|
|
|
# [13,54,127,232,369,538] (16*$d**2 + -7*$d + 4) |
346
|
|
|
|
|
|
|
# [18,67,148,261,406,583,792] (16*$d**2 + $d + 1) |
347
|
|
|
|
|
|
|
# |
348
|
0
|
|
|
|
|
|
my $n = 16*$d*$d + $y; |
349
|
0
|
0
|
|
|
|
|
if (($x ^ $y ^ $d) & 1) { |
350
|
0
|
0
|
|
|
|
|
if ($yodd) { |
351
|
0
|
|
|
|
|
|
return $n -15*$d + 6 + $self->{'n_start'}; |
352
|
|
|
|
|
|
|
} else { |
353
|
0
|
|
|
|
|
|
return $n -7*$d + 3 + $self->{'n_start'}; |
354
|
|
|
|
|
|
|
} |
355
|
|
|
|
|
|
|
} else { |
356
|
0
|
0
|
|
|
|
|
if ($yodd) { |
357
|
0
|
|
|
|
|
|
return $n -23*$d + 9 + $self->{'n_start'}; |
358
|
|
|
|
|
|
|
} else { |
359
|
0
|
|
|
|
|
|
return $n + $d + $self->{'n_start'}; |
360
|
|
|
|
|
|
|
} |
361
|
|
|
|
|
|
|
} |
362
|
|
|
|
|
|
|
} |
363
|
|
|
|
|
|
|
|
364
|
0
|
0
|
|
|
|
|
if ($y <= -$r) { |
365
|
|
|
|
|
|
|
### bottom horizontal |
366
|
0
|
|
|
|
|
|
my $xodd = ($x & 1); |
367
|
0
|
|
|
|
|
|
$x = ($x - $xodd) / 2; |
368
|
|
|
|
|
|
|
### $xodd |
369
|
|
|
|
|
|
|
### $x |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
# x odd |
372
|
|
|
|
|
|
|
# [7,38,101,196,323] (16*$d**2 + -17*$d + 8) |
373
|
|
|
|
|
|
|
# [12,51,122,225,360,527] (16*$d**2 + -9*$d + 5) |
374
|
|
|
|
|
|
|
# |
375
|
|
|
|
|
|
|
# x even |
376
|
|
|
|
|
|
|
# [17,64,143,254,397,572] (16*$d**2 + -1*$d + 2) |
377
|
|
|
|
|
|
|
# [24,79,166,285,436] (16*$d**2 + 7*$d + 1) |
378
|
|
|
|
|
|
|
|
379
|
0
|
|
|
|
|
|
my $n = 16*$d*$d + $x; |
380
|
0
|
0
|
|
|
|
|
if (($x ^ $y ^ $d) & 1) { |
381
|
0
|
0
|
|
|
|
|
if ($xodd) { |
382
|
0
|
|
|
|
|
|
return $n -9*$d + 4 + $self->{'n_start'}; |
383
|
|
|
|
|
|
|
} else { |
384
|
0
|
|
|
|
|
|
return $n -1*$d + 1 + $self->{'n_start'}; |
385
|
|
|
|
|
|
|
} |
386
|
|
|
|
|
|
|
} else { |
387
|
0
|
0
|
|
|
|
|
if ($xodd) { |
388
|
0
|
|
|
|
|
|
return $n -17*$d + 7 + $self->{'n_start'}; |
389
|
|
|
|
|
|
|
} else { |
390
|
0
|
|
|
|
|
|
return $n + 7*$d + $self->{'n_start'}; |
391
|
|
|
|
|
|
|
} |
392
|
|
|
|
|
|
|
} |
393
|
|
|
|
|
|
|
} |
394
|
|
|
|
|
|
|
|
395
|
0
|
0
|
|
|
|
|
if ($x <= -$r) { |
396
|
|
|
|
|
|
|
### left vertical |
397
|
0
|
|
|
|
|
|
my $yodd = ($y & 1); |
398
|
0
|
|
|
|
|
|
$y = ($y - $yodd) / 2; |
399
|
|
|
|
|
|
|
### $yodd |
400
|
|
|
|
|
|
|
### $y |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
# y odd |
403
|
|
|
|
|
|
|
# [10,47,116,217,350,515] (16*$d**2 + -11*$d + 5) |
404
|
|
|
|
|
|
|
# [15,60,137,246,387] (16*$d**2 + -3*$d + 2) |
405
|
|
|
|
|
|
|
# |
406
|
|
|
|
|
|
|
# y even |
407
|
|
|
|
|
|
|
# [5,34,95,188,313] (16*$d**2 + -19*$d + 8) |
408
|
|
|
|
|
|
|
# [22,75,160,277,426] (16*$d**2 + 5*$d + 1) |
409
|
|
|
|
|
|
|
# |
410
|
0
|
|
|
|
|
|
my $n = 16*$d*$d - $y; |
411
|
0
|
0
|
|
|
|
|
if (($x ^ $y ^ $d) & 1) { |
412
|
0
|
0
|
|
|
|
|
if ($yodd) { |
413
|
0
|
|
|
|
|
|
return $n -11*$d + 4 + $self->{'n_start'}; |
414
|
|
|
|
|
|
|
} else { |
415
|
0
|
|
|
|
|
|
return $n -19*$d + 7 + $self->{'n_start'}; |
416
|
|
|
|
|
|
|
} |
417
|
|
|
|
|
|
|
} else { |
418
|
0
|
0
|
|
|
|
|
if ($yodd) { |
419
|
0
|
|
|
|
|
|
return $n -3*$d + 1 + $self->{'n_start'}; |
420
|
|
|
|
|
|
|
} else { |
421
|
0
|
|
|
|
|
|
return $n + 5*$d + $self->{'n_start'}; |
422
|
|
|
|
|
|
|
} |
423
|
|
|
|
|
|
|
} |
424
|
|
|
|
|
|
|
} |
425
|
|
|
|
|
|
|
} |
426
|
|
|
|
|
|
|
|
427
|
|
|
|
|
|
|
# not exact |
428
|
|
|
|
|
|
|
sub rect_to_n_range { |
429
|
0
|
|
|
0
|
1
|
|
my ($self, $x1,$y1, $x2,$y2) = @_; |
430
|
|
|
|
|
|
|
|
431
|
0
|
|
|
|
|
|
$x1 = round_nearest ($x1); |
432
|
0
|
|
|
|
|
|
$y1 = round_nearest ($y1); |
433
|
0
|
|
|
|
|
|
$x2 = round_nearest ($x2); |
434
|
0
|
|
|
|
|
|
$y2 = round_nearest ($y2); |
435
|
|
|
|
|
|
|
|
436
|
0
|
|
|
|
|
|
my $x = max(abs($x1),abs($x2)); |
437
|
0
|
|
|
|
|
|
my $y = max(abs($y1),abs($y2)); |
438
|
|
|
|
|
|
|
|
439
|
0
|
|
|
|
|
|
my $d = max(abs($x),abs($y)); |
440
|
0
|
|
|
|
|
|
$d += ($d & 1); # next even number if not already even |
441
|
|
|
|
|
|
|
### $x |
442
|
|
|
|
|
|
|
### $y |
443
|
|
|
|
|
|
|
### $d |
444
|
|
|
|
|
|
|
### is: $d*$d |
445
|
|
|
|
|
|
|
|
446
|
0
|
|
|
|
|
|
$d = 2*$d+1; # width of whole square |
447
|
|
|
|
|
|
|
# ENHANCE-ME: find actual minimum if rect doesn't cover 0,0 |
448
|
|
|
|
|
|
|
return ($self->{'n_start'}, |
449
|
0
|
|
|
|
|
|
$self->{'n_start'} + $d*$d); |
450
|
|
|
|
|
|
|
} |
451
|
|
|
|
|
|
|
|
452
|
|
|
|
|
|
|
1; |
453
|
|
|
|
|
|
|
__END__ |