Sierpinski pentagon: Difference between revisions

Content deleted Content added
Trizen (talk | contribs)
m →‎{{header|Sidef}}: modified the code to work with Sidef 2.30
Line 265: Line 265:


say '</svg>'</lang>
say '</svg>'</lang>

=={{header|zkl}}==
{{trans|Perl 6}}
<lang zkl>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'|<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg height="%d" width="%d" style="fill:blue" transform="translate(%d,%d) rotate(-18)"
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("</svg>"); // 3,131 lines

fcn pgon(vertices){ // eg ( ((250,0),(248.595,1.93317),...), len 5
0'|<polygon points="%s"/>|.fmt(
vertices.pump(String,fcn(v){ "%.3f %.3f ".fmt(v.xplode()) }) )
}</lang>
{{out}}
See [http://www.zenkinetic.com/Images/RosettaCode/sierpinskiPentagon.zkl.svg this image].
Displays fine in FireFox, in Chrome, it doesn't appear to be transformed so you only see part of the image.
<pre>
zkl bbb > sierpinskiPentagon.zkl.svg
$ wc sierpinskiPentagon.zkl.svg
3131 37519 314183 sierpinskiPentagon.zkl.svg
</pre>