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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sierpinski pentagon step by step in the zkl 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 zkl programming language

Source code in the zkl programming language

const order=5, sides=5, dim=250, scaleFactor=((3.0 - (5.0).pow(0.5))/2);
const tau=(0.0).pi*2; // 2*pi*r
orders:=order.pump(List,fcn(n){ (1.0 - scaleFactor)*dim*scaleFactor.pow(n) });

println(
#<<<
0'|

    "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">

    version="1.1" xmlns="http://www.w3.org/2000/svg">|
#<<<
   .fmt(dim*2,dim*2,dim,dim));

vertices:=sides.pump(List,fcn(s){ (1.0).toRectangular(tau*s/sides) }); // points on unit circle
vx:=vertices.apply('wrap([(a,b)]v,x){ return(a*x,b*x) },  // scaled points
		orders[-1]*(1.0 - scaleFactor));
fmt:="%%0%d.%dB".fmt(sides,order).fmt; //-->%05.5B (leading zeros, 5 places, base 5)
sides.pow(order).pump(Console.println,'wrap(i){
   vector:=fmt(i).pump(List,vertices.get)  // "00012"-->(vertices[0],..,vertices[2])
     .zipWith(fcn([(a,b)]v,x){ return(a*x,b*x) },orders) // ((a,b)...)*x -->((ax,bx)...)
     .reduce(fcn(vsum,v){ vsum[0]+=v[0]; vsum[1]+=v[1]; vsum },L(0.0, 0.0)); //-->(x,y)
   pgon(vx.apply(fcn([(a,b)]v,c,d){ return(a+c,b+d) },vector.xplode()));
});
println("");  // 3,131 lines

fcn pgon(vertices){  // eg ( ((250,0),(248.595,1.93317),...), len 5
   0'||.fmt(
       vertices.pump(String,fcn(v){ "%.3f %.3f ".fmt(v.xplode()) }) )
}

  

You may also check:How to resolve the algorithm Bell numbers step by step in the Lua programming language
You may also check:How to resolve the algorithm Greatest subsequential sum step by step in the Crystal programming language
You may also check:How to resolve the algorithm Show ASCII table step by step in the Nanoquery programming language
You may also check:How to resolve the algorithm Show the epoch step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Define a primitive data type step by step in the UNIX Shell programming language