Hilbert curve

From Rosetta Code
Revision as of 22:43, 24 April 2018 by Thundergnat (talk | contribs) (→‎{{header|Perl 6}}: New draft task and add Perl6 example)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Hilbert curve is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


Task

Produce a graphical or ASCII-art representation of a Hilbert curve of at least order 3.

Perl 6

Works with: Rakudo version 2018.03

<lang perl6>use SVG;

role Lindenmayer {

   has %.rules;
   method succ {

self.comb.map( { %!rules{$^c} // $c } ).join but Lindenmayer(%!rules)

   }

}

my $hilbert = 'A' but Lindenmayer( { A => '-BF+AFA+FB-', B => '+AF-BFB-FA+' } );

$hilbert++ xx 7; my @points = (647, 13);

for $hilbert.comb -> $v {

   state ($x, $y) = @points[0,1];
   state $d = -5 - 0i;
   with $v {
       when 'F' { @points.append: ($x += $d.re).round(.01), ($y += $d.im).round(.01) }
       when /< + - >/ { $d *= "{$v}1i" }
       default { }
   }

}

say SVG.serialize(

   svg => [
       width => 660, height => 660, style => 'stroke:rgb(0,0,198)',
       :rect[:width<100%>, :height<100%>, :fill<white>],
       :polyline[ :points(@points.join: ','), :fill<white> ],
   ],

);</lang>

See: Hilbert curve