Jump to content

Sierpinski square curve: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 8:
{{trans|C++}}
 
<langsyntaxhighlight lang="11l">F sierpinski_square(fname, size, length, order)
V x = (size - length) / 2
V y = Float(length)
Line 35:
outfile.write("'/>\n</svg>\n")
 
sierpinski_square(‘sierpinski_square.svg’, 635, 5, 5)</langsyntaxhighlight>
 
{{out}}
Line 42:
=={{header|C++}}==
Output is a file in SVG format.
<langsyntaxhighlight lang="cpp">// See https://en.wikipedia.org/wiki/Sierpi%C5%84ski_curve#Representation_as_Lindenmayer_system
#include <cmath>
#include <fstream>
Line 121:
s.write(out, 635, 5, 5);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 128:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: accessors kernel L-system sequences ui ;
 
: square-curve ( L-system -- L-system )
Line 141:
<L-system> square-curve
"Sierpinski square curve" open-window
] with-ui</langsyntaxhighlight>
 
 
Line 178:
{{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.
<langsyntaxhighlight lang="go">package main
 
import (
Line 237:
dc.Stroke()
dc.SavePNG("sierpinski_square_curve.png")
}</langsyntaxhighlight>
 
=={{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:<langsyntaxhighlight Jlang="j"> 1j1#"1' #'{~{{l,(1,~0{.~#y),l=.y,.0,.y}}^:3,.1
# # # # # # # #
# # # #
Line 255:
# # # # # # # #
# # # #
# # # # # # # # </langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|C++}}
<langsyntaxhighlight lang="java">import java.io.*;
 
public class SierpinskiSquareCurve {
Line 349:
private static final String PRODUCTION = "XF-F+F-XF+F+XF-F+F-X";
private static final int ANGLE = 90;
}</langsyntaxhighlight>
 
{{out}}
Line 366:
for the simple-turtle.jq module used in this entry. The `include`
statement assumes the file is in the pwd.
<langsyntaxhighlight lang="jq">include "simple-turtle" {search: "."};
 
def rules: {"X": "XF-F+F-XF+F+XF-F+F-X"};
Line 393:
sierpinski_curve(5)
| path("none"; "red"; 1) | svg(1000)
</syntaxhighlight>
</lang>
 
 
=={{header|Julia}}==
<langsyntaxhighlight 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")
Line 409:
showpreview = true
)
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang Mathematica="mathematica">Graphics[SierpinskiCurve[3]]</langsyntaxhighlight>
{{out}}
Outputs a graphical version of a 3rd order Sierpinski curve.
Line 419:
{{trans|C++}}
We produce a SVG file.
<langsyntaxhighlight Nimlang="nim">import math
 
type
Line 472:
var sc = SierpinskiCurve(file: outfile)
sc.write(635, 5, 5)
outfile.close()</langsyntaxhighlight>
 
{{out}}
Line 478:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use SVG;
Line 514:
open my $fh, '>', 'sierpinski-square-curve.svg';
print $fh $svg->xmlify(-namespace=>'svg');
close $fh;</langsyntaxhighlight>
See: [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sierpinski-square-curve.svg sierpinski-square-curve.svg] (offsite SVG image)
 
Line 521:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/Sierpinski_square_curve.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Sierpinski_square_curve.exw
Line 583:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
and an svg-creating version:
<!--<langsyntaxhighlight Phixlang="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;">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:
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|Python}}==
<langsyntaxhighlight Pythonlang="python">import matplotlib.pyplot as plt
import math
 
Line 676:
s_angle = 90
 
draw_lsystem(s_axiom, s_rules, s_angle, 3)</langsyntaxhighlight>
 
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "turtleduck.qky" loadfile ] now!
[ stack ] is switch.arg ( --> [ )
Line 713:
char R case [ 1 4 turn ]
char F case [ 5 1 walk ]
otherwise ( ignore ) ] ]</langsyntaxhighlight>
 
{{output}}
Line 723:
{{works with|Rakudo|2020.02}}
 
<syntaxhighlight lang="raku" perl6line>use SVG;
 
role Lindenmayer {
Line 766:
],
],
);</langsyntaxhighlight>
See: [https://github.com/thundergnat/rc/blob/master/img/sierpinski-square-curve-perl6.svg Sierpinski-square-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 852:
fn main() {
SierpinskiSquareCurve::save("sierpinski_square_curve.svg", 635, 5.0, 5).unwrap();
}</langsyntaxhighlight>
 
{{out}}
Line 859:
=={{header|Sidef}}==
Uses the '''LSystem()''' class from [https://rosettacode.org/wiki/Hilbert_curve#Sidef Hilbert curve].
<langsyntaxhighlight lang="ruby">var rules = Hash(
x => 'xF-F+F-xF+F+xF-F+F-x',
)
Line 875:
)
 
lsys.execute('F+xF+F+xF', 5, "sierpiński_square_curve.png", rules)</langsyntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/sierpi%C5%84ski_square_curve-sidef.png Sierpiński square curve]
 
=={{header|VBScript}}==
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
Line 1,034:
sierp 5,4
set x=nothing
</syntaxhighlight>
</lang>
=={{header|Wren}}==
{{trans|Go}}
{{libheader|DOME}}
{{libheader|Wren-lsystem}}
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color
import "dome" for Window
import "math" for Math
Line 1,094:
}
 
var Game = SierpinskiSquareCurve.new(770, 770, Color.blue, Color.yellow)</langsyntaxhighlight>
 
=={{header|zkl}}==
Uses Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight lang="zkl">sierpinskiSquareCurve(4) : turtle(_);
 
fcn sierpinskiSquareCurve(n){ // Lindenmayer system --> Data of As
Line 1,127:
}
img.writeJPGFile("sierpinskiSquareCurve.zkl.jpg");
}</langsyntaxhighlight>
{{out}}
Offsite image at [http://www.zenkinetic.com/Images/RosettaCode/sierpinskiSquareCurve.zkl.jpg Sierpinski square curve of order 4]
10,333

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.