File: dragon-curve-table.pl

package info (click to toggle)
libmath-planepath-perl 117-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,988 kB
  • ctags: 5,587
  • sloc: perl: 99,131; ansic: 299; sh: 233; lisp: 73; makefile: 4
file content (274 lines) | stat: -rw-r--r-- 6,555 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/perl -w

# Copyright 2012 Kevin Ryde

# This file is part of Math-PlanePath.
#
# Math-PlanePath is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-PlanePath is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-PlanePath.  If not, see <http://www.gnu.org/licenses/>.


# Usage: perl dragon-curve-table.pl
#
# Print the state tables used for DragonCurve n_to_xy().


use 5.010;
use strict;
use List::Util 'max';

# uncomment this to run the ### lines
#use Smart::Comments;


sub print_table {
  my ($name, $aref) = @_;
  print "my \@$name = (";
  my $entry_width = max (map {length($_//'')} @$aref);

  foreach my $i (0 .. $#$aref) {
    printf "%*s", $entry_width, $aref->[$i]//'undef';
    if ($i == $#$aref) {
      print ");\n";
    } else {
      print ",";
      if (($i % 16) == 15
          || ($entry_width >= 3 && ($i % 4) == 3)) {
        print "\n        ".(" " x length($name));
      } elsif (($i % 4) == 3) {
        print " ";
      }
    }
  }
}

  my @next_state;
my @digit_to_x;
my @digit_to_y;
my @digit_to_dxdy;

sub make_state {
  my %param = @_;
  my $state = 0;
  $state <<= 1; $state |= delete $param{'rev'};
  $state <<= 2; $state |= delete $param{'rot'};
  $state <<= 2; $state |= delete $param{'digit'};
  return $state;
}
sub state_string {
  my ($state) = @_;
  my $digit = $state & 3;  $state >>= 2;
  my $rot = $state & 3;  $state >>= 2;
  my $rev = $state & 1;  $state >>= 1;
  return "rot=$rot  rev=$rev (digit=$digit)";
}

foreach my $rot (0 .. 3) {
  foreach my $rev (0, 1) {
    foreach my $digit (0, 1, 2, 3) {
      my $state = make_state (rot => $rot, rev => $rev, digit => $digit);

      my $new_rev;
      my $new_rot = $rot;

      my $x;
      my $y;
      if ($rev) {
        #
        #      2<--3
        #      ^   |
        #      |   v
        #  0<--1   *
        #
        if ($digit == 0) {
          $x = 0;
          $y = 0;
          $new_rev = 0;
        } elsif ($digit == 1) {
          $x = 1;
          $y = 0;
          $new_rev = 1;
          $new_rot++;
        } elsif ($digit == 2) {
          $x = 1;
          $y = 1;
          $new_rev = 0;
        } elsif ($digit == 3) {
          $x = 2;
          $y = 1;
          $new_rev = 1;
          $new_rot--;
        }
      } else {
        #
        #  0   3<--*
        #  |   ^
        #  v   |
        #  1<--2
        #
        if ($digit == 0) {
          $x = 0;
          $y = 0;
          $new_rev = 0;
          $new_rot--;
        } elsif ($digit == 1) {
          $x = 0;
          $y = -1;
          $new_rev = 1;
        } elsif ($digit == 2) {
          $x = 1;
          $y = -1;
          $new_rev = 0;
          $new_rot++;
        } elsif ($digit == 3) {
          $x = 1;
          $y = 0;
          $new_rev = 1;
        }
      }
      $new_rot &= 3;

      my $dx = 1;
      my $dy = 0;

      if ($rot & 2) {
        $x = -$x;
        $y = -$y;
        $dx = -$dx;
        $dy = -$dy;
      }
      if ($rot & 1) {
        ($x,$y) = (-$y,$x); # rotate +90
        ($dx,$dy) = (-$dy,$dx); # rotate +90
      }
      ### rot to: "$x, $y"

      my $next_dx = $x;
      my $next_dy = $y;
      $digit_to_x[$state] = $x;
      $digit_to_y[$state] = $y;

      if ($digit == 0) {
        $digit_to_dxdy[$state] = $dx;
        $digit_to_dxdy[$state+1] = $dy;
      }

      my $next_state = make_state
        (rot   => $new_rot,
         rev   => $new_rev,
         digit => 0);
      $next_state[$state] = $next_state;
    }
  }
}


### @next_state
### next_state length: 4*(4*2*2 + 4*2)

print "# next_state length ", scalar(@next_state), "\n";
print_table ("next_state", \@next_state);
print_table ("digit_to_x", \@digit_to_x);
print_table ("digit_to_y", \@digit_to_y);
print_table ("digit_to_dxdy", \@digit_to_dxdy);
print "\n";

# {
#  DIGIT: foreach my $digit (0 .. 3) {
#     foreach my $rot (0 .. 3) {
#       foreach my $rev (0 .. 1) {
#         if ($digit_to_x[make_state(rot => $rot,
#                                    rev => $rev,
#                                    digit => $digit)]
#             != $digit_to_dxdy[make_state(rot => $rot,
#                                          rev => $rev,
#                                          digit => 0)]) {
#           print "digit=$digit dx different at rot=$rot rev=$rev\n";
#           next DIGIT;
#         }
#       }
#     }
#     print "digit=$digit digit_to_x[] is dx\n";
#   }
# }

{
  my @pending_state = (0, 4, 8, 12);  # in 4 arm directions
  my $count = 0;
  my @seen_state;
  my $depth = 1;
  foreach my $state (@pending_state) {
    $seen_state[$state] = $depth;
  }
  while (@pending_state) {
    my @new_pending_state;
    foreach my $state (@pending_state) {
      $count++;
      ### consider state: $state

      foreach my $digit (0 .. 1) {
        my $next_state = $next_state[$state+$digit];
        if (! $seen_state[$next_state]) {
          $seen_state[$next_state] = $depth;
          push @new_pending_state, $next_state;
          ### push: "$next_state  depth $depth"
        }
      }
      $depth++;
    }
    @pending_state = @new_pending_state;
  }
  for (my $state = 0; $state < @next_state; $state += 2) {
    $seen_state[$state] ||= '-';
    my $state_string = state_string($state);
    print "# used state $state   depth $seen_state[$state]  $state_string\n";
  }
  print "used state count $count\n";
}


use Math::PlanePath::Base::Digits
  'digit_split_lowtohigh',
  'digit_join_lowtohigh';

foreach my $int (0 .. 16) {
  ### $int

  my @digits = digit_split_lowtohigh($int,4);
  my $len = 2 ** $#digits;
  my $state = (scalar(@digits) & 3) << 2;
  ### @digits
  ### $len
  ### initial state: $state.' '.state_string($state)

  my $x = 0;
  my $y = 0;
  foreach my $i (reverse 0 .. $#digits) {
    ### at: "i=$i len=$len digit=$digits[$i] state=$state ".state_string($state)
    $state += $digits[$i];
    ### digit x: $digit_to_x[$state]
    ### digit y: $digit_to_y[$state]
    $x += $len * $digit_to_x[$state];
    $y += $len * $digit_to_y[$state];
    $state = $next_state[$state];
    $len /= 2;
  }

  ### $x
  ### $y
  print "$int  $x $y\n";
}

exit 0;

__END__