File: numbers.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 (532 lines) | stat: -rwxr-xr-x 18,749 bytes parent folder | download
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#!/usr/bin/perl -w

# Copyright 2010, 2011, 2012, 2013, 2014 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 numbers.pl CLASS...
#        perl numbers.pl all
#
# Print the given path CLASS or classes as N numbers in a grid.  Eg.
#
#     perl numbers.pl SquareSpiral DiamondSpiral
#
# Parameters to the class can be given as
#
#     perl numbers.pl SquareSpiral,wider=4
#
# With option "all" print all classes and a selection of their parameters,
# per the table in the code below
#
#     perl numbers.pl all
#
# See square-numbers.pl for a simpler program designed just for the
# SquareSpiral.  The code here tries to adapt itself to the tty width and
# stops when the width of the numbers to be displayed would be wider than
# the tty.
#
# Stopping when N goes outside the tty means that just the first say 99 or
# so N values will be shown.  There's often other bigger N within the X,Y
# grid region, but the first few N show how the path begins, without
# clogging up the output.
#
# The origin 0,0 is kept in the middle of the output, horizontally, to help
# see how much is on each side and to make multiple paths printed line up
# such as the "all" option.  Vertically only as many rows as necessary are
# printed.
#
# Paths with fractional X,Y positions like SacksSpiral or VogelFloret are
# rounded to character positions.  There's some hard-coded fudge factors to
# try to make them come out nicely.
#
# When an X,Y position is visited more than once multiple N's are shown with
# a comma like "9,24".  This can happen for example in the DragonCurve where
# points are visited twice, or when rounding gives the same X,Y for a few
# initial points such as in KochSquareflakes.
#

use 5.004;
use strict;
use POSIX ();
use List::Util 'min', 'max';

my $width = 79;
my $height = 23;

# use Term::Size if available
# chars() can return 0 for unknown size, ignore that
if (eval { require Term::Size }) {
  my ($term_width, $term_height) = Term::Size::chars();
  if ($term_width)  { $width = $term_width - 1; }
  if ($term_height) { $height = $term_height - 1; }
}

if (! @ARGV) {
  push @ARGV, 'HexSpiral';  # default class to print if no args
}

