Sierpinski arrowhead curve

From Rosetta Code
Revision as of 21:15, 1 March 2020 by Thundergnat (talk | contribs) (Start as a draft)
Sierpinski arrowhead 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 Sierpinski arrowhead curve of at least order 3.


Perl 6

Works with: Rakudo version 2020.02

<lang perl6>use SVG;

role Lindenmayer {

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

}

my $arrow = 'X' but Lindenmayer( { X => 'YF+XF+Y', Y => 'XF-YF-X' } );

$arrow++ xx 7;

my $w = 800; my $h = ($w * 3**.5 / 2).round(1);

my $scale = 6; my @points = (400, 15); my $dir = pi/3;

for $arrow.comb {

   state ($x, $y) = @points[0,1];
   state $d = $dir;
   when 'F' { @points.append: ($x += $scale * $d.cis.re).round(1), ($y += $scale * $d.cis.im).round(1) }
   when '+' { $d += $dir }
   when '-' { $d -= $dir }
   default { }

}

my $out = './sierpinski-arrowhead-curve-perl6.svg'.IO;

$out.spurt: SVG.serialize(

   svg => [
       :width($w), :height($h),
       :rect[:width<100%>, :height<100%>, :fill<black>],
       :polyline[ :points(@points.join: ','), :fill<black>, :style<stroke:#FF4EA9> ],
   ],

);</lang> See: Sierpinski-arrowhead-curve-perl6.svg (offsite SVG image)