How to resolve the algorithm Sierpinski pentagon step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sierpinski pentagon step by step in the Raku programming language

Table of Contents

Problem Statement

Produce a graphical or ASCII-art representation of a Sierpinski pentagon (aka a Pentaflake) of order 5. Your code should also be able to correctly generate representations of lower orders: 1 to 4.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sierpinski pentagon step by step in the Raku programming language

Source code in the raku programming language

constant $sides = 5;
constant order  = 5;
constant $dim   = 250;
constant scaling-factor = ( 3 - 5**.5 ) / 2;
my @orders = ((1 - scaling-factor) * $dim) «*» scaling-factor «**» (^order);

my $fh = open('sierpinski_pentagon.svg', :w);

$fh.say: qq||;

my @vertices = map { cis( $_ * τ / $sides ) }, ^$sides;

for 0 ..^ $sides ** order -> $i {
   my $vector = [+] @vertices[$i.base($sides).fmt("%{order}d").comb] «*» @orders;
   $fh.say: pgon ((@orders[*-1] * (1 - scaling-factor)) «*» @vertices «+» $vector)».reals».fmt("%0.3f");
};

sub pgon (@q) { qq|| }

$fh.say: '';
$fh.close;


  

You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Janet programming language
You may also check:How to resolve the algorithm Higher-order functions step by step in the МК-61/52 programming language
You may also check:How to resolve the algorithm S-expressions step by step in the Racket programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Scala programming language