my @all_classes = ('SquareSpiral',
                   'SquareSpiral,wider=9',
                   'DiamondSpiral',
                   'PentSpiral',
                   'PentSpiralSkewed',
                   'HexSpiral',
                   'HexSpiral,wider=3',
                   'HexSpiralSkewed',
                   'HexSpiralSkewed,wider=5',
                   'HeptSpiralSkewed',
                   'AnvilSpiral',
                   'AnvilSpiral,wider=3',
                   'OctagramSpiral',

                   'PyramidSpiral',
                   'PyramidRows',
                   'PyramidRows,step=5',
                   'PyramidRows,align=right',
                   'PyramidRows,align=left,step=4',

                   'PyramidSides',
                   'CellularRule,rule=30',
                   'CellularRule,rule=73',
                   'CellularRule54',
                   'CellularRule57',
                   'CellularRule57,mirror=1',
                   'CellularRule190',
                   'CellularRule190,mirror=1',
                   'TriangleSpiral',
                   'TriangleSpiralSkewed',
                   'TriangleSpiralSkewed,skew=right',
                   'TriangleSpiralSkewed,skew=up',
                   'TriangleSpiralSkewed,skew=down',

                   'Diagonals',
                   'Diagonals,direction=up',
                   'DiagonalsAlternating',
                   'DiagonalsOctant',
                   'DiagonalsOctant,direction=up',
                   'Staircase',
                   'StaircaseAlternating',
                   'StaircaseAlternating,end_type=square',
                   'Corner',
                   'Corner,wider=5',
                   'KnightSpiral',
                   'CretanLabyrinth',

                   'SquareArms',
                   'DiamondArms',
                   'HexArms',
                   'GreekKeySpiral',
                   'GreekKeySpiral,turns=4',
                   'GreekKeySpiral,turns=1',

                   'AztecDiamondRings',
                   'MPeaks',

                   'SacksSpiral',
                   'VogelFloret',
                   'ArchimedeanChords',
                   'TheodorusSpiral',
                   'MultipleRings',
                   'MultipleRings,step=14',
                   'PixelRings',
                   'FilledRings',
                   'Hypot',
                   'Hypot,points=even',
                   'Hypot,points=odd',
                   'HypotOctant',
                   'HypotOctant,points=even',
                   'HypotOctant,points=odd',

                   'TriangularHypot',
                   'TriangularHypot,points=odd',
                   'TriangularHypot,points=all',
                   'TriangularHypot,points=hex',
                   'TriangularHypot,points=hex_rotated',
                   'TriangularHypot,points=hex_centred',

                   'Rows',
                   'Columns',
                   'UlamWarburton',
                   'UlamWarburton,parts=2',
                   'UlamWarburton,parts=1',
                   'UlamWarburton,parts=octant',
                   'UlamWarburton,parts=octant_up',
                   'UlamWarburtonQuarter',
                   'UlamWarburtonQuarter,parts=octant',
                   'UlamWarburtonQuarter,parts=octant_up',

                   'PeanoCurve',
                   'PeanoCurve,radix=5',
                   'WunderlichSerpentine',
                   'WunderlichSerpentine,serpentine_type=coil',
                   'WunderlichSerpentine,radix=5,serpentine_type=01001_01110_01000_11111_00010',
                   'WunderlichMeander',

                   'HilbertCurve',
                   'HilbertSpiral',

                   'ZOrderCurve',
                   'ZOrderCurve,radix=5',

                   'GrayCode',
                   'GrayCode,apply_type=Ts',
                   'GrayCode,radix=4',

                   'BetaOmega',
                   'AR2W2Curve',
                   'AR2W2Curve,start_shape=D2',
                   'AR2W2Curve,start_shape=B2',
                   'AR2W2Curve,start_shape=B1rev',
                   'AR2W2Curve,start_shape=D1rev',
                   'AR2W2Curve,start_shape=A2rev',
                   'KochelCurve',
                   'DekkingCurve',
                   'DekkingCentres',
                   'CincoCurve',

                   'ImaginaryBase',
                   'ImaginaryBase,radix=4',

                   'ImaginaryHalf',
                   'ImaginaryHalf,radix=4',
                   'ImaginaryHalf,digit_order=XXY',
                   'ImaginaryHalf,digit_order=YXX',
                   'ImaginaryHalf,digit_order=XnXY',
                   'ImaginaryHalf,digit_order=XnYX',
                   'ImaginaryHalf,digit_order=YXnX',

                   'CubicBase',
                   'CubicBase,radix=4',
                   'SquareReplicate',
                   'CornerReplicate',
                   'LTiling',
                   'LTiling,L_fill=ends',
                   'LTiling,L_fill=all',
                   'DigitGroups',
                   'FibonacciWordFractal',

                   'Flowsnake',
                   'Flowsnake,arms=3',
                   'FlowsnakeCentres',
                   'FlowsnakeCentres,arms=3',
                   'GosperReplicate',
                   'GosperIslands',
                   'GosperSide',

                   'QuintetCurve',
                   'QuintetCurve,arms=4',
                   'QuintetCentres',
                   'QuintetReplicate',

                   'KochCurve',
                   'KochPeaks',
                   'KochSnowflakes',
                   'KochSquareflakes',
                   'KochSquareflakes,inward=1',
                   'QuadricCurve',
                   'QuadricIslands',

                   'SierpinskiCurve',
                   'SierpinskiCurve,arms=8',
                   'SierpinskiCurveStair',
                   'SierpinskiCurveStair,arms=2',
                   'SierpinskiCurveStair,diagonal_length=4',
                   'HIndexing',

                   'SierpinskiTriangle',
                   'SierpinskiTriangle,align=right',
                   'SierpinskiTriangle,align=left',
                   'SierpinskiTriangle,align=diagonal',

                   'SierpinskiArrowhead',
                   'SierpinskiArrowhead,align=right',
                   'SierpinskiArrowhead,align=left',
                   'SierpinskiArrowhead,align=diagonal',

                   'SierpinskiArrowheadCentres',
                   'SierpinskiArrowheadCentres,align=right',
                   'SierpinskiArrowheadCentres,align=left',
                   'SierpinskiArrowheadCentres,align=diagonal',

                   'DragonCurve',
                   'DragonCurve,arms=4',
                   'DragonRounded',
                   'DragonRounded,arms=4',
                   'DragonMidpoint',
                   'DragonMidpoint,arms=2',
                   'DragonMidpoint,arms=3',
                   'DragonMidpoint,arms=4',
                   'AlternatePaper',
                   'AlternatePaper,arms=2',
                   'AlternatePaper,arms=8',
                   'AlternatePaperMidpoint',
                   'AlternatePaperMidpoint,arms=2',
                   'AlternatePaperMidpoint,arms=8',
                   'CCurve',
                   'TerdragonCurve',
                   'TerdragonCurve,arms=6',
                   'TerdragonRounded',
                   'TerdragonRounded,arms=6',
                   'TerdragonMidpoint',
                   'TerdragonMidpoint,arms=6',
                   'R5DragonCurve',
                   'R5DragonCurve,arms=4',
                   'R5DragonMidpoint',
                   'R5DragonMidpoint,arms=2',
                   'R5DragonMidpoint,arms=3',
                   'R5DragonMidpoint,arms=4',
                   'ComplexPlus',
                   'ComplexPlus,realpart=2',
                   'ComplexMinus',
                   'ComplexMinus,realpart=2',
                   'ComplexRevolving',

                   'PythagoreanTree,tree_type=UAD',
                   'PythagoreanTree,tree_type=UAD,coordinates=AC',
                   'PythagoreanTree,tree_type=UAD,coordinates=BC',
                   'PythagoreanTree,tree_type=UAD,coordinates=PQ',
                   'PythagoreanTree,tree_type=UAD,coordinates=SM',
                   'PythagoreanTree,tree_type=UAD,coordinates=SC',
                   'PythagoreanTree,tree_type=UAD,coordinates=MC',
                   'PythagoreanTree,tree_type=FB',
                   'PythagoreanTree,tree_type=FB,coordinates=AC',
                   'PythagoreanTree,tree_type=FB,coordinates=BC',
                   'PythagoreanTree,tree_type=FB,coordinates=PQ',
                   'PythagoreanTree,tree_type=FB,coordinates=SM',
                   'PythagoreanTree,tree_type=FB,coordinates=SC',
                   'PythagoreanTree,tree_type=FB,coordinates=MC',
                   'PythagoreanTree,tree_type=UMT',
                   'PythagoreanTree,tree_type=UMT,coordinates=AC',
                   'PythagoreanTree,tree_type=UMT,coordinates=BC',
                   'PythagoreanTree,tree_type=UMT,coordinates=PQ',
                   'PythagoreanTree,tree_type=UMT,coordinates=SM',
                   'PythagoreanTree,tree_type=UMT,coordinates=SC',
                   'PythagoreanTree,tree_type=UMT,coordinates=MC',

                   'DiagonalRationals',
                   'DiagonalRationals,direction=up',
                   'CoprimeColumns',
                   'FactorRationals',
                   'GcdRationals',
                   'GcdRationals,pairs_order=rows_reverse',
                   'GcdRationals,pairs_order=diagonals_down',
                   'GcdRationals,pairs_order=diagonals_up',
                   'RationalsTree,tree_type=SB',
                   'RationalsTree,tree_type=CW',
                   'RationalsTree,tree_type=AYT',
                   'RationalsTree,tree_type=HCS',
                   'RationalsTree,tree_type=Bird',
                   'RationalsTree,tree_type=Drib',
                   'RationalsTree,tree_type=L',
                   'FractionsTree',
                   'ChanTree',
                   'ChanTree,k=4',
                   'ChanTree,k=5',
                   'ChanTree,k=7',
                   'ChanTree,k=8',

                   'CfracDigits',
                   'CfracDigits,radix=3',
                   'CfracDigits,radix=4',
                   'CfracDigits,radix=1',

                   'DivisibleColumns',
                   'DivisibleColumns,divisor_type=proper',

                   'WythoffArray',
                   'WythoffPreliminaryTriangle',
                   'PowerArray',
                   'PowerArray,radix=3',
                   'PowerArray,radix=4',

                   # in separate Math-PlanePath-Toothpick

                   '*ToothpickTree',
                   '*ToothpickTree,parts=3',
                   '*ToothpickTree,parts=2',
                   '*ToothpickTree,parts=1',
                   '*ToothpickTree,parts=octant',
                   '*ToothpickTree,parts=octant_up',
                   '*ToothpickTree,parts=wedge',
                   
                   '*ToothpickReplicate',
                   '*ToothpickReplicate,parts=3',
                   '*ToothpickReplicate,parts=2',
                   '*ToothpickReplicate,parts=1',

                   '*ToothpickUpist',
                   '*ToothpickSpiral',
                   
                   '*LCornerTree',
                   '*LCornerTree,parts=3',
                   '*LCornerTree,parts=2',
                   '*LCornerTree,parts=1',
                   '*LCornerTree,parts=octant',
                   '*LCornerTree,parts=octant+1',
                   '*LCornerTree,parts=octant_up',
                   '*LCornerTree,parts=octant_up+1',
                   '*LCornerTree,parts=wedge',
                   '*LCornerTree,parts=wedge+1',
                   '*LCornerTree,parts=diagonal',
                   '*LCornerTree,parts=diagonal-1',
                   
                   '*LCornerReplicate',

                   '*OneOfEight',
                   '*OneOfEight,parts=4',
                   '*OneOfEight,parts=1',
                   '*OneOfEight,parts=octant',
                   '*OneOfEight,parts=octant_up',
                   '*OneOfEight,parts=3mid',
                   '*OneOfEight,parts=3side',
                   '*OneOfEight,parts=wedge',

                   '*HTree',
                  );
