Sierpinski square curve: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
Line 8: Line 8:
{{trans|C++}}
{{trans|C++}}


<lang 11l>F sierpinski_square(fname, size, length, order)
<syntaxhighlight lang="11l">F sierpinski_square(fname, size, length, order)
V x = (size - length) / 2
V x = (size - length) / 2
V y = Float(length)
V y = Float(length)
Line 35: Line 35:
outfile.write("'/>\n</svg>\n")
outfile.write("'/>\n</svg>\n")


sierpinski_square(‘sierpinski_square.svg’, 635, 5, 5)</lang>
sierpinski_square(‘sierpinski_square.svg’, 635, 5, 5)</syntaxhighlight>


{{out}}
{{out}}
Line 42: Line 42:
=={{header|C++}}==
=={{header|C++}}==
Output is a file in SVG format.
Output is a file in SVG format.
<lang cpp>// See https://en.wikipedia.org/wiki/Sierpi%C5%84ski_curve#Representation_as_Lindenmayer_system
<syntaxhighlight lang="cpp">// See https://en.wikipedia.org/wiki/Sierpi%C5%84ski_curve#Representation_as_Lindenmayer_system
#include <cmath>
#include <cmath>
#include <fstream>
#include <fstream>
Line 121: Line 121:
s.write(out, 635, 5, 5);
s.write(out, 635, 5, 5);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 128: Line 128:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
{{works with|Factor|0.99 2020-08-14}}
<lang factor>USING: accessors kernel L-system sequences ui ;
<syntaxhighlight lang="factor">USING: accessors kernel L-system sequences ui ;


: square-curve ( L-system -- L-system )
: square-curve ( L-system -- L-system )
Line 141: Line 141:
<L-system> square-curve
<L-system> square-curve
"Sierpinski square curve" open-window
"Sierpinski square curve" open-window
] with-ui</lang>
] with-ui</syntaxhighlight>




Line 178: Line 178:
{{libheader|go-lindenmayer}}
{{libheader|go-lindenmayer}}
The following uses the Lindenmayer system with the appropriate parameters from the Wikipedia article and produces a similar image (apart from the colors, yellow on blue) to the Sidef and zkl entries.
The following uses the Lindenmayer system with the appropriate parameters from the Wikipedia article and produces a similar image (apart from the colors, yellow on blue) to the Sidef and zkl entries.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 237: Line 237:
dc.Stroke()
dc.Stroke()
dc.SavePNG("sierpinski_square_curve.png")
dc.SavePNG("sierpinski_square_curve.png")
}</lang>
}</syntaxhighlight>


=={{header|J}}==
=={{header|J}}==
It looks like there's two different (though similar) concepts implemented here, of what a "Sierpinski square curve" looks like (the wikipedia writeup shows 45 degree angles -- like [[j:File:Sierpinski_curve.png]] but many of the implementations here show only right angles). And, the wikipedia writeup is obtuse about some of the details of the structure. And, we've got some dead links here. So, for now, a quickie ascii art implementation:<lang J> 1j1#"1' #'{~{{l,(1,~0{.~#y),l=.y,.0,.y}}^:3,.1
It looks like there's two different (though similar) concepts implemented here, of what a "Sierpinski square curve" looks like (the wikipedia writeup shows 45 degree angles -- like [[j:File:Sierpinski_curve.png]] but many of the implementations here show only right angles). And, the wikipedia writeup is obtuse about some of the details of the structure. And, we've got some dead links here. So, for now, a quickie ascii art implementation:<syntaxhighlight lang="j"> 1j1#"1' #'{~{{l,(1,~0{.~#y),l=.y,.0,.y}}^:3,.1
# # # # # # # #
# # # # # # # #
# # # #
# # # #
Line 255: Line 255:
# # # # # # # #
# # # # # # # #
# # # #
# # # #
# # # # # # # # </lang>
# # # # # # # # </syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{trans|C++}}
{{trans|C++}}
<lang java>import java.io.*;
<syntaxhighlight lang="java">import java.io.*;


