Sierpinski arrowhead curve: Difference between revisions

m
syntax highlighting fixup automation
(Ada version)
m (syntax highlighting fixup automation)
Line 8:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">V sqrt3_2 = sqrt(3) / 2
 
F sierpinski_arrowhead_next(points)
Line 49:
outfile.write(I L.index == 0 {‘M’} E ‘L’)
outfile.write(point.x‘,’point.y"\n")
outfile.write("'/>\n</svg>\n")</langsyntaxhighlight>
 
{{out}}
Line 56:
=={{header|Ada}}==
{{libheader|APDF}}Even order curves are drawn pointing downwards.
<langsyntaxhighlight Adalang="ada">with Ada.Command_Line;
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Text_IO;
Line 119:
Rule => Nonzero_Winding_Number);
Doc.Close;
end Arrowhead_Curve;</langsyntaxhighlight>
 
=={{header|ALGOL W}}==
Produces an Ascii Art Sierpinski Arrowhead Curve using the algorithm from the Wikipedia page.<br>
Note that the Wikipedia algotrithm draws even order curves with the base at the top.
<langsyntaxhighlight lang="algolw">begin % draw sierpinski arrowhead curves using ascii art %
integer CANVAS_WIDTH;
CANVAS_WIDTH := 200;
Line 240:
end for_order
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 283:
{{trans|Go}}
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library]
<langsyntaxhighlight AutoHotkeylang="autohotkey">order := 7
theta := 0
 
Line 387:
Gdip_Shutdown(pToken)
ExitApp
Return</langsyntaxhighlight>
 
=={{header|C}}==
This code is based on the Phix and Go solutions, but produces a file in SVG format.
<langsyntaxhighlight lang="c">// See https://en.wikipedia.org/wiki/Sierpi%C5%84ski_curve#Arrowhead_curve
#include <math.h>
#include <stdio.h>
Line 456:
fclose(out);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 463:
=={{header|C++}}==
The output of this program is an SVG file.
<langsyntaxhighlight lang="cpp">#include <fstream>
#include <iostream>
#include <vector>
Line 528:
write_sierpinski_arrowhead(out, 600, 8);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
Line 535:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: accessors L-system ui ;
 
: arrowhead ( L-system -- L-system )
Line 546:
} >>rules ;
 
[ <L-system> arrowhead "Arrowhead" open-window ] with-ui</langsyntaxhighlight>
 
 
Line 585:
 
===ASCII===
<langsyntaxhighlight lang="forth">( ASCII output with use of ANSI terminal control )
 
: draw-line ( direction -- )
Line 619:
;
 
5 arrowhead</langsyntaxhighlight>
 
{{out}}
Line 660:
 
===SVG file===
<langsyntaxhighlight lang="forth">( SVG ouput )
 
: draw-line ( direction -- ) \ line-length=10 ; sin(60)=0.87 ; cos(60)=0.5
Line 709:
;
 
5 arrowhead</langsyntaxhighlight>
 
 
Line 717:
{{trans|Phix}}
A partial translation anyway which produces a static image of a SAC of order 6, magenta on black, which can be viewed with a utility such as EOG.
<langsyntaxhighlight lang="go">package main
 
import (
Line 782:
dc.Stroke()
dc.SavePNG("sierpinski_arrowhead_curve.png")
}</langsyntaxhighlight>
 