# expand arg "all" to full list
@ARGV = map {$_ eq 'all' ? @all_classes : $_} @ARGV;

my $separator = '';
foreach my $class (@ARGV) {
  print $separator;
  $separator = "\n";

  print_class ($class);
}

sub print_class {
  my ($name) = @_;

  # secret leading "*Foo" means print if available
  my $if_available = ($name =~ s/^\*//);

  my $class = $name;
  unless ($class =~ /::/) {
    $class = "Math::PlanePath::$class";
  }
  ($class, my @parameters) = split /\s*,\s*/, $class;

  $class =~ /^[a-z_][:a-z_0-9]*$/i or die "Bad class name: $class";
  if (! eval "require $class") {
    if ($if_available) {
      next;
    } else {
      die $@;
    }
  }

  @parameters = map { /(.*?)=(.*)/ or die "Missing value for parameter \"$_\"";
                      $1,$2 } @parameters;

  my %rows;
  my $x_min = 0;
  my $x_max = 0;
  my $y_min = 0;
  my $y_max = 0;
  my $cellwidth = 1;

  my $path = $class->new (width  => POSIX::ceil($width / 4),
                          height => POSIX::ceil($height / 2),
                          @parameters);
  my $x_limit_lo;
  my $x_limit_hi;
  if ($path->x_negative) {
    my $w_cells = int ($width / $cellwidth);
    my $half = int(($w_cells - 1) / 2);
    $x_limit_lo = -$half;
    $x_limit_hi = +$half;
  } else {
    my $w_cells = int ($width / $cellwidth);
    $x_limit_lo = 0;
    $x_limit_hi = $w_cells - 1;
  }

  my $y_limit_lo = 0;
  my $y_limit_hi = $height-1;
  if ($path->y_negative) {
    my $half = int(($height-1)/2);
    $y_limit_lo = -$half;
    $y_limit_hi = +$half;
  }

  my $n_start = $path->n_start;
  my $n = $n_start;
  for ($n = $n_start; $n <= 999; $n++) {
    my ($x, $y) = $path->n_to_xy ($n);

    # stretch these out for better resolution
    if ($class =~ /Sacks/) { $x *= 1.5; $y *= 2; }
    if ($class =~ /Archimedean/) { $x *= 2; $y *= 3; }
    if ($class =~ /Theodorus|MultipleRings/) { $x *= 2; $y *= 2; }
    if ($class =~ /Vogel/) { $x *= 2; $y *= 3.5; }

    # nearest integers
    $x = POSIX::floor ($x + 0.5);
    $y = POSIX::floor ($y + 0.5);

    my $cell = $rows{$x}{$y};
    if (defined $cell) { $cell .= ','; }
    $cell .= $n;
    my $new_cellwidth = max ($cellwidth, length($cell) + 1);

    my $new_x_limit_lo;
    my $new_x_limit_hi;
    if ($path->x_negative) {
      my $w_cells = int ($width / $new_cellwidth);
      my $half = int(($w_cells - 1) / 2);
      $new_x_limit_lo = -$half;
      $new_x_limit_hi = +$half;
    } else {
      my $w_cells = int ($width / $new_cellwidth);
      $new_x_limit_lo = 0;
      $new_x_limit_hi = $w_cells - 1;
    }

    my $new_x_min = min($x_min, $x);
    my $new_x_max = max($x_max, $x);
    my $new_y_min = min($y_min, $y);
    my $new_y_max = max($y_max, $y);
    if ($new_x_min < $new_x_limit_lo
        || $new_x_max > $new_x_limit_hi
        || $new_y_min < $y_limit_lo
        || $new_y_max > $y_limit_hi) {
      last;
    }

    $rows{$x}{$y} = $cell;
    $cellwidth = $new_cellwidth;
    $x_limit_lo = $new_x_limit_lo;
    $x_limit_hi = $new_x_limit_hi;
    $x_min = $new_x_min;
    $x_max = $new_x_max;
    $y_min = $new_y_min;
    $y_max = $new_y_max;
  }
  $n--; # the last N actually plotted

  print "$name   N=$n_start to N=$n\n\n";
  foreach my $y (reverse $y_min .. $y_max) {
    foreach my $x ($x_limit_lo .. $x_limit_hi) {
      my $cell = $rows{$x}{$y};
      if (! defined $cell) { $cell = ''; }
      printf ('%*s', $cellwidth, $cell);
    }
    print "\n";
  }
}

exit 0;