public class SierpinskiSquareCurve {
public class SierpinskiSquareCurve {
Line 349: Line 349:
private static final String PRODUCTION = "XF-F+F-XF+F+XF-F+F-X";
private static final String PRODUCTION = "XF-F+F-XF+F+XF-F+F-X";
private static final int ANGLE = 90;
private static final int ANGLE = 90;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 366: Line 366:
for the simple-turtle.jq module used in this entry. The `include`
for the simple-turtle.jq module used in this entry. The `include`
statement assumes the file is in the pwd.
statement assumes the file is in the pwd.
<lang jq>include "simple-turtle" {search: "."};
<syntaxhighlight lang="jq">include "simple-turtle" {search: "."};


def rules: {"X": "XF-F+F-XF+F+XF-F+F-X"};
def rules: {"X": "XF-F+F-XF+F+XF-F+F-X"};
Line 393: Line 393:
sierpinski_curve(5)
sierpinski_curve(5)
| path("none"; "red"; 1) | svg(1000)
| path("none"; "red"; 1) | svg(1000)
</syntaxhighlight>
</lang>




=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Lindenmayer # https://github.com/cormullion/Lindenmayer.jl
<syntaxhighlight lang="julia">using Lindenmayer # https://github.com/cormullion/Lindenmayer.jl


scurve = LSystem(Dict("X" => "XF-F+F-XF+F+XF-F+F-X"), "F+XF+F+XF")
scurve = LSystem(Dict("X" => "XF-F+F-XF+F+XF-F+F-X"), "F+XF+F+XF")
Line 409: Line 409:
showpreview = true
showpreview = true
)
)
</syntaxhighlight>
</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>Graphics[SierpinskiCurve[3]]</lang>
<syntaxhighlight lang="mathematica">Graphics[SierpinskiCurve[3]]</syntaxhighlight>
{{out}}
{{out}}
Outputs a graphical version of a 3rd order Sierpinski curve.
Outputs a graphical version of a 3rd order Sierpinski curve.
Line 419: Line 419:
{{trans|C++}}
{{trans|C++}}
We produce a SVG file.
We produce a SVG file.
<lang Nim>import math
<syntaxhighlight lang="nim">import math


type
type
Line 472: Line 472:
var sc = SierpinskiCurve(file: outfile)
var sc = SierpinskiCurve(file: outfile)
sc.write(635, 5, 5)
sc.write(635, 5, 5)
outfile.close()</lang>
outfile.close()</syntaxhighlight>


{{out}}
{{out}}
Line 478: Line 478:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use SVG;
use SVG;
Line 514: Line 514:
open my $fh, '>', 'sierpinski-square-curve.svg';
open my $fh, '>', 'sierpinski-square-curve.svg';
print $fh $svg->xmlify(-namespace=>'svg');
print $fh $svg->xmlify(-namespace=>'svg');
close $fh;</lang>
close $fh;</syntaxhighlight>
See: [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sierpinski-square-curve.svg sierpinski-square-curve.svg] (offsite SVG image)
See: [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sierpinski-square-curve.svg sierpinski-square-curve.svg] (offsite SVG image)


Line 521: Line 521:
{{libheader|Phix/online}}
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Sierpinski_square_curve.htm here].
You can run this online [http://phix.x10.mx/p2js/Sierpinski_square_curve.htm here].
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Sierpinski_square_curve.exw
-- demo\rosetta\Sierpinski_square_curve.exw
Line 583: Line 583:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
and an svg-creating version:
and an svg-creating version:
<!--<lang Phix>-->
<!--<syntaxhighlight lang="phix">-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">rule</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"XF-F+F-XF+F+XF-F+F-X"</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">rule</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"XF-F+F-XF+F+XF-F+F-X"</span>
Line 624: Line 624:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">svgfmt</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">X</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">xt</span><span style="color: #0000FF;">+</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">yt</span><span style="color: #0000FF;">+</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">points</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">yt</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">svgfmt</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">X</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">xt</span><span style="color: #0000FF;">+</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">Y</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">yt</span><span style="color: #0000FF;">+</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">points</span><span style="color: #0000FF;">,</span><span style="color: #000000;">xt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">yt</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->


=={{header|Python}}==
=={{header|Python}}==
<lang Python>import matplotlib.pyplot as plt
<syntaxhighlight lang="python">import matplotlib.pyplot as plt
import math
import math


Line 676: Line 676:
s_angle = 90
s_angle = 90


draw_lsystem(s_axiom, s_rules, s_angle, 3)</lang>
draw_lsystem(s_axiom, s_rules, s_angle, 3)</syntaxhighlight>


=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> [ $ "turtleduck.qky" loadfile ] now!
<syntaxhighlight lang="quackery"> [ $ "turtleduck.qky" loadfile ] now!
[ stack ] is switch.arg ( --> [ )
[ stack ] is switch.arg ( --> [ )
Line 713: Line 713:
char R case [ 1 4 turn ]
char R case [ 1 4 turn ]
char F case [ 5 1 walk ]
char F case [ 5 1 walk ]
otherwise ( ignore ) ] ]</lang>
otherwise ( ignore ) ] ]</syntaxhighlight>


