Sierpinski pentagon: Difference between revisions

 
(3 intermediate revisions by 2 users not shown)
Line 518:
=={{header|EasyLang}}==
{{Trans|Processing}}
[https://easylang.dev/show/#cod=pZHNrsIgEIX3PMVJ3GgbsSVpdNOHacroJemFBog/by+DqHXh3Vx2w/mYc2ZwXpNHj06sIMaJBi8mY+lidPxBI5UI4zBRAlrssFaoMbqAvUIFteE3s3cjZrJxODmLK24IRhM0zamBhBQAzLHUPRqu0/l1Z2K6lEfnMdhTdmoQIs3sER3U4VCQdK6o++z/QKts9ZZvLAdjv8g818IyJ6MpPIkcu0oNeOByp02I6B9SXYjFAv4Xnpv/Ef5T/rbhLdrlPFJI8UJVh7ZD18DlT2b0Dg== Run it]
[https://easylang.dev/show/#cod=pZHBbsMgEETvfMVIvaS2Smwkq774YyyzSZEIWIDS5O/DEqI6h/QSbss8dmYXHzQFTBjEB8RiaQ7CGke/RqcfdFKJuMyWMtBjj51Ci8VHfCs0UJ/8Zg1+wUouzUfvcMEV0WiCpjU3kJACgDnUekLHdT4nfyama3nwAbM7FqcOMdHKHslDjWNF8rmgnYr/HW2K1Z98ZTka90LmuTaWJRnZ+CBK7CY34IHrnTYxYbpLbSU2C3gvPDf/J/yznEPZl2v+Qr8dSgopnnk1oB8wdPDlu5m/AQ== Run it]
<syntaxhighlight lang="easylang">
order = 5
Line 540:
x += cos angle * dist
y += sin angle * dist
call pentagon x y side depth - 1
.
.
.
call pentagon 25 15 50 order - 1
</syntaxhighlight>
 
Line 913:
Clicking Pentaflake you can see orders 1-6 of it in different colors.
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Julia|Julia]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
The following program produces an SVG image.
To produce the .svg file, use a command such as:
<pre>
jq -nr -f sierpinski-pentagon.jq > sierpinski-pentagon.svg
</pre>
where "sierpinski-pentagon.jq" is a file containing the program shown
below.
 
The .svg file can conveniently be viewed as a graphic using a browser,
or an editor such as Emacs or Aquamacs.
<syntaxhighlight lang="jq">
### Generic functions
 
def addComplex(stream): reduce stream as [$x,$y] ([0,0]; .[0] += $x | .[1] += $y);
 
def lpad($len; $x): tostring | ($len - length) as $l | ($x * $l) + .;
 
# Input: an array
def multiply($x): map(. * $x);
 
# Round to approx $ndec places
def round($ndec): pow(10;$ndec) as $p | . * $p | round / $p;
 
def power($a; $b): reduce range(0;$b) as $i (1; . * $a);
 
def tau: 8 * atan2(1; 1);
 
def tobase($b):
def digit: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[.:.+1];
def mod: . % $b;
def div: ((. - mod) / $b);
def digits: recurse( select(. > 0) | div) | mod ;
# For jq it would be wise to protect against `infinite` as input, but using `isinfinite` confuses gojq
select( (tostring|test("^[0-9]+$")) and 2 <= $b and $b <= 36)
| if . == 0 then "0"
else [digits | digit] | reverse[1:] | add
end;
 
### Sierpinski Pentagons
 
def svgHead($width):
"<svg height=\"\($width)\" width=\"\($width)\" style=\"fill:blue\"",
"version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">";
 
def svgEnd: "</svg>";
 
# SVG height and width will be 2 * dim
def pentagon($dim):
def sides: 5;
def order: 5;
def scale: (3 - (order | sqrt)) / 2;
 
def cis: [cos, sin];
def orders:
[range(0; order) | ((1 - scale) * $dim) * power(scale ; .) ];
def vertices:
tau as $tau | [range(0; sides) | ( . * $tau / sides | cis)];
svgHead(2*$dim),
(orders as $orders
| vertices as $vertices
| range(1; 1 + power(sides; order)) as $i
| [ ($i|tobase(sides) | lpad(order; "0") | split("")[]) | $vertices[tonumber]] as $varr
| addComplex(range(0; $orders|length) as $i | $varr[$i] | multiply($orders[$i])) as $vector
| ($vertices | map( addComplex($vector, multiply($orders[-1] * (1-scale))))) as $vprod
| ($vprod | map( map(round(3)) | "\(.[0]) \(.[1])") | join(" ")) as $points
| "<polygon points=\"\($points)\" transform=\"translate(\($dim),\($dim)) rotate(-18)\" />"),
svgEnd ;
 
pentagon(250)
</syntaxhighlight>
{{output}}
See [[#Julia|Julia]].
 
 
=={{header|Julia}}==
Line 2,083 ⟶ 2,165:
{{libheader|DOME}}
Black backgound and slightly different palette to Go. Also pentagons are unfilled.
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 2,145 ⟶ 2,227:
 
var Game = SierpinskiPentagon.new(640, 640)</syntaxhighlight>
 
{{out}}
[[File:Wren-Sierpinski_pentagon.png|400px]]
 
=={{header|XPL0}}==
2,515

edits