| 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::Staircase; |
|
20
|
1
|
|
|
1
|
|
1003
|
use 5.004; |
|
|
1
|
|
|
|
|
4
|
|
|
21
|
1
|
|
|
1
|
|
5
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
25
|
|
|
22
|
|
|
|
|
|
|
|
|
23
|
1
|
|
|
1
|
|
4
|
use vars '$VERSION', '@ISA'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
54
|
|
|
24
|
|
|
|
|
|
|
$VERSION = 128; |
|
25
|
1
|
|
|
1
|
|
700
|
use Math::PlanePath; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
58
|
|
|
26
|
|
|
|
|
|
|
@ISA = ('Math::PlanePath'); |
|
27
|
|
|
|
|
|
|
*_divrem_mutate = \&Math::PlanePath::_divrem_mutate; |
|
28
|
|
|
|
|
|
|
*_sqrtint = \&Math::PlanePath::_sqrtint; |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
use Math::PlanePath::Base::Generic |
|
31
|
1
|
|
|
1
|
|
6
|
'round_nearest'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
40
|
|
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# uncomment this to run the ### lines |
|
34
|
|
|
|
|
|
|
#use Smart::Comments; |
|
35
|
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
|
|
37
|
1
|
|
|
1
|
|
5
|
use constant class_x_negative => 0; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
47
|
|
|
38
|
1
|
|
|
1
|
|
5
|
use constant class_y_negative => 0; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
42
|
|
|
39
|
1
|
|
|
1
|
|
5
|
use constant n_frac_discontinuity => .5; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
52
|
|
|
40
|
|
|
|
|
|
|
*xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad1; |
|
41
|
|
|
|
|
|
|
|
|
42
|
1
|
|
|
1
|
|
5
|
use constant dx_maximum => 1; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
54
|
|
|
43
|
1
|
|
|
1
|
|
8
|
use constant dy_minimum => -1; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
56
|
|
|
44
|
1
|
|
|
1
|
|
6
|
use constant dsumxy_minimum => -1; # straight S |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
52
|
|
|
45
|
1
|
|
|
1
|
|
6
|
use constant dsumxy_maximum => 2; # next row |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
85
|
|
|
46
|
1
|
|
|
1
|
|
7
|
use constant ddiffxy_maximum => 1; # straight S,E |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
55
|
|
|
47
|
1
|
|
|
1
|
|
7
|
use constant dir_maximum_dxdy => (0,-1); # South |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
55
|
|
|
48
|
|
|
|
|
|
|
|
|
49
|
1
|
|
|
|
|
560
|
use constant parameter_info_array => |
|
50
|
|
|
|
|
|
|
[ |
|
51
|
|
|
|
|
|
|
Math::PlanePath::Base::Generic::parameter_info_nstart1(), |
|
52
|
1
|
|
|
1
|
|
6
|
]; |
|
|
1
|
|
|
|
|
1
|
|
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
#------------------------------------------------------------------------------ |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub new { |
|
58
|
3
|
|
|
3
|
1
|
1867
|
my $self = shift->SUPER::new(@_); |
|
59
|
3
|
50
|
|
|
|
17
|
if (! defined $self->{'n_start'}) { |
|
60
|
3
|
|
|
|
|
17
|
$self->{'n_start'} = $self->default_n_start; |
|
61
|
|
|
|
|
|
|
} |
|
62
|
3
|
|
|
|
|
7
|
return $self; |
|
63
|
|
|
|
|
|
|
} |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# start from 0.5 back |
|
66
|
|
|
|
|
|
|
# d = [ 0, 1, 2, 3 ] |
|
67
|
|
|
|
|
|
|
# n = [ 1.5, 6.5, 15.5 ] |
|
68
|
|
|
|
|
|
|
# n = ((2*$d - 1)*$d + 0.5) |
|
69
|
|
|
|
|
|
|
# d = 1/4 + sqrt(1/2 * $n + -3/16) |
|
70
|
|
|
|
|
|
|
# |
|
71
|
|
|
|
|
|
|
# start from integer vertical |
|
72
|
|
|
|
|
|
|
# d = [ 0, 1, 2, 3, 4 ] |
|
73
|
|
|
|
|
|
|
# n = [ 1, 2, 7, 16, 29 ] |
|
74
|
|
|
|
|
|
|
# n = ((2*$d - 1)*$d + 1) |
|
75
|
|
|
|
|
|
|
# d = 1/4 + sqrt(1/2 * $n + -7/16) |
|
76
|
|
|
|
|
|
|
# = [1 + sqrt(8*$n-7) ] / 4 |
|
77
|
|
|
|
|
|
|
# |
|
78
|
|
|
|
|
|
|
sub n_to_xy { |
|
79
|
25
|
|
|
25
|
1
|
6247
|
my ($self, $n) = @_; |
|
80
|
|
|
|
|
|
|
#### Staircase n_to_xy: $n |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# adjust to N=1 start |
|
83
|
25
|
|
|
|
|
52
|
$n = $n - $self->{'n_start'} + 1; |
|
84
|
|
|
|
|
|
|
|
|
85
|
25
|
|
|
|
|
42
|
my $d; |
|
86
|
|
|
|
|
|
|
{ |
|
87
|
25
|
|
|
|
|
33
|
my $r = 8*$n - 3; |
|
|
25
|
|
|
|
|
46
|
|
|
88
|
25
|
50
|
|
|
|
59
|
if ($r < 1) { |
|
89
|
0
|
|
|
|
|
0
|
return; # N < 0.5, so before start of path |
|
90
|
|
|
|
|
|
|
} |
|
91
|
25
|
|
|
|
|
67
|
$d = int( (_sqrtint($r) + 1)/4 ); |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
### $d |
|
94
|
|
|
|
|
|
|
### base: ((2*$d - 1)*$d + 0.5) |
|
95
|
|
|
|
|
|
|
|
|
96
|
25
|
|
|
|
|
48
|
$n -= (2*$d - 1)*$d; |
|
97
|
|
|
|
|
|
|
### fractional: $n |
|
98
|
|
|
|
|
|
|
|
|
99
|
25
|
|
|
|
|
36
|
my $int = int($n); |
|
100
|
25
|
|
|
|
|
41
|
$n -= $int; |
|
101
|
|
|
|
|
|
|
|
|
102
|
25
|
|
|
|
|
60
|
my $rem = _divrem_mutate ($int, 2); |
|
103
|
25
|
100
|
|
|
|
52
|
if ($rem) { |
|
104
|
|
|
|
|
|
|
### down ... |
|
105
|
14
|
|
|
|
|
45
|
return ($int, |
|
106
|
|
|
|
|
|
|
-$n + 2*$d - $int); |
|
107
|
|
|
|
|
|
|
} else { |
|
108
|
|
|
|
|
|
|
### across ... |
|
109
|
11
|
|
|
|
|
38
|
return ($n + $int-1, |
|
110
|
|
|
|
|
|
|
2*$d - $int); |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# d = [ 1 2, 3, 4 ] |
|
115
|
|
|
|
|
|
|
# N = [ 2, 7, 16, 29 ] |
|
116
|
|
|
|
|
|
|
# N = (2 d^2 - d + 1) |
|
117
|
|
|
|
|
|
|
# and add 2*$d |
|
118
|
|
|
|
|
|
|
# base = 2*d^2 - d + 1 + 2*d |
|
119
|
|
|
|
|
|
|
# = 2*d^2 + d + 1 |
|
120
|
|
|
|
|
|
|
# = (2*$d + 1)*$d + 1 |
|
121
|
|
|
|
|
|
|
# |
|
122
|
|
|
|
|
|
|
sub xy_to_n { |
|
123
|
15
|
|
|
15
|
1
|
863
|
my ($self, $x, $y) = @_; |
|
124
|
|
|
|
|
|
|
|
|
125
|
15
|
|
|
|
|
36
|
$x = round_nearest ($x); |
|
126
|
15
|
|
|
|
|
29
|
$y = round_nearest ($y); |
|
127
|
15
|
50
|
33
|
|
|
61
|
if ($x < 0 || $y < 0) { |
|
128
|
0
|
|
|
|
|
0
|
return undef; |
|
129
|
|
|
|
|
|
|
} |
|
130
|
15
|
|
|
|
|
29
|
my $d = int(($x + $y + 1) / 2); |
|
131
|
15
|
|
|
|
|
38
|
return (2*$d + 1)*$d - $y + $x + $self->{'n_start'}; |
|
132
|
|
|
|
|
|
|
} |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# exact |
|
135
|
|
|
|
|
|
|
sub rect_to_n_range { |
|
136
|
0
|
|
|
0
|
1
|
|
my ($self, $x1,$y1, $x2,$y2) = @_; |
|
137
|
|
|
|
|
|
|
### Staircase rect_to_n_range(): "$x1,$y1 $x2,$y2" |
|
138
|
|
|
|
|
|
|
|
|
139
|
0
|
|
|
|
|
|
$x1 = round_nearest ($x1); |
|
140
|
0
|
|
|
|
|
|
$y1 = round_nearest ($y1); |
|
141
|
0
|
|
|
|
|
|
$x2 = round_nearest ($x2); |
|
142
|
0
|
|
|
|
|
|
$y2 = round_nearest ($y2); |
|
143
|
|
|
|
|
|
|
|
|
144
|
0
|
0
|
|
|
|
|
if ($x1 > $x2) { ($x1,$x2) = ($x2,$x1); } # x2 > x1 |
|
|
0
|
|
|
|
|
|
|
|
145
|
0
|
0
|
|
|
|
|
if ($y1 > $y2) { ($y1,$y2) = ($y2,$y1); } # y2 > y1 |
|
|
0
|
|
|
|
|
|
|
|
146
|
0
|
0
|
0
|
|
|
|
if ($x2 < 0 || $y2 < 0) { |
|
147
|
0
|
|
|
|
|
|
return (1, 0); # nothing outside first quadrant |
|
148
|
|
|
|
|
|
|
} |
|
149
|
|
|
|
|
|
|
|
|
150
|
0
|
0
|
|
|
|
|
if ($x1 < 0) { $x1 *= 0; } |
|
|
0
|
|
|
|
|
|
|
|
151
|
0
|
0
|
|
|
|
|
if ($y1 < 0) { $y1 *= 0; } |
|
|
0
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
my $y_min = $y1; |
|
153
|
|
|
|
|
|
|
|
|
154
|
0
|
0
|
0
|
|
|
|
if ((($x1 ^ $y1) & 1) && $y1 < $y2) { # y2==y_max |
|
155
|
0
|
|
|
|
|
|
$y1 += 1; |
|
156
|
|
|
|
|
|
|
### y1 inc: $y1 |
|
157
|
|
|
|
|
|
|
} |
|
158
|
0
|
0
|
0
|
|
|
|
if (! (($x2 ^ $y2) & 1) && $y2 > $y_min) { |
|
159
|
0
|
|
|
|
|
|
$y2 -= 1; |
|
160
|
|
|
|
|
|
|
### y2 dec: $y2 |
|
161
|
|
|
|
|
|
|
} |
|
162
|
0
|
|
|
|
|
|
return ($self->xy_to_n($x1,$y1), |
|
163
|
|
|
|
|
|
|
$self->xy_to_n($x2,$y2)); |
|
164
|
|
|
|
|
|
|
} |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
1; |
|
167
|
|
|
|
|
|
|
__END__ |