Sierpinski curve: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 7:
=={{header|Action!}}==
Action! language does not support recursion. Therefore an iterative approach with a stack has been proposed.
<langsyntaxhighlight Actionlang="action!">DEFINE C_="10+"
DEFINE N_="20+"
DEFINE E_="30+"
Line 169:
DO UNTIL CH#$FF OD
CH=$FF
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sierpinski_curve.png Screenshot from Atari 8-bit computer]
Line 176:
{{trans|Go}}
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library]
<langsyntaxhighlight AutoHotkeylang="autohotkey">SierpinskiW := 500
SierpinskiH := 500
level := 5
Line 331:
ExitApp
Return
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
Output is a file in SVG format. The curve is generated using the Lindenmayer system method.
<langsyntaxhighlight lang="cpp">// See https://en.wikipedia.org/wiki/Sierpi%C5%84ski_curve#Representation_as_Lindenmayer_system
#include <cmath>
#include <fstream>
Line 415:
s.write(out, 545, 7, 5);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 422:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: accessors kernel L-system sequences ui ;
 
: curve ( L-system -- L-system )
Line 433:
} >>rules ;
 
[ <L-system> curve "Sierpinski curve" open-window ] with-ui</langsyntaxhighlight>
 
 
Line 478:
{{trans|Phix}}
A partial translation anyway which produces a static image of a SC of level 5, yellow on blue, which can be viewed with a utility such as EOG.
<langsyntaxhighlight lang="go">package main
 
import (
Line 595:
dc.Stroke()
dc.SavePNG("sierpinski_curve.png")
}</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">import java.io.*;
 
public class SierpinskiCurve {
Line 689:
private static final String PRODUCTION = "XF+G+XF--F--XF+G+X";
private static final int ANGLE = 45;
}</langsyntaxhighlight>
 
{{out}}
Line 705:
depending on the location of the included file, and the command-line
options used.
<langsyntaxhighlight lang="jq">include "turtle" {search: "."};
 
# Compute the curve using a Lindenmayer system of rules
Line 745:
sierpinski_curve(5)
| svg
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
===Turtle procedural (lineto) version===
Modified from [https://craftofcoding.wordpress.com/2018/05/08/recursive-patterns-the-sierpinski-curve/ Craft of Coding blog, Processing version]
<langsyntaxhighlight Julialang="julia">using Luxor
 
function sierpinski_curve(x0, y0, h, level)
Line 802:
finish()
preview()
</syntaxhighlight>
</lang>
===LSystem version===
<langsyntaxhighlight lang="julia">using Lindenmayer # https://github.com/cormullion/Lindenmayer.jl
 
sierpcurve = LSystem(Dict("X" => "XF+G+XF--F--XF+G+X"), "F--XF--F--XF")
Line 819:
showpreview = true
)
</syntaxhighlight>
</lang>
 
=={{header|Lambdatalk}}==
 
<syntaxhighlight lang="scheme">
<lang Scheme>
{def sierp
{def sierp.r
Line 861:
stroke="#000" fill="transparent" stroke-width="1"}}
}
</syntaxhighlight>
</lang>
 
See the result in http://lambdaway.free.fr/lambdawalks/?view=sierpinsky
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">Graphics[SierpinskiCurve[3]]</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|C++}}
We produce a SVG file using same algorithm as the one of C++ version.
<langsyntaxhighlight Nimlang="nim">import math
 
type
Line 924:
var sc = SierpinskiCurve(file: outfile)
sc.write(545, 7, 5)
outfile.close()</langsyntaxhighlight>
 
{{out}}
Line 930:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use SVG;
Line 967:
open my $fh, '>', 'sierpinski-curve.svg';
print $fh $svg->xmlify(-namespace=>'svg');
close $fh;</langsyntaxhighlight>
See: [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sierpinski-curve.svg sierpinski-curve.svg] (offsite SVG image)
 
Line 974:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Sierpinski_curve.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Sierpinski_curve.exw
Line 1,144:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Processing}}==
{{trans|Go}}
<syntaxhighlight lang="processing">
<lang Processing>
 
// https://rosettacode.org/wiki/Sierpinski_curve#C.2B.2B
Line 1,293:
}
}
</syntaxhighlight>
</lang>
{{out}}
Offsite image at [https://github.com/rupertrussell/sierpinski_curve/blob/main/Sierpinski_Curve_Level5.png Sierpinski_Curve_Level5.png]
Line 1,300:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import hsv_to_rgb as hsv
Line 1,330:
#curve('F', {'F': 'G-F-G', 'G': 'F+G+F'}, 60, 7)
#curve('A', {'A': '+BF-AFA-FB+', 'B': '-AF+BFB+FA-'}, 90, 6)
#curve('FX+FX+', {'X': 'X+YF', 'Y': 'FX-Y'}, 90, 12)</langsyntaxhighlight>
 
Output in the plot window.
Line 1,336:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "turtleduck.qky" loadfile ] now!
[ stack ] is switch.arg ( --> [ )
Line 1,372:
char A case [ ( ignore ) ]
otherwise [ 5 1 walk ] ] ]
-1 8 turn</langsyntaxhighlight>
 
{{output}}
Line 1,382:
{{works with|Rakudo|2020.02}}
 
<syntaxhighlight lang="raku" perl6line>use SVG;
 
role Lindenmayer {
Line 1,420:
],
],
);</langsyntaxhighlight>
See: [https://github.com/thundergnat/rc/blob/master/img/sierpinski-curve-perl6.svg Sierpinski-curve-perl6.svg] (offsite SVG image)
 
=={{header|Rust}}==
Program output is a file in SVG format.
<langsyntaxhighlight lang="rust">// [dependencies]
// svg = "0.8.0"
 
Line 1,507:
fn main() {
SierpinskiCurve::save("sierpinski_curve.svg", 545, 5).unwrap();
}</langsyntaxhighlight>
 
{{out}}
Line 1,514:
=={{header|Sidef}}==
Uses the '''LSystem()''' class from [https://rosettacode.org/wiki/Hilbert_curve#Sidef Hilbert curve].
<langsyntaxhighlight lang="ruby">var rules = Hash(
x => 'xF+G+xF--F--xF+G+x',
)
Line 1,530:
)
 
lsys.execute('F--xF--F--xF', 5, "sierpiński_curve.png", rules)</langsyntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/sierpi%C5%84ski_curve-sidef.png Sierpiński curve]
 
Line 1,536:
{{trans|Go}}
{{libheader|DOME}}
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
 
Line 1,664:
}
 
var Game = SierpinskiCurve.new(770, 770, 5, Color.blue, Color.yellow)</langsyntaxhighlight>
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">// Rosetta Code problem: http://rosettacode.org/wiki/Sierpinski_curve
// Adapted from https://www.ocg.at/sites/ocg.at/files/EuroLogo2001/P74Batagelj.pdf to Yabasic by Galileo, 01/2022
 
Line 1,694:
 
startTurtle()
Sierpinski(9, 12) </langsyntaxhighlight>
 
=={{header|zkl}}==
Uses Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight lang="zkl">sierpinskiCurve(5) : turtle(_,45,45); // n=5 --> 11,606 characters
 
fcn sierpinskiCurve(order){
Line 1,729:
}
img.writeJPGFile("sierpinskiCurve.zkl.jpg");
}</langsyntaxhighlight>
{{out}}
Offsite image at [http://www.zenkinetic.com/Images/RosettaCode/sierpinskiCurve.zkl.jpg Sierpinski curve order 5]
10,333

edits