=={{header|jq}}==
Line 795:
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 835:
sierpinski_curve(8)
| svg
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Lindenmayer # https://github.com/cormullion/Lindenmayer.jl
scurve = LSystem(Dict("F" => "G+F+Gt", "G"=>"F-G-F"), "G")
Line 851:
showpreview = true
)
</syntaxhighlight>
</lang>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
<lang Scheme>
{def sierp
{def sierp.r
Line 874:
 
the output can be seen in http://lambdaway.free.fr/lambdawalks/?view=sierpinsky
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[DoStep]
DoStep[Line[{x_, y_}]] := Module[{diff, perp, pts},
diff = y - x;
Line 886:
lns = {Line[{{0.0, 0.0}, {1.0, 0.0}}]};
lns = Nest[Catenate[DoStep /@ #] &, lns, 5];
Graphics[lns]</langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|C++}}
Output is an SVG file.
<langsyntaxhighlight Nimlang="nim">import math
 
const Sqrt3_2 = sqrt(3.0) / 2.0
Line 938:
let outfile = open("sierpinski_arrowhead.svg", fmWrite)
outfile.writeSierpinskiArrowhead(600, 8)
outfile.close()</langsyntaxhighlight>
 
{{out}}
Line 944:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use SVG;
Line 983:
open my $fh, '>', 'sierpinski-arrowhead-curve.svg';
print $fh $svg->xmlify(-namespace=>'svg');
close $fh;</langsyntaxhighlight>
See: [https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/sierpinski-arrowhead-curve.svg sierpinski-arrowhead-curve.svg] (offsite SVG image)
 
Line 989:
{{libheader|Phix/pGUI}}
You can run this online [http://phix.x10.mx/p2js/Sierpinski_arrowhead_curve.htm here], and experiment with [shift] +/- as noted below.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Sierpinski_arrowhead_curve.exw
Line 1,113:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|Processing}}==
<langsyntaxhighlight lang="java">final PVector t = new PVector(20, 30, 60);
 
void setup() {
Line 1,135:
s.y += sin(radians(s.z)) * l);
return s;
}</langsyntaxhighlight>'''The sketch can be run online''' :<BR> [https://www.openprocessing.org/sketch/954181 here.]
 
==={{header|Processing Python mode}}===
<langsyntaxhighlight lang="python">
 
t = { 'x': 20, 'y': 30, 'a': 60 }
Line 1,162:
 
return s
</syntaxhighlight>
</lang>
 
'''The sketch can be run online''' :<BR> [https://Trinket.io/processing/de6eddb155 here.]
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
import matplotlib.pyplot as plt
import math
Line 1,219:
 
draw_lsystem(s_axiom, s_rules, s_angle, 7)
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
{{Trans|C}}
<langsyntaxhighlight lang="prolog">main:-
write_sierpinski_arrowhead('sierpinski_arrowhead.svg', 600, 8).
 
Line 1,265:
curve(Stream, Order1, Length2, Cursor3, Cursor4, Angle),
turn(Cursor4, Angle, Cursor5),
curve(Stream, Order1, Length2, Cursor5, Cursor1, Angle1).</langsyntaxhighlight>
 
{{out}}
Line 1,274:
Using an L-system.
 
<langsyntaxhighlight Quackerylang="quackery"> [ $ "turtleduck.qky" loadfile ] now!
[ stack ] is switch.arg ( --> [ )
Line 1,306:
[ char L case [ -1 6 turn ]
char R case [ 1 6 turn ]
otherwise [ 4 1 walk ] ] ]</langsyntaxhighlight>
 
{{output}}
Line 1,316:
{{works with|Rakudo|2020.02}}
 
<syntaxhighlight lang="raku" perl6line>use SVG;
 
role Lindenmayer {
Line 1,353:
:polyline[ :points(@points.join: ','), :fill<black>, :style<stroke:#FF4EA9> ],
],
);</langsyntaxhighlight>
See: [https://github.com/thundergnat/rc/blob/master/img/sierpinski-arrowhead-curve-perl6.svg Sierpinski-arrowhead-curve-perl6.svg] (offsite SVG image)
 
=={{header|REXX}}==
{{trans|Algol W}}
<langsyntaxhighlight lang="rexx">/*REXX pgm computes and displays a Sierpinski Arrowhead Curve using the characters: \_/ */
parse arg order . /*obtain optional argument from the CL.*/
if order=='' | order=="," then order= 5 /*Not specified? Then use the default.*/
Line 1,393:
end /*select*/ /*curve character is based on direction*/
Lx= min(Lx,x); Hx= max(Hx,x); Ly= min(Ly,y); Hy= max(Hy,y) /*min&max of x,y*/
return /*#: heading in degrees of the curve. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input value of: &nbsp; &nbsp; <tt> 5 </tt>}}
<b><pre>
Line 1,436:
{{libheader|JRubyArt}}
For grammar see Hilbert Curve
<langsyntaxhighlight lang="ruby">
load_libraries :grammar
attr_reader :points
Line 1,520:
end
 
</syntaxhighlight>
</lang>
 
=={{header|Rust}}==
Output is a file in SVG format. Another variation on the same theme as the C, Go and Phix solutions.
<langsyntaxhighlight lang="rust">// [dependencies]
// svg = "0.8.0"
 
Line 1,600:
fn main() {
write_sierpinski_arrowhead("sierpinski_arrowhead.svg", 600, 8).unwrap();
}</langsyntaxhighlight>
 
{{out}}
Line 1,607:
=={{header|Sidef}}==
Uses the '''LSystem()''' class from [https://rosettacode.org/wiki/Hilbert_curve#Sidef Hilbert curve].
<langsyntaxhighlight lang="ruby">var rules = Hash(
x => 'yF+xF+y',
y => 'xF-yF-x',
Line 1,625:
)
 
lsys.execute('xF', 7, "sierpiński_arrowhead.png", rules)</langsyntaxhighlight>
Output image: [https://github.com/trizen/rc/blob/master/img/sierpi%C5%84ski_arrowhead-sidef.png Sierpiński arrowhead]
 
Line 1,631:
{{trans|Go}}
{{libheader|DOME}}
<langsyntaxhighlight lang="ecmascript">import "graphics" for Canvas, Color, Point
import "dome" for Window
 
Line 1,689:
}
}
}</langsyntaxhighlight>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int PosX, PosY;
real Dir;
 
Line 1,720:
Curve(Order, Length, -Sixty);
];
]</langsyntaxhighlight>
 
{{out}}
Line 1,730:
Uses Image Magick and
the PPM class from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#zkl
<langsyntaxhighlight lang="zkl">order:=7;
sierpinskiArrowheadCurve(order) : turtle(_,order);
 
Line 1,760:
}
img.writeJPGFile("sierpinskiArrowheadCurve.zkl.jpg");
}</langsyntaxhighlight>
{{out}}
Offsite image at [http://www.zenkinetic.com/Images/RosettaCode/sierpinskiArrowheadCurve.zkl.jpg Sierpinski arrowhead curve order 7]
10,333

edits