| 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=AR2W2Curve --all --output=numbers_dash | ||
| 20 | # | ||
| 21 | # http://www.springerlink.com/content/y1l60g7125038668/ [pay] | ||
| 22 | # | ||
| 23 | # Google Books LATIN'95 link: page 44 definition | ||
| 24 | # http://books.google.com.au/books?id=_aKhJUJunYwC&lpg=PA44&ots=ARyDkP_hjU&dq=%22Space-Filling%20Curves%20and%20Their%20Use%20in%20the%20Design%20of%20Geometric%20Data%20Structures%22&pg=PA44#v=onepage&q&f=false | ||
| 25 | # | ||
| 26 | |||
| 27 | package Math::PlanePath::AR2W2Curve; | ||
| 28 | use 5.004; | ||
| 29 | use strict; | ||
| 30 | use Carp 'croak'; | ||
| 31 | #use List::Util 'max'; | ||
| 32 | *max = \&Math::PlanePath::_max; | ||
| 33 | |||
| 34 | use vars '$VERSION', '@ISA'; | ||
| 35 | $VERSION = 128; | ||
| 36 | use Math::PlanePath; | ||
| 37 | @ISA = ('Math::PlanePath'); | ||
| 38 | |||
| 39 | use Math::PlanePath::Base::Generic | ||
| 40 | 'is_infinite', | ||
| 41 | 'round_nearest'; | ||
| 42 | use Math::PlanePath::Base::Digits | ||
| 43 | 'round_down_pow', | ||
| 44 | 'bit_split_lowtohigh', | ||
| 45 | 'digit_split_lowtohigh', | ||
| 46 | 'digit_join_lowtohigh'; | ||
| 47 | |||
| 48 | |||
| 49 | use constant n_start => 0; | ||
| 50 | use constant class_x_negative => 0; | ||
| 51 | use constant class_y_negative => 0; | ||
| 52 | *xy_is_visited = \&Math::PlanePath::Base::Generic::xy_is_visited_quad1; | ||
| 53 | |||
| 54 | use constant parameter_info_array => | ||
| 55 | [ | ||
| 56 | { | ||
| 57 | name => 'start_shape', | ||
| 58 | share_key => 'start_shape_ar2w2', | ||
| 59 | display => 'Start Shape', | ||
| 60 | type => 'enum', | ||
| 61 | default => 'A1', | ||
| 62 | choices => ['A1','D2', | ||
| 63 | 'B2','B1rev', | ||
| 64 | 'D1rev','A2rev', | ||
| 65 | ], | ||
| 66 | choices_display => ['A1','D2', | ||
| 67 | 'B2','B1rev', | ||
| 68 | 'D1rev','A2rev', | ||
| 69 | ], | ||
| 70 | description => 'The starting 2x2 pattern in the bottom left corner.', | ||
| 71 | }, | ||
| 72 | ]; | ||
| 73 | |||
| 74 | use constant dx_minimum => -1; # NSEW+diagonals | ||
| 75 | use constant dx_maximum => 1; | ||
| 76 | use constant dy_minimum => -1; | ||
| 77 | use constant dy_maximum => 1; | ||
| 78 | *_UNDOCUMENTED__dxdy_list = \&Math::PlanePath::_UNDOCUMENTED__dxdy_list_eight; | ||
| 79 | { | ||
| 80 | my %_UNDOCUMENTED__dxdy_list_at_n = (A1 => 201, | ||
| 81 | D2 => 329, | ||
| 82 | B2 => 201, | ||
| 83 | B1rev => 201, | ||
| 84 | D1rev => 329, | ||
| 85 | A2rev => 201, | ||
| 86 | ); | ||
| 87 | sub _UNDOCUMENTED__dxdy_list_at_n { | ||
| 88 | my ($self) = @_; | ||
| 89 | return $_UNDOCUMENTED__dxdy_list_at_n{$self->{'start_shape'}}; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | use constant dsumxy_minimum => -2; # diagonals | ||
| 94 | use constant dsumxy_maximum => 2; | ||
| 95 | use constant ddiffxy_minimum => -2; | ||
| 96 | use constant ddiffxy_maximum => 2; | ||
| 97 | use constant dir_maximum_dxdy => (1,-1); # South-East | ||
| 98 | |||
| 99 | |||
| 100 | #------------------------------------------------------------------------------ | ||
| 101 | |||
| 102 | # tables generated by tools/ar2w2-curve-table.pl | ||
| 103 | # | ||
| 104 | my @next_state | ||
| 105 | = (224, 92,132,120, 228, 80,136,124, 232, 84,140,112, 236, 88,128,116, | ||
| 106 | 104,148, 76,240, 108,152, 64,244, 96,156, 68,248, 100,144, 72,252, | ||
| 107 | 92,160,120,196, 80,164,124,200, 84,168,112,204, 88,172,116,192, | ||
| 108 | 212,104,176, 76, 216,108,180, 64, 220, 96,184, 68, 208,100,188, 72, | ||
| 109 | 220,160, 64,116, 208,164, 68,120, 212,168, 72,124, 216,172, 76,112, | ||
| 110 | 100, 80,176,204, 104, 84,180,192, 108, 88,184,196, 96, 92,188,200, | ||
| 111 | 92, 96,128,244, 80,100,132,248, 84,104,136,252, 88,108,140,240, | ||
| 112 | 228,144,112, 76, 232,148,116, 64, 236,152,120, 68, 224,156,124, 72, | ||
| 113 | 32, 68, 12,116, 36, 72, 0,120, 40, 76, 4,124, 44, 64, 8,112, | ||
| 114 | 100, 28, 84, 48, 104, 16, 88, 52, 108, 20, 92, 56, 96, 24, 80, 60, | ||
| 115 | 92, 32,108, 12, 80, 36, 96, 0, 84, 40,100, 4, 88, 44,104, 8, | ||
| 116 | 28,124, 48, 76, 16,112, 52, 64, 20,116, 56, 68, 24,120, 60, 72, | ||
| 117 | 220, 32,172, 44, 208, 36,160, 32, 212, 40,164, 36, 216, 44,168, 40, | ||
| 118 | 60,188, 48,204, 48,176, 52,192, 52,180, 56,196, 56,184, 60,200, | ||
| 119 | 0,132, 12,244, 4,136, 0,248, 8,140, 4,252, 12,128, 8,240, | ||
| 120 | 228, 28,148, 16, 232, 16,152, 20, 236, 20,156, 24, 224, 24,144, 28); | ||
| 121 | my @digit_to_x | ||
| 122 | = (0,1,0,1, 1,1,0,0, 1,0,1,0, 0,0,1,1, | ||
| 123 | 1,0,1,0, 0,0,1,1, 0,1,0,1, 1,1,0,0, | ||
| 124 | 0,0,1,1, 1,0,1,0, 1,1,0,0, 0,1,0,1, | ||
| 125 | 1,1,0,0, 0,1,0,1, 0,0,1,1, 1,0,1,0, | ||
| 126 | 0,0,1,1, 1,0,0,1, 1,1,0,0, 0,1,1,0, | ||
| 127 | 1,1,0,0, 1,0,0,1, 0,0,1,1, 0,1,1,0, | ||
| 128 | 0,0,1,1, 1,0,0,1, 1,1,0,0, 0,1,1,0, | ||
| 129 | 1,1,0,0, 1,0,0,1, 0,0,1,1, 0,1,1,0, | ||
| 130 | 0,0,1,1, 1,0,0,1, 1,1,0,0, 0,1,1,0, | ||
| 131 | 1,1,0,0, 1,0,0,1, 0,0,1,1, 0,1,1,0, | ||
| 132 | 0,0,1,1, 1,0,0,1, 1,1,0,0, 0,1,1,0, | ||
| 133 | 1,1,0,0, 1,0,0,1, 0,0,1,1, 0,1,1,0, | ||
| 134 | 0,0,1,1, 1,0,0,1, 1,1,0,0, 0,1,1,0, | ||
| 135 | 1,1,0,0, 1,0,0,1, 0,0,1,1, 0,1,1,0, | ||
| 136 | 0,0,1,1, 1,0,0,1, 1,1,0,0, 0,1,1,0, | ||
| 137 | 1,1,0,0, 1,0,0,1, 0,0,1,1, 0,1,1,0); | ||
| 138 | my @digit_to_y | ||
| 139 | = (0,0,1,1, 0,1,0,1, 1,1,0,0, 1,0,1,0, | ||
| 140 | 1,1,0,0, 1,0,1,0, 0,0,1,1, 0,1,0,1, | ||
| 141 | 0,1,0,1, 0,0,1,1, 1,0,1,0, 1,1,0,0, | ||
| 142 | 1,0,1,0, 1,1,0,0, 0,1,0,1, 0,0,1,1, | ||
| 143 | 0,1,1,0, 0,0,1,1, 1,0,0,1, 1,1,0,0, | ||
| 144 | 0,1,1,0, 1,1,0,0, 1,0,0,1, 0,0,1,1, | ||
| 145 | 0,1,1,0, 0,0,1,1, 1,0,0,1, 1,1,0,0, | ||
| 146 | 0,1,1,0, 1,1,0,0, 1,0,0,1, 0,0,1,1, | ||
| 147 | 0,1,1,0, 0,0,1,1, 1,0,0,1, 1,1,0,0, | ||
| 148 | 0,1,1,0, 1,1,0,0, 1,0,0,1, 0,0,1,1, | ||
| 149 | 0,1,1,0, 0,0,1,1, 1,0,0,1, 1,1,0,0, | ||
| 150 | 0,1,1,0, 1,1,0,0, 1,0,0,1, 0,0,1,1, | ||
| 151 | 0,1,1,0, 0,0,1,1, 1,0,0,1, 1,1,0,0, | ||
| 152 | 0,1,1,0, 1,1,0,0, 1,0,0,1, 0,0,1,1, | ||
| 153 | 0,1,1,0, 0,0,1,1, 1,0,0,1, 1,1,0,0, | ||
| 154 | 0,1,1,0, 1,1,0,0, 1,0,0,1, 0,0,1,1); | ||
| 155 | my @yx_to_digit | ||
| 156 | = (0,1,2,3, 2,0,3,1, 3,2,1,0, 1,3,0,2, | ||
| 157 | 3,2,1,0, 1,3,0,2, 0,1,2,3, 2,0,3,1, | ||
| 158 | 0,2,1,3, 1,0,3,2, 3,1,2,0, 2,3,0,1, | ||
| 159 | 3,1,2,0, 2,3,0,1, 0,2,1,3, 1,0,3,2, | ||
| 160 | 0,3,1,2, 1,0,2,3, 2,1,3,0, 3,2,0,1, | ||
| 161 | 3,0,2,1, 2,3,1,0, 1,2,0,3, 0,1,3,2, | ||
| 162 | 0,3,1,2, 1,0,2,3, 2,1,3,0, 3,2,0,1, | ||
| 163 | 3,0,2,1, 2,3,1,0, 1,2,0,3, 0,1,3,2, | ||
| 164 | 0,3,1,2, 1,0,2,3, 2,1,3,0, 3,2,0,1, | ||
| 165 | 3,0,2,1, 2,3,1,0, 1,2,0,3, 0,1,3,2, | ||
| 166 | 0,3,1,2, 1,0,2,3, 2,1,3,0, 3,2,0,1, | ||
| 167 | 3,0,2,1, 2,3,1,0, 1,2,0,3, 0,1,3,2, | ||
| 168 | 0,3,1,2, 1,0,2,3, 2,1,3,0, 3,2,0,1, | ||
| 169 | 3,0,2,1, 2,3,1,0, 1,2,0,3, 0,1,3,2, | ||
| 170 | 0,3,1,2, 1,0,2,3, 2,1,3,0, 3,2,0,1, | ||
| 171 | 3,0,2,1, 2,3,1,0, 1,2,0,3, 0,1,3,2); | ||
| 172 | my @min_digit = (0,0,1, 0,0,1, 2,2,3, undef,undef,undef, # 3* 0 | ||
| 173 | 2,0,0, 2,0,0, 3,1,1, undef,undef,undef, # 3* 4 | ||
| 174 | 3,2,2, 1,0,0, 1,0,0, undef,undef,undef, # 3* 8 | ||
| 175 | 1,1,3, 0,0,2, 0,0,2, undef,undef,undef, # 3* 12 | ||
| 176 | 3,2,2, 1,0,0, 1,0,0, undef,undef,undef, # 3* 16 | ||
| 177 | 1,1,3, 0,0,2, 0,0,2, undef,undef,undef, # 3* 20 | ||
| 178 | 0,0,1, 0,0,1, 2,2,3, undef,undef,undef, # 3* 24 | ||
| 179 | 2,0,0, 2,0,0, 3,1,1, undef,undef,undef, # 3* 28 | ||
| 180 | 0,0,2, 0,0,2, 1,1,3, undef,undef,undef, # 3* 32 | ||
| 181 | 1,0,0, 1,0,0, 3,2,2, undef,undef,undef, # 3* 36 | ||
| 182 | 3,1,1, 2,0,0, 2,0,0, undef,undef,undef, # 3* 40 | ||
| 183 | 2,2,3, 0,0,1, 0,0,1, undef,undef,undef, # 3* 44 | ||
| 184 | 3,1,1, 2,0,0, 2,0,0, undef,undef,undef, # 3* 48 | ||
| 185 | 2,2,3, 0,0,1, 0,0,1, undef,undef,undef, # 3* 52 | ||
| 186 | 0,0,2, 0,0,2, 1,1,3, undef,undef,undef, # 3* 56 | ||
| 187 | 1,0,0, 1,0,0, 3,2,2, undef,undef,undef, # 3* 60 | ||
| 188 | 0,0,3, 0,0,2, 1,1,2, undef,undef,undef, # 3* 64 | ||
| 189 | 1,0,0, 1,0,0, 2,2,3, undef,undef,undef, # 3* 68 | ||
| 190 | 2,1,1, 2,0,0, 3,0,0, undef,undef,undef, # 3* 72 | ||
| 191 | 3,2,2, 0,0,1, 0,0,1, undef,undef,undef, # 3* 76 | ||
| 192 | 3,0,0, 2,0,0, 2,1,1, undef,undef,undef, # 3* 80 | ||
| 193 | 2,2,3, 1,0,0, 1,0,0, undef,undef,undef, # 3* 84 | ||
| 194 | 1,1,2, 0,0,2, 0,0,3, undef,undef,undef, # 3* 88 | ||
| 195 | 0,0,1, 0,0,1, 3,2,2, undef,undef,undef, # 3* 92 | ||
| 196 | 0,0,3, 0,0,2, 1,1,2, undef,undef,undef, # 3* 96 | ||
| 197 | 1,0,0, 1,0,0, 2,2,3, undef,undef,undef, # 3* 100 | ||
| 198 | 2,1,1, 2,0,0, 3,0,0, undef,undef,undef, # 3* 104 | ||
| 199 | 3,2,2, 0,0,1, 0,0,1, undef,undef,undef, # 3* 108 | ||
| 200 | 3,0,0, 2,0,0, 2,1,1, undef,undef,undef, # 3* 112 | ||
| 201 | 2,2,3, 1,0,0, 1,0,0, undef,undef,undef, # 3* 116 | ||
| 202 | 1,1,2, 0,0,2, 0,0,3, undef,undef,undef, # 3* 120 | ||
| 203 | 0,0,1, 0,0,1, 3,2,2, undef,undef,undef, # 3* 124 | ||
| 204 | 0,0,3, 0,0,2, 1,1,2, undef,undef,undef, # 3* 128 | ||
| 205 | 1,0,0, 1,0,0, 2,2,3, undef,undef,undef, # 3* 132 | ||
| 206 | 2,1,1, 2,0,0, 3,0,0, undef,undef,undef, # 3* 136 | ||
| 207 | 3,2,2, 0,0,1, 0,0,1, undef,undef,undef, # 3* 140 | ||
| 208 | 3,0,0, 2,0,0, 2,1,1, undef,undef,undef, # 3* 144 | ||
| 209 | 2,2,3, 1,0,0, 1,0,0, undef,undef,undef, # 3* 148 | ||
| 210 | 1,1,2, 0,0,2, 0,0,3, undef,undef,undef, # 3* 152 | ||
| 211 | 0,0,1, 0,0,1, 3,2,2, undef,undef,undef, # 3* 156 | ||
| 212 | 0,0,3, 0,0,2, 1,1,2, undef,undef,undef, # 3* 160 | ||
| 213 | 1,0,0, 1,0,0, 2,2,3, undef,undef,undef, # 3* 164 | ||
| 214 | 2,1,1, 2,0,0, 3,0,0, undef,undef,undef, # 3* 168 | ||
| 215 | 3,2,2, 0,0,1, 0,0,1, undef,undef,undef, # 3* 172 | ||
| 216 | 3,0,0, 2,0,0, 2,1,1, undef,undef,undef, # 3* 176 | ||
| 217 | 2,2,3, 1,0,0, 1,0,0, undef,undef,undef, # 3* 180 | ||
| 218 | 1,1,2, 0,0,2, 0,0,3, undef,undef,undef, # 3* 184 | ||
| 219 | 0,0,1, 0,0,1, 3,2,2, undef,undef,undef, # 3* 188 | ||
| 220 | 0,0,3, 0,0,2, 1,1,2, undef,undef,undef, # 3* 192 | ||
| 221 | 1,0,0, 1,0,0, 2,2,3, undef,undef,undef, # 3* 196 | ||
| 222 | 2,1,1, 2,0,0, 3,0,0, undef,undef,undef, # 3* 200 | ||
| 223 | 3,2,2, 0,0,1, 0,0,1, undef,undef,undef, # 3* 204 | ||
| 224 | 3,0,0, 2,0,0, 2,1,1, undef,undef,undef, # 3* 208 | ||
| 225 | 2,2,3, 1,0,0, 1,0,0, undef,undef,undef, # 3* 212 | ||
| 226 | 1,1,2, 0,0,2, 0,0,3, undef,undef,undef, # 3* 216 | ||
| 227 | 0,0,1, 0,0,1, 3,2,2, undef,undef,undef, # 3* 220 | ||
| 228 | 0,0,3, 0,0,2, 1,1,2, undef,undef,undef, # 3* 224 | ||
| 229 | 1,0,0, 1,0,0, 2,2,3, undef,undef,undef, # 3* 228 | ||
| 230 | 2,1,1, 2,0,0, 3,0,0, undef,undef,undef, # 3* 232 | ||
| 231 | 3,2,2, 0,0,1, 0,0,1, undef,undef,undef, # 3* 236 | ||
| 232 | 3,0,0, 2,0,0, 2,1,1, undef,undef,undef, # 3* 240 | ||
| 233 | 2,2,3, 1,0,0, 1,0,0, undef,undef,undef, # 3* 244 | ||
| 234 | 1,1,2, 0,0,2, 0,0,3, undef,undef,undef, # 3* 248 | ||
| 235 | 0,0,1, 0,0,1, 3,2,2); | ||
| 236 | my @max_digit = (0,1,1, 2,3,3, 2,3,3, undef,undef,undef, # 3* 0 | ||
| 237 | 2,2,0, 3,3,1, 3,3,1, undef,undef,undef, # 3* 4 | ||
| 238 | 3,3,2, 3,3,2, 1,1,0, undef,undef,undef, # 3* 8 | ||
| 239 | 1,3,3, 1,3,3, 0,2,2, undef,undef,undef, # 3* 12 | ||
| 240 | 3,3,2, 3,3,2, 1,1,0, undef,undef,undef, # 3* 16 | ||
| 241 | 1,3,3, 1,3,3, 0,2,2, undef,undef,undef, # 3* 20 | ||
| 242 | 0,1,1, 2,3,3, 2,3,3, undef,undef,undef, # 3* 24 | ||
| 243 | 2,2,0, 3,3,1, 3,3,1, undef,undef,undef, # 3* 28 | ||
| 244 | 0,2,2, 1,3,3, 1,3,3, undef,undef,undef, # 3* 32 | ||
| 245 | 1,1,0, 3,3,2, 3,3,2, undef,undef,undef, # 3* 36 | ||
| 246 | 3,3,1, 3,3,1, 2,2,0, undef,undef,undef, # 3* 40 | ||
| 247 | 2,3,3, 2,3,3, 0,1,1, undef,undef,undef, # 3* 44 | ||
| 248 | 3,3,1, 3,3,1, 2,2,0, undef,undef,undef, # 3* 48 | ||
| 249 | 2,3,3, 2,3,3, 0,1,1, undef,undef,undef, # 3* 52 | ||
| 250 | 0,2,2, 1,3,3, 1,3,3, undef,undef,undef, # 3* 56 | ||
| 251 | 1,1,0, 3,3,2, 3,3,2, undef,undef,undef, # 3* 60 | ||
| 252 | 0,3,3, 1,3,3, 1,2,2, undef,undef,undef, # 3* 64 | ||
| 253 | 1,1,0, 2,3,3, 2,3,3, undef,undef,undef, # 3* 68 | ||
| 254 | 2,2,1, 3,3,1, 3,3,0, undef,undef,undef, # 3* 72 | ||
| 255 | 3,3,2, 3,3,2, 0,1,1, undef,undef,undef, # 3* 76 | ||
| 256 | 3,3,0, 3,3,1, 2,2,1, undef,undef,undef, # 3* 80 | ||
| 257 | 2,3,3, 2,3,3, 1,1,0, undef,undef,undef, # 3* 84 | ||
| 258 | 1,2,2, 1,3,3, 0,3,3, undef,undef,undef, # 3* 88 | ||
| 259 | 0,1,1, 3,3,2, 3,3,2, undef,undef,undef, # 3* 92 | ||
| 260 | 0,3,3, 1,3,3, 1,2,2, undef,undef,undef, # 3* 96 | ||
| 261 | 1,1,0, 2,3,3, 2,3,3, undef,undef,undef, # 3* 100 | ||
| 262 | 2,2,1, 3,3,1, 3,3,0, undef,undef,undef, # 3* 104 | ||
| 263 | 3,3,2, 3,3,2, 0,1,1, undef,undef,undef, # 3* 108 | ||
| 264 | 3,3,0, 3,3,1, 2,2,1, undef,undef,undef, # 3* 112 | ||
| 265 | 2,3,3, 2,3,3, 1,1,0, undef,undef,undef, # 3* 116 | ||
| 266 | 1,2,2, 1,3,3, 0,3,3, undef,undef,undef, # 3* 120 | ||
| 267 | 0,1,1, 3,3,2, 3,3,2, undef,undef,undef, # 3* 124 | ||
| 268 | 0,3,3, 1,3,3, 1,2,2, undef,undef,undef, # 3* 128 | ||
| 269 | 1,1,0, 2,3,3, 2,3,3, undef,undef,undef, # 3* 132 | ||
| 270 | 2,2,1, 3,3,1, 3,3,0, undef,undef,undef, # 3* 136 | ||
| 271 | 3,3,2, 3,3,2, 0,1,1, undef,undef,undef, # 3* 140 | ||
| 272 | 3,3,0, 3,3,1, 2,2,1, undef,undef,undef, # 3* 144 | ||
| 273 | 2,3,3, 2,3,3, 1,1,0, undef,undef,undef, # 3* 148 | ||
| 274 | 1,2,2, 1,3,3, 0,3,3, undef,undef,undef, # 3* 152 | ||
| 275 | 0,1,1, 3,3,2, 3,3,2, undef,undef,undef, # 3* 156 | ||
| 276 | 0,3,3, 1,3,3, 1,2,2, undef,undef,undef, # 3* 160 | ||
| 277 | 1,1,0, 2,3,3, 2,3,3, undef,undef,undef, # 3* 164 | ||
| 278 | 2,2,1, 3,3,1, 3,3,0, undef,undef,undef, # 3* 168 | ||
| 279 | 3,3,2, 3,3,2, 0,1,1, undef,undef,undef, # 3* 172 | ||
| 280 | 3,3,0, 3,3,1, 2,2,1, undef,undef,undef, # 3* 176 | ||
| 281 | 2,3,3, 2,3,3, 1,1,0, undef,undef,undef, # 3* 180 | ||
| 282 | 1,2,2, 1,3,3, 0,3,3, undef,undef,undef, # 3* 184 | ||
| 283 | 0,1,1, 3,3,2, 3,3,2, undef,undef,undef, # 3* 188 | ||
| 284 | 0,3,3, 1,3,3, 1,2,2, undef,undef,undef, # 3* 192 | ||
| 285 | 1,1,0, 2,3,3, 2,3,3, undef,undef,undef, # 3* 196 | ||
| 286 | 2,2,1, 3,3,1, 3,3,0, undef,undef,undef, # 3* 200 | ||
| 287 | 3,3,2, 3,3,2, 0,1,1, undef,undef,undef, # 3* 204 | ||
| 288 | 3,3,0, 3,3,1, 2,2,1, undef,undef,undef, # 3* 208 | ||
| 289 | 2,3,3, 2,3,3, 1,1,0, undef,undef,undef, # 3* 212 | ||
| 290 | 1,2,2, 1,3,3, 0,3,3, undef,undef,undef, # 3* 216 | ||
| 291 | 0,1,1, 3,3,2, 3,3,2, undef,undef,undef, # 3* 220 | ||
| 292 | 0,3,3, 1,3,3, 1,2,2, undef,undef,undef, # 3* 224 | ||
| 293 | 1,1,0, 2,3,3, 2,3,3, undef,undef,undef, # 3* 228 | ||
| 294 | 2,2,1, 3,3,1, 3,3,0, undef,undef,undef, # 3* 232 | ||
| 295 | 3,3,2, 3,3,2, 0,1,1, undef,undef,undef, # 3* 236 | ||
| 296 | 3,3,0, 3,3,1, 2,2,1, undef,undef,undef, # 3* 240 | ||
| 297 | 2,3,3, 2,3,3, 1,1,0, undef,undef,undef, # 3* 244 | ||
| 298 | 1,2,2, 1,3,3, 0,3,3, undef,undef,undef, # 3* 248 | ||
| 299 | 0,1,1, 3,3,2, 3,3,2); | ||
| 300 | # state length 256 in each of 4 tables | ||
| 301 | # grand total 2554 | ||
| 302 | |||
| 303 | # cycle 0/224 part=A1 rot=0 digit=0 <-> part=D2 rot=0 digit=0 | ||
| 304 | # cycle 224/0 part=D2 rot=0 digit=0 <-> part=A1 rot=0 digit=0 | ||
| 305 | # cycle 56/220 part=A2rev rot=2 digit=0 <-> part=D1rev rot=3 digit=0 | ||
| 306 | # cycle 220/56 part=D1rev rot=3 digit=0 <-> part=A2rev rot=2 digit=0 | ||
| 307 | # cycle 92/96 part=B1rev rot=3 digit=0 <-> part=B2 rot=0 digit=0 | ||
| 308 | # cycle 96/92 part=B2 rot=0 digit=0 <-> part=B1rev rot=3 digit=0 | ||
| 309 | # | ||
| 310 | my %start_state = (A1 => [0, 224], | ||
| 311 | D2 => [224, 0], | ||
| 312 | |||
| 313 | B2 => [96, 92], | ||
| 314 | B1rev => [92, 96], | ||
| 315 | |||
| 316 | D1rev => [220, 56], | ||
| 317 | A2rev => [56, 220], | ||
| 318 | ); | ||
| 319 | |||
| 320 | sub new { | ||
| 321 | my $self = shift->SUPER::new (@_); | ||
| 322 | my $start_shape = ($self->{'start_shape'} ||= 'A1'); # default | ||
| 323 | $start_state{$start_shape} | ||
| 324 | || croak "Unrecognised start_shape option: ",$start_shape; | ||
| 325 | return $self; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub n_to_xy { | ||
| 329 | my ($self, $n) = @_; | ||
| 330 | ### AR2W2Curve n_to_xy(): $n | ||
| 331 | |||
| 332 | if ($n < 0) { return; } | ||
| 333 | if (is_infinite($n)) { return ($n,$n); } | ||
| 334 | |||
| 335 | my $int = int($n); | ||
| 336 | $n -= $int; | ||
| 337 | |||
| 338 | my @digits = digit_split_lowtohigh($int,4); | ||
| 339 | my $len = ($n*0 + 2) ** scalar(@digits); # inherit possible bigint | ||
| 340 | |||
| 341 | ### digits: join(', ',@digits)." count ".scalar(@digits) | ||
| 342 | ### $len | ||
| 343 | |||
| 344 | # $dir default if all $digit==3 | ||
| 345 | my ($state,$dir) = @{$start_state{$self->{'start_shape'}}}; | ||
| 346 | if ($#digits & 1) { | ||
| 347 | ($state,$dir) = ($dir,$state); | ||
| 348 | } | ||
| 349 | |||
| 350 | ### initial ... | ||
| 351 | ### $state | ||
| 352 | ### $dir | ||
| 353 | |||
| 354 | my $x = 0; | ||
| 355 | my $y = 0; | ||
| 356 | while (@digits) { | ||
| 357 | $len /= 2; | ||
| 358 | $state += (my $digit = pop @digits); # high to low digits | ||
| 359 | if ($digit != 3) { | ||
| 360 | $dir = $state; # lowest non-3 digit | ||
| 361 | } | ||
| 362 | |||
| 363 | ### $len | ||
| 364 | ### $state | ||
| 365 | ### state: state_string($state) | ||
| 366 | ### digit_to_x: $digit_to_x[$state] | ||
| 367 | ### digit_to_y: $digit_to_y[$state] | ||
| 368 | ### next_state: $next_state[$state] | ||
| 369 | |||
| 370 | $x += $len * $digit_to_x[$state]; | ||
| 371 | $y += $len * $digit_to_y[$state]; | ||
| 372 | $state = $next_state[$state]; | ||
| 373 | } | ||
| 374 | |||
| 375 | ### $dir | ||
| 376 | ### frac: $n | ||
| 377 | |||
| 378 | # with $n fractional part | ||
| 379 | return ($n * ($digit_to_x[$dir+1] - $digit_to_x[$dir]) + $x, | ||
| 380 | $n * ($digit_to_y[$dir+1] - $digit_to_y[$dir]) + $y); | ||
| 381 | } | ||
| 382 | |||
| 383 | sub xy_to_n { | ||
| 384 | my ($self, $x, $y) = @_; | ||
| 385 | ### AR2W2Curve xy_to_n(): "$x, $y" | ||
| 386 | |||
| 387 | $x = round_nearest ($x); | ||
| 388 | $y = round_nearest ($y); | ||
| 389 | if ($x < 0 || $y < 0) { | ||
| 390 | return undef; | ||
| 391 | } | ||
| 392 | if (is_infinite($x)) { | ||
| 393 | return $x; | ||
| 394 | } | ||
| 395 | if (is_infinite($y)) { | ||
| 396 | return $y; | ||
| 397 | } | ||
| 398 | |||
| 399 | my @xdigits = bit_split_lowtohigh ($x); | ||
| 400 | my @ydigits = bit_split_lowtohigh ($y); | ||
| 401 | my $level = max($#xdigits,$#ydigits); | ||
| 402 | my $state = $start_state{$self->{'start_shape'}}->[$level & 1]; | ||
| 403 | my @ndigits; | ||
| 404 | foreach my $i (reverse 0 .. max($#xdigits,$#ydigits)) { # high to low | ||
| 405 | my $ndigit = $yx_to_digit[$state | ||
| 406 | + 2*($ydigits[$i]||0) | ||
| 407 | + ($xdigits[$i]||0)]; | ||
| 408 | $ndigits[$i] = $ndigit; | ||
| 409 | $state = $next_state[$state+$ndigit]; | ||
| 410 | } | ||
| 411 | |||
| 412 | return digit_join_lowtohigh (\@ndigits, 4, | ||
| 413 | $x * 0 * $y); # bignum zero | ||
| 414 | } | ||
| 415 | |||
| 416 | # exact | ||
| 417 | sub rect_to_n_range { | ||
| 418 | my ($self, $x1,$y1, $x2,$y2) = @_; | ||
| 419 | ### AR2W2Curve rect_to_n_range(): "$x1,$y1, $x2,$y2" | ||
| 420 | |||
| 421 | $x1 = round_nearest ($x1); | ||
| 422 | $x2 = round_nearest ($x2); | ||
| 423 | $y1 = round_nearest ($y1); | ||
| 424 | $y2 = round_nearest ($y2); | ||
| 425 | ($x1,$x2) = ($x2,$x1) if $x1 > $x2; | ||
| 426 | ($y1,$y2) = ($y2,$y1) if $y1 > $y2; | ||
| 427 | |||
| 428 | if ($x2 < 0 || $y2 < 0) { | ||
| 429 | return (1, 0); | ||
| 430 | } | ||
| 431 | |||
| 432 | my ($len, $level) = round_down_pow (max($x2,$y2), 2); | ||
| 433 | ### len/level: "$len $level" | ||
| 434 | if (is_infinite($level)) { | ||
| 435 | return (0, $level); | ||
| 436 | } | ||
| 437 | |||
| 438 | # At this point an easy over-estimate would be | ||
| 439 | # return (0, 4*$len*$len-1); | ||
| 440 | |||
| 441 | |||
| 442 | my $n_min = my $n_max | ||
| 443 | = my $y_min = my $y_max | ||
| 444 | = my $x_min = my $x_max = 0; | ||
| 445 | my $min_state = my $max_state | ||
| 446 | = $start_state{$self->{'start_shape'}}->[$level & 1]; | ||
| 447 | ### $x_min | ||
| 448 | ### $y_min | ||
| 449 | |||
| 450 | while ($level >= 0) { | ||
| 451 | ### $level | ||
| 452 | ### $len | ||
| 453 | { | ||
| 454 | my $x_cmp = $x_min + $len; | ||
| 455 | my $y_cmp = $y_min + $len; | ||
| 456 | my $digit = $min_digit[3*$min_state | ||
| 457 | + ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0) | ||
| 458 | + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0)]; | ||
| 459 | |||
| 460 | # my $xr = ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0); | ||
| 461 | # my $yr = ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0); | ||
| 462 | # ### $min_state | ||
| 463 | # ### min_state: state_string($min_state) | ||
| 464 | # ### $xr | ||
| 465 | # ### $yr | ||
| 466 | # ### $digit | ||
| 467 | |||
| 468 | $n_min = 4*$n_min + $digit; | ||
| 469 | $min_state += $digit; | ||
| 470 | if ($digit_to_x[$min_state]) { $x_min += $len; } | ||
| 471 | if ($digit_to_y[$min_state]) { $y_min += $len; } | ||
| 472 | $min_state = $next_state[$min_state]; | ||
| 473 | } | ||
| 474 | { | ||
| 475 | my $x_cmp = $x_max + $len; | ||
| 476 | my $y_cmp = $y_max + $len; | ||
| 477 | my $digit = $max_digit[3*$max_state | ||
| 478 | + ($x1 >= $x_cmp ? 2 : $x2 >= $x_cmp ? 1 : 0) | ||
| 479 | + ($y1 >= $y_cmp ? 6 : $y2 >= $y_cmp ? 3 : 0)]; | ||
| 480 | |||
| 481 | $n_max = 4*$n_max + $digit; | ||
| 482 | $max_state += $digit; | ||
| 483 | if ($digit_to_x[$max_state]) { $x_max += $len; } | ||
| 484 | if ($digit_to_y[$max_state]) { $y_max += $len; } | ||
| 485 | $max_state = $next_state[$max_state]; | ||
| 486 | } | ||
| 487 | |||
| 488 | $len = int($len/2); | ||
| 489 | $level--; | ||
| 490 | } | ||
| 491 | |||
| 492 | return ($n_min, $n_max); | ||
| 493 | } | ||
| 494 | |||
| 495 | #------------------------------------------------------------------------------ | ||
| 496 | # levels | ||
| 497 | |||
| 498 | use Math::PlanePath::HilbertCurve; | ||
| 499 | *level_to_n_range = \&Math::PlanePath::HilbertCurve::level_to_n_range; | ||
| 500 | *n_to_level = \&Math::PlanePath::HilbertCurve::n_to_level; | ||
| 501 | |||
| 502 | #------------------------------------------------------------------------------ | ||
| 503 | 1; | ||
| 504 | __END__ |