{{output}}
{{output}}
Line 723: Line 723:
{{works with|Rakudo|2020.02}}
{{works with|Rakudo|2020.02}}


<lang perl6>use SVG;
<syntaxhighlight lang="raku" line>use SVG;


role Lindenmayer {
role Lindenmayer {
Line 766: Line 766:
],
],
],
],
);</lang>
);</syntaxhighlight>
See: [https://github.com/thundergnat/rc/blob/master/img/sierpinski-square-curve-perl6.svg Sierpinski-square-curve-perl6.svg] (offsite SVG image)
See: [https://github.com/thundergnat/rc/blob/master/img/sierpinski-square-curve-perl6.svg Sierpinski-square-curve-perl6.svg] (offsite SVG image)


=={{header|Rust}}==
=={{header|Rust}}==
Program output is a file in SVG format.
Program output is a file in SVG format.
<lang rust>// [dependencies]
<syntaxhighlight lang="rust">// [dependencies]
// svg = "0.8.0"
// svg = "0.8.0"


Line 852: Line 852:
fn main() {
fn main() {
SierpinskiSquareCurve::save("sierpinski_square_curve.svg", 635, 5.0, 5).unwrap();
SierpinskiSquareCurve::save("sierpinski_square_curve.svg", 635, 5.0, 5).unwrap();
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 859: Line 859:
=={{header|Sidef}}==
=={{header|Sidef}}==
Uses the '''LSystem()''' class from [https://rosettacode.org/wiki/Hilbert_curve#Sidef Hilbert curve].
Uses the '''LSystem()''' class from [https://rosettacode.org/wiki/Hilbert_curve#Sidef Hilbert curve].
<lang ruby>var rules = Hash(
<syntaxhighlight lang="ruby">var rules = Hash(
x => 'xF-F+F-xF+F+xF-F+F-x',
x => 'xF-F+F-xF+F+xF-F+F-x',
)
)
Line 875: Line 875:
)
)


lsys.execute('F+xF+F+xF', 5, "sierpiński_square_curve.png", rules)</lang>
lsys.execute('F+xF+F+xF', 5, "sierpiński_square_curve.png", rules)</syntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/sierpi%C5%84ski_square_curve-sidef.png Sierpiński square curve]
Output image: [https://github.com/trizen/rc/blob/master/img/sierpi%C5%84ski_square_curve-sidef.png Sierpiński square curve]


=={{header|VBScript}}==
=={{header|VBScript}}==
Output to html (svg) displayed in the default browser. A turtle graphics class helps to keep the curve definition simple
Output to html (svg) displayed in the default browser. A turtle graphics class helps to keep the curve definition simple
<syntaxhighlight lang="vb">
<lang vb>


option explicit
option explicit
Line 1,034: Line 1,034:
sierp 5,4
sierp 5,4
set x=nothing
set x=nothing
</syntaxhighlight>
</lang>
=={{header|Wren}}==
=={{header|Wren}}==
{{trans|Go}}
{{trans|Go}}
{{libheader|DOME}}
{{libheader|DOME}}
{{libheader|Wren-lsystem}}
{{libheader|Wren-lsystem}}
<lang ecmascript>import "graphics" for Canvas, Color
<syntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
import "dome" for Window
import "math" for Math
import "math" for Math
Line 1,094: Line 1,094:
}
}


var Game = SierpinskiSquareCurve.new(770, 770, Color.blue, Color.yellow)</lang>
var Game = SierpinskiSquareCurve.new(770, 770, Color.blue, Color.yellow)</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
Uses Image Magick and
Uses Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<lang zkl>sierpinskiSquareCurve(4) : turtle(_);
<syntaxhighlight lang="zkl">sierpinskiSquareCurve(4) : turtle(_);


fcn sierpinskiSquareCurve(n){ // Lindenmayer system --> Data of As
fcn sierpinskiSquareCurve(n){ // Lindenmayer system --> Data of As
Line 1,127: Line 1,127:
}
}
img.writeJPGFile("sierpinskiSquareCurve.zkl.jpg");
img.writeJPGFile("sierpinskiSquareCurve.zkl.jpg");
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
Offsite image at [http://www.zenkinetic.com/Images/RosettaCode/sierpinskiSquareCurve.zkl.jpg Sierpinski square curve of order 4]
Offsite image at [http://www.zenkinetic.com/Images/RosettaCode/sierpinskiSquareCurve.zkl.jpg Sierpinski square curve of order 4]