| @@ 114-138 (lines=25) @@ | ||
| 111 | return $position; |
|
| 112 | } |
|
| 113 | ||
| 114 | function hilbert_to_point($hilbert, $order) { |
|
| 115 | $current_square = "a"; |
|
| 116 | $amount = 1 << $order - 1; |
|
| 117 | $x = $y = 0; |
|
| 118 | for ($i = 2 * $order; $i > 0; $i -= 2) { |
|
| 119 | list ($position, $current_square) = $this->rev_map[$current_square][$hilbert >> $i - 2]; |
|
| 120 | switch ($position) { |
|
| 121 | case 1: |
|
| 122 | $x += $amount; |
|
| 123 | break; |
|
| 124 | case 2: |
|
| 125 | $y += $amount; |
|
| 126 | break; |
|
| 127 | case 3: |
|
| 128 | $y += $amount; |
|
| 129 | $x += $amount; |
|
| 130 | break; |
|
| 131 | default: |
|
| 132 | break; |
|
| 133 | } |
|
| 134 | $amount /= 2; |
|
| 135 | $hilbert &= (1 << ($i - 2)) - 1; |
|
| 136 | } |
|
| 137 | return [$x, $y]; |
|
| 138 | } |
|
| 139 | ||
| 140 | ||
| 141 | function xy2d($x, $y, $order = 16) { |
|
| @@ 156-180 (lines=25) @@ | ||
| 153 | return $position; |
|
| 154 | } |
|
| 155 | ||
| 156 | function d2xy($hilbert, $order = 16) { |
|
| 157 | $current_square = "a"; |
|
| 158 | $amount = 1 << $order - 1; |
|
| 159 | $x = $y = 0; |
|
| 160 | for ($i = 2 * $order; $i > 0; $i -= 2) { |
|
| 161 | list ($position, $current_square) = $this->rev_map[$current_square][$hilbert >> $i - 2]; |
|
| 162 | switch ($position) { |
|
| 163 | case 1: |
|
| 164 | $x += $amount; |
|
| 165 | break; |
|
| 166 | case 2: |
|
| 167 | $y += $amount; |
|
| 168 | break; |
|
| 169 | case 3: |
|
| 170 | $y += $amount; |
|
| 171 | $x += $amount; |
|
| 172 | break; |
|
| 173 | default: |
|
| 174 | break; |
|
| 175 | } |
|
| 176 | $amount /= 2; |
|
| 177 | $hilbert &= (1 << ($i - 2)) - 1; |
|
| 178 | } |
|
| 179 | return [$x, $y]; |
|
| 180 | } |
|
| 181 | ||
| 182 | function test_pth() { |
|
| 183 | foreach (range(7, 0, -1) as $x) { |
|