File: hilbert-path.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 (108 lines) | stat: -rwxr-xr-x 2,586 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
#!/usr/bin/perl -w

# Copyright 2010, 2011, 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 hilbert-lines.pl
#
# This is a bit of fun printing the HilbertCurve path in ascii.  It follows
# the terminal width if you've got Term::Size, otherwise 79x23.
#
# Enough curve is drawn to fill the whole output size, clipped when the path
# goes outside the output bounds.  You could instead stop at say
#
#     $n_hi = 2**6;
#
# to see just a square portion of the curve.
#
# The $scale variable spaces out the points.  3 apart is good, or tighten it
# up to 2 to fit more on the screen.
#
# The output has Y increasing down the screen.  It could be instead printed
# up the screen in the final output by going $y from $height-1 down to 0.
#

use 5.004;
use strict;
use Math::PlanePath::HilbertCurve;

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

if (eval { require Term::Size }) {
  my ($w, $h) = Term::Size::chars();
  if ($w) { $width = $w - 1; }
  if ($h) { $height = $h - 1; }
}

my $x = 0;
my $y = 0;
my %grid;

# write $char at $x,$y in %grid
sub plot {
  my ($char) = @_;
  if ($x < $width && $y < $height) {
    $grid{$x}{$y} = $char;
  }
}

# at the origin 0,0
plot('+');

my $path = Math::PlanePath::HilbertCurve->new;
my $path_width = int($width / $scale) + 1;
my $path_height = int($height / $scale) + 1;
my ($n_lo, $n_hi) = $path->rect_to_n_range (0,0, $path_width,$path_height);

foreach my $n (1 .. $n_hi) {

  my ($next_x, $next_y) = $path->n_to_xy ($n);
  $next_x *= $scale;
  $next_y *= $scale;

  while ($x > $next_x) {  # draw to left
    $x--;
    plot ('-');
  }
  while ($x < $next_x) {  # draw to right
    $x++;
    plot ('-');
  }

  while ($y > $next_y) {  # draw up
    $y--;
    plot ('|');
  }
  while ($y < $next_y) {  # draw down
    $y++;
    plot ('|');
  }

  plot ('+');
}

foreach my $y (0 .. $height-1) {
  foreach my $x (0 .. $width-1) {
    print $grid{$x}{$y} || ' ';
  }
  print "\n";
}

exit 0;