Sierpinski arrowhead curve: Difference between revisions

m
(Added Forth entry)
 
(48 intermediate revisions by 21 users not shown)
Line 1:
{{draft task|Fractals}}
;Task
 
Produce a graphical or ASCII-art representation of a  [[wp:Sierpiński_curve#Arrowhead_curve|Sierpinski arrowhead curve]]  of at least order  '''3'''.
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">V sqrt3_2 = sqrt(3) / 2
 
F sierpinski_arrowhead_next(points)
V result = [(0.0, 0.0)] * (3 * (points.len - 1) + 1)
V j = 0
L(i) 0 .< points.len - 1
V (x0, y0) = points[i]
V (x1, y1) = points[i + 1]
V dx = x1 - x0
result[j] = (x0, y0)
I y0 == y1
V d = abs(dx * :sqrt3_2 / 2)
result[j + 1] = (x0 + dx / 4, y0 - d)
result[j + 2] = (x1 - dx / 4, y0 - d)
E I y1 < y0
result[j + 1] = (x1, y0)
result[j + 2] = (x1 + dx / 2, (y0 + y1) / 2)
E
result[j + 1] = (x0 - dx / 2, (y0 + y1) / 2)
result[j + 2] = (x0, y1)
j += 3
result[j] = points.last
R result
 
V size = 600
V iterations = 8
V outfile = File(‘sierpinski_arrowhead.svg’, WRITE)
 
outfile.write(‘<svg xmlns='http://www.w3.org/2000/svg' width='’size‘' height='’size"'>\n")
outfile.write("<rect width='100%' height='100%' fill='white'/>\n")
outfile.write(‘<path stroke-width='1' stroke='black' fill='none' d='’)
V margin = 20.0
V side = size - 2 * margin
V x = margin
V y = 0.5 * size + 0.5 * sqrt3_2 * side
V points = [(x, y), (x + side, y)]
L 0 .< iterations
points = sierpinski_arrowhead_next(points)
L(point) points
outfile.write(I L.index == 0 {‘M’} E ‘L’)
outfile.write(point.x‘,’point.y"\n")
outfile.write("'/>\n</svg>\n")</syntaxhighlight>
 
{{out}}
Output is similar to Nim and C++.
 
=={{header|Ada}}==
{{libheader|APDF}}Even order curves are drawn pointing downwards.
<syntaxhighlight lang="ada">with Ada.Command_Line;
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Text_IO;
with PDF_Out;
 
procedure Arrowhead_Curve is
 
package Real_Math is
new Ada.Numerics.Generic_Elementary_Functions (PDF_Out.Real);
use Real_Math, PDF_Out, Ada.Command_Line, Ada.Text_IO;
 
subtype Angle_Deg is Real;
type Order_Type is range 0 .. 7;
 
Purple : constant Color_Type := (0.7, 0.0, 0.5);
Length : constant Real := 340.0;
Corner : constant Point := (120.0, 480.0);
 
Order : Order_Type;
Current : Point := (0.0, 0.0);
Direction : Angle_Deg := Angle_Deg'(0.0);
Doc : PDF_Out_File;
 
procedure Curve (Order : Order_Type; Length : Real; Angle : Angle_Deg) is
begin
if Order = 0 then
Current := Current + Length * Point'(Cos (Direction, 360.0),
Sin (Direction, 360.0));
Doc.Line (Corner + Current);
else
Curve (Order - 1, Length / 2.0, -Angle); Direction := Direction + Angle;
Curve (Order - 1, Length / 2.0, Angle); Direction := Direction + Angle;
Curve (Order - 1, Length / 2.0, -Angle);
end if;
end Curve;
 
begin
if Argument_Count /= 1 then
Put_Line ("arrowhead_curve <order>");
Put_Line (" <order> 0 .. 7");
Put_Line ("open sierpinski-arrowhead-curve.pdf to view ouput");
return;
end if;
Order := Order_Type'Value (Argument (1));
 
Doc.Create ("sierpinski-arrowhead-curve.pdf");
Doc.Page_Setup (A4_Portrait);
Doc.Margins (Margins_Type'(Left => Cm_2_5,
others => One_cm));
Doc.Stroking_Color (Purple);
Doc.Line_Width (2.0);
Doc.Move (Corner);
if Order mod 2 = 0 then
Direction := 0.0;
Curve (Order, Length, 60.0);
else
Direction := 60.0;
Curve (Order, Length, -60.0);
end if;
Doc.Finish_Path (Close_Path => False,
Rendering => Stroke,
Rule => Nonzero_Winding_Number);
Doc.Close;
end Arrowhead_Curve;</syntaxhighlight>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-l-system}}
Generates an SVG file containing the curve using the L-System. Very similar to the Algol 68 Sierpinski square curve sample. Note the Algol 68 L-System library source code is on a separate page on Rosetta Code - follow the above link and then to the Talk page.
<syntaxhighlight lang="algol68">
BEGIN # Sierpinski Arrowhead Curve in SVG #
# uses the RC Algol 68 L-System library for the L-System evaluation & #
# interpretation #
 
PR read "lsystem.incl.a68" PR # include L-System utilities #
 
PROC sierpinski arrowhead curve = ( STRING fname, INT size, length, order, init x, init y )VOID:
IF FILE svg file;
BOOL open error := IF open( svg file, fname, stand out channel ) = 0
THEN
# opened OK - file already exists and #
# will be overwritten #
FALSE
ELSE
# failed to open the file #
# - try creating a new file #
establish( svg file, fname, stand out channel ) /= 0
FI;
open error
THEN # failed to open the file #
print( ( "Unable to open ", fname, newline ) );
stop
ELSE # file opened OK #
 
REAL x := init x;
REAL y := init y;
INT angle := 0;
put( svg file, ( "<svg xmlns='http://www.w3.org/2000/svg' width='"
, whole( size, 0 ), "' height='", whole( size, 0 ), "'>"
, newline, "<rect width='100%' height='100%' fill='white'/>"
, newline, "<path stroke-width='1' stroke='black' fill='none' d='"
, newline, "M", whole( x, 0 ), ",", whole( y, 0 ), newline
)
);
 
LSYSTEM ssc = ( "XF"
, ( "X" -> "YF+XF+Y"
, "Y" -> "XF-YF-X"
)
);
STRING curve = ssc EVAL order;
curve INTERPRET ( ( CHAR c )VOID:
IF c = "F" THEN
x +:= length * cos( angle * pi / 180 );
y +:= length * sin( angle * pi / 180 );
put( svg file, ( " L", whole( x, 0 ), ",", whole( y, 0 ), newline ) )
ELIF c = "+" THEN
angle +:= 60 MODAB 360
ELIF c = "-" THEN
angle -:= 60 MODAB 360
FI
);
put( svg file, ( "'/>", newline, "</svg>", newline ) );
close( svg file )
FI # sierpinski square # ;
 
sierpinski arrowhead curve( "sierpinski_arrowhead.svg", 1200, 12, 6, 200, 700 )
 
END
</syntaxhighlight>
 
=={{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 124 ⟶ 306:
end for_order
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 167 ⟶ 349:
{{trans|Go}}
Requires [https://www.autohotkey.com/boards/viewtopic.php?t=6517 Gdip Library]
<langsyntaxhighlight AutoHotkeylang="autohotkey">order := 7
theta := 0
 
Line 271 ⟶ 453:
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 340 ⟶ 522:
fclose(out);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
[[Media:Sierpinski_arrowhead_c.svg]]
See: [https://slack-files.com/T0CNUL56D-F016A6S357Z-dde1d67b8c sierpinski_arrowhead.svg] (offsite SVG image)
 
=={{header|C++}}==
The output of this program is an SVG file.
<langsyntaxhighlight lang="cpp">#include <fstream>
#include <iostream>
#include <vector>
Line 412 ⟶ 594:
write_sierpinski_arrowhead(out, 600, 8);
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
{{out}}
[[Media:Sierpinski_arrowhead_cpp.svg]]
See: [https://slack-files.com/T0CNUL56D-F016A6S357Z-dde1d67b8c sierpinski_arrowhead.svg] (offsite SVG image)
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
[[File:DelphiSierpinskiArrowhead.png|frame|none]]
<syntaxhighlight lang="Delphi">
 
type T2DVector=packed record
X,Y: double;
end;
 
var Pos: T2DVector;
var CurAngle: double;
 
 
procedure Turn(Angle: double);
{Turn current angle by specified degrees}
begin
CurAngle:=CurAngle+Angle;
end;
 
 
procedure DrawLine(Canvas: TCanvas; Len: double);
{Draw current line, rotated by the current angle}
var P2: T2DVector;
begin
Canvas.Pen.Mode:=pmCopy;
Canvas.Pen.Style:=psSolid;
P2.X:= Pos.X + Len*Cos(DegToRad(CurAngle));
P2.Y:= Pos.Y - Len*Sin(DegToRad(CurAngle));
Canvas.MoveTo(Round(Pos.X),Round(Pos.Y));
Canvas.LineTo(Round(P2.X),Round(P2.Y));
Pos:=P2;
end;
 
procedure Curve(Canvas: TCanvas; Order: integer; Length: double; Angle: integer);
{Recursively draw curve of specified order and based specified Angle}
begin
if 0 = Order then DrawLine(Canvas,Length)
else
begin
Curve(Canvas,Order - 1, Length / 2, -Angle);
Turn(angle);
curve(Canvas,Order - 1, Length / 2, Angle);
Turn(angle);
Curve(Canvas,Order - 1, Length / 2, -Angle);
end;
end;
 
 
procedure SierpinskiArrowheadCurve(Image: TImage; Order: integer; Length: double);
{Draw arrowhead curve of specified order.}
{Length controls the width of one side of the arrowhead }
begin
Pos.X:=10; Pos.Y:=10;
if (Order and 1)=0 then Curve(Image.Canvas, Order, Length, +60)
else
begin
{Order is odd}
Turn(+60);
Curve(Image.Canvas, Order, Length, -60);
end;
end;
 
 
procedure ShowSierpinskiArrowhead(Image: TImage);
var Size: double;
begin
if Image.Width>Image.Height then Size:=Image.Height/0.9
else Size:=Image.Width;
{Super impose three different orders, colors and line-widths}
Image.Canvas.Pen.Color:=clBlack;
Image.Canvas.Pen.Width:=3;
SierpinskiArrowheadCurve(Image,4, Size);
Image.Canvas.Pen.Color:=clBlue;
Image.Canvas.Pen.Width:=2;
SierpinskiArrowheadCurve(Image,6, Size);
Image.Canvas.Pen.Color:=clRed;
Image.Canvas.Pen.Width:=1;
SierpinskiArrowheadCurve(Image,8, Size);
end;
 
</syntaxhighlight>
{{out}}
<pre>
Elapsed Time: 50.555 ms.
 
</pre>
 
=={{header|EasyLang}}==
 
[https://easylang.dev/show/#cod=hZBNDgIhDIX3PcVL3PgTEE3GiQsOQxCVBMEMOs7c3qKMi9nYBen7Wl6bDtBoaOR3p8jECycHRcFH9/KnxxVKNrQA3btkYZ9dj4QAAwlJAPyZtYYqOceAjYZNGcVojVDxWHD2cYbLEP4yFulCdhUnCF5m6sFWY1/Fb/5SmFVlxZLdzbzF/KlPFpIk3VL/XeRTbXFUEHyENw== Run it]
 
<syntaxhighlight lang="easylang">
x = 5
y = 10
ang = 60
linewidth 0.5
#
proc curv o l a . .
if o = 0
x += cos ang * l
y += sin ang * l
line x y
else
o -= 1
l /= 2
curv o l (-a)
ang += a
curv o l a
ang += a
curv o l (-a)
.
.
move x y
curv 7 90 -60
</syntaxhighlight>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: accessors L-system ui ;
 
: arrowhead ( L-system -- L-system )
Line 430 ⟶ 730:
} >>rules ;
 
[ <L-system> arrowhead "Arrowhead" open-window ] with-ui</langsyntaxhighlight>
 
 
Line 462 ⟶ 762:
| x || iterate L-system
|}
 
 
=={{header|Forth}}==
Line 469 ⟶ 768:
 
===ASCII===
<langsyntaxhighlight lang="forth">( ASCII output with use of ANSI terminal control )
 
: draw-line ( direction -- )
Line 503 ⟶ 802:
;
 
5 arrowhead</langsyntaxhighlight>
 
{{out}}
Line 544 ⟶ 843:
 
===SVG file===
<langsyntaxhighlight lang="forth">( SVG ouput )
 
: draw-line ( direction -- ) \ line-length=10 ; sin(60)=0.87 ; cos(60)=0.5
Line 593 ⟶ 892:
;
 
5 arrowhead</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|XPL0}}
<syntaxhighlight lang="vb">#define pi 4 * Atn(1)
#define yellow Rgb(255,255,0)
 
Dim Shared As Integer posX, posY
Dim Shared As Single direc
 
Sub Curva(orden As Byte, largo As Single, angulo As Integer)
If orden = 0 Then
posX += Fix(largo * Cos(direc))
posY -= Fix(largo * Sin(direc))
Line - (posX, posY), yellow
Else
Curva(orden-1, largo/2, -angulo)
direc += angulo
Curva(orden-1, largo/2, +angulo)
direc += angulo
Curva(orden-1, largo/2, -angulo)
End If
End Sub
 
Screenres 640, 480, 32
 
Dim As Integer x, y
Dim As Byte orden = 5
Dim As Integer tam = 2^orden
Dim As Single largo = 300
Dim As Single sixty = pi/3
 
direc = 0
posX = 640/4
posY = 3*480/4
Pset (posX, posY)
 
If (orden And 1) = 0 Then
Curva(orden, largo, +sixty)
Else
direc += sixty
Curva(orden, largo, -sixty)
End If
 
Windowtitle "Hit any key to end program"
Sleep</syntaxhighlight>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/L-system}}
 
'''Solution'''
 
It can be done using an [[wp:L-system|L-system]]. There are generic functions written in Fōrmulæ to compute an L-system in the page [[L-system#Fōrmulæ | L-system]].
 
The program that creates a Sierpiński arrowhead is:
 
[[File:Fōrmulæ - L-system - Sierpiński triangle (rounded) 01.png]]
 
[[File:Fōrmulæ - L-system - Sierpiński triangle (rounded) 02.png]]
 
=={{header|Go}}==
Line 601 ⟶ 957:
{{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 666 ⟶ 1,022:
dc.Stroke()
dc.SavePNG("sierpinski_arrowhead_curve.png")
}</langsyntaxhighlight>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "Sierpin2.bas"
110 LET LEVEL=5
120 OPTION ANGLE DEGREES
130 SET VIDEO MODE 1:SET VIDEO COLOUR 0:SET VIDEO X 40:SET VIDEO Y 27
140 OPEN #101:"video:"
150 SET PALETTE BLACK,YELLOW
160 DISPLAY #101:AT 1 FROM 1 TO 27
170 PLOT 1180,20,ANGLE 180;
180 CALL SIERP(560,LEVEL)
190 DEF CURVE(D,A,LEV)
200 IF LEV=0 THEN
210 PLOT FORWARD D;
220 ELSE
230 CALL CURVE(D/2,-A,LEV-1)
240 PLOT RIGHT A;
250 CALL CURVE(D/2,A,LEV-1)
260 PLOT RIGHT A;
270 CALL CURVE(D/2,-A,LEV-1)
280 END IF
290 END DEF
300 DEF SIERP(D,LEV)
310 CALL CURVE(D,60,LEV)
320 PLOT LEFT 60;
330 CALL CURVE(D,-60,LEV)
340 PLOT LEFT 60;
350 CALL CURVE(D,60,LEV)
360 END DEF</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
import java.awt.Point;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public final class SierpinskiArrowhead {
 
public static void main(String[] aArgs) throws IOException {
List<Point> points = initialStraightLine();
for ( int i = 1; i < 8; i++ ) {
points = nextIteration(points);
}
String text = sierpinskiArrowheadText(points, IMAGE_SIZE);
Files.write(Paths.get("sierpinkskiArrowhead.svg"), text.getBytes());
}
private static List<Point> initialStraightLine() {
final int margin = 50;
final int boxSize = IMAGE_SIZE - 2 * margin;
final int x = margin;
final int y = Math.round(( IMAGE_SIZE + SQRT3_2 * boxSize ) / 2.0F);
List<Point> points = Arrays.asList( new Point(x, y), new Point(x + boxSize, y) );
return points;
}
private static List<Point> nextIteration(List<Point> aPoints) {
List<Point> result = new ArrayList<Point>();
for ( int i = 0; i < aPoints.size() - 1; i++ ) {
final int x0 = aPoints.get(i).x;
final int y0 = aPoints.get(i).y;
final int x1 = aPoints.get(i + 1).x;
final int y1 = aPoints.get(i + 1).y;
final int dx = x1 - x0;
result.add( new Point(x0, y0) );
if ( y0 == y1 ) {
final float d = Math.abs(dx) * SQRT3_2 / 2;
result.add( new Point(x0 + dx / 4, Math.round(y0 - d)) );
result.add( new Point(x1 - dx / 4, Math.round(y0 - d)) );
} else if ( y1 < y0 ) {
result.add( new Point(x1, y0));
result.add( new Point(x1 + dx / 2, ( y0 + y1 ) / 2) );
} else {
result.add( new Point(x0 - dx / 2, ( y0 + y1 ) / 2) );
result.add( new Point(x0, y1) );
}
}
result.add(aPoints.get(aPoints.size() - 1));
return result;
}
private static String sierpinskiArrowheadText(List<Point> aPoints, int aSize) {
StringBuilder text = new StringBuilder();
text.append("<svg xmlns='http://www.w3.org/2000/svg'");
text.append(" width='" + aSize + "' height='" + aSize + "'>\n");
text.append("<rect width='100%' height='100%' fill='white'/>\n");
text.append("<path stroke-width='1' stroke='black' fill='white' d='");
for ( int i = 0; i < aPoints.size(); i++ ) {
text.append(( i == 0 ? "M" : "L" ) + aPoints.get(i).x + ", " + aPoints.get(i).y + " ");
}
text.append("'/>\n</svg>\n");
return text.toString();
}
private static final float SQRT3_2 = (float) Math.sqrt(3.0F) / 2.0F;
private static final int IMAGE_SIZE = 700;
 
}
</syntaxhighlight>
{{out}}
[[Media:SierpinskiArrowheadJava.svg]]
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
This entry uses an L-system and turtle graphics to generate an SVG
file, which can be viewed using a web browser, at least if the file type is `.svg`.
 
See [[Category_talk:Jq-turtle]] for the turtle.jq module used here.
Please note that the `include` directive may need to be modified
depending on the location of the included file, and the command-line
options used.
<syntaxhighlight lang="jq">include "turtle" {search: "."};
 
# Compute the curve using a Lindenmayer system of rules
def rules:
{X: "Yf+Xf+Y", Y: "Xf-Yf-X", "": "X"};
 
def sierpinski($count):
rules as $rules
| def repeat($count):
if $count == 0 then .
else ascii_downcase | gsub("x"; $rules["X"]) | gsub("y"; $rules["Y"])
| repeat($count-1)
end;
$rules[""] | repeat($count) ;
 
def interpret($x):
if $x == "+" then turtleRotate(60)
elif $x == "-" then turtleRotate(-60)
elif $x == "f" then turtleForward(3)
else .
end;
 
def sierpinski_curve($n):
sierpinski($n)
| split("")
| reduce .[] as $action (
turtle([0,-350]) | turtleDown | turtleRotate(60);
interpret($action) ) ;
 
# viewBox = <min-x> <min-y> <width> <height>
# Input: {svg, minx, miny, maxx, maxy}
def svg:
"<svg viewBox='\(.minx|floor) \(.miny - 4 |floor) \(.maxx - .minx|ceil) \(6 + .maxy - .miny|ceil)'",
" preserveAspectRatio='xMinYmin meet'",
" xmlns='http://www.w3.org/2000/svg' >",
path("none"; "red"; 1),
"</svg>";
 
sierpinski_curve(8)
| svg
</syntaxhighlight>
 
=={{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 683 ⟶ 1,203:
showpreview = true
)
</syntaxhighlight>
</lang>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def sierp
{def sierp.r
{lambda {:order :length :angle}
{if {= :order 0}
then M:length
else {sierp.r {- :order 1} {/ :length 2} {- :angle}}
T:angle
{sierp.r {- :order 1} {/ :length 2} :angle}
T:angle
{sierp.r {- :order 1} {/ :length 2} {- :angle}}
}}}
{lambda {:order :length}
{if {= {% :order 2} 0}
then {sierp.r :order :length 60}
else T60
{sierp.r :order :length -60}
}}}
 
the output can be seen in http://lambdaway.free.fr/lambdawalks/?view=sierpinsky
</syntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[DoStep]
DoStep[Line[{x_, y_}]] := Module[{diff, perp, pts},
diff = y - x;
perp = Cross[diff] Sqrt[3]/2;
pts = {x, x + diff/4 + perp/2, x + 3 diff/4 + perp/2, y};
{Line[pts[[{2, 1}]]], Line[pts[[{2, 3}]]], Line[pts[[{4, 3}]]]}
]
lns = {Line[{{0.0, 0.0}, {1.0, 0.0}}]};
lns = Nest[Catenate[DoStep /@ #] &, lns, 5];
Graphics[lns]</syntaxhighlight>
 
=={{header|Nim}}==
{{trans|C++}}
Output is an SVG file.
<syntaxhighlight lang="nim">import math
 
const Sqrt3_2 = sqrt(3.0) / 2.0
 
type Point = tuple[x, y: float]
 
 
func sierpinskiArrowheadNext(points: seq[Point]): seq[Point] =
result.setLen(3 * (points.len - 1) + 1)
var j = 0
for i in 0..<points.high:
let (x0, y0) = points[i]
let (x1, y1) = points[i + 1]
let dx = x1 - x0
result[j] = (x0, y0)
if y0 == y1:
let d = abs(dx * Sqrt3_2 / 2)
result[j + 1] = (x0 + dx / 4, y0 - d)
result[j + 2] = (x1 - dx / 4, y0 - d)
elif y1 < y0:
result[j + 1] = (x1, y0)
result[j + 2] = (x1 + dx / 2, (y0 + y1) / 2)
else:
result[j + 1] = (x0 - dx / 2, (y0 + y1) / 2)
result[j + 2] = (x0, y1)
inc j, 3
result[j] = points[^1]
 
 
proc writeSierpinskiArrowhead(outfile: File; size, iterations: int) =
outfile.write "<svg xmlns='http://www.w3.org/2000/svg' width='", size, "' height='", size, "'>\n"
outfile.write "<rect width='100%' height='100%' fill='white'/>\n"
outfile.write "<path stroke-width='1' stroke='black' fill='none' d='"
const Margin = 20.0
let side = size.toFloat - 2 * Margin
let x = Margin
let y = 0.5 * size.toFloat + 0.5 * Sqrt3_2 * side
var points = @[(x: x, y: y), (x: x + side, y: y)]
for _ in 1..iterations:
points = sierpinskiArrowheadNext(points)
for i, point in points:
outfile.write if i == 0: 'M' else: 'L', point.x, ',', point.y, '\n'
outfile.write "'/>\n</svg>\n"
 
 
let outfile = open("sierpinski_arrowhead.svg", fmWrite)
outfile.writeSierpinskiArrowhead(600, 8)
outfile.close()</syntaxhighlight>
 
{{out}}
See output of C++ program.
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use SVG;
Line 726 ⟶ 1,335:
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)
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
<langYou Phix>--can demo\rosetta\run this online [http://phix.x10.mx/p2js/Sierpinski_arrowhead_curve.exwhtm here], and experiment with [shift] +/- as noted below.
<!--<syntaxhighlight lang="phix">(phixonline)-->
--
<span style="color: #000080;font-style:italic;">--
-- Draws curves lo to hi (simultaneously), initially {6,6}, max {10,10}
-- demo\rosetta\Sierpinski_arrowhead_curve.exw
-- Press +/- to change hi, shift +/- to change lo.
-- ===========================================
-- ("=_" are also mapped to "+-", for the non-numpad +/-)
--
-- Draws curves lo to hi (simultaneously), initially {6,6}, max {10,10}, min {1,1}
include pGUI.e
-- Press +/- to change hi, shift +/- to change lo.
 
-- ("=_" are also mapped to "+-", for the non-numpad +/-)
Ihandle dlg, canvas
--</span>
cdCanvas cddbuffer, cdcanvas
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
integer width, height,
lo = 6, hi = 6
<span style="color: #004080;">Ihandle</span> <span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">canvas</span>
atom cx, cy, h, theta
<span style="color: #004080;">cdCanvas</span> <span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span>
 
integer iy = +1
<span style="color: #004080;">integer</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">,</span>
 
<span style="color: #000000;">lo</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">hi</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">6</span>
procedure draw_line(atom l)
<span style="color: #004080;">atom</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cy</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">h</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">theta</span>
cdCanvasVertex(cddbuffer, cx-width/2+h, (height-cy)*iy+2*h)
cx += l*cos(theta*CD_DEG2RAD)
<span style="color: #004080;">integer</span> <span style="color: #000000;">iy</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">+</span><span style="color: #000000;">1</span>
cy += l*sin(theta*CD_DEG2RAD)
end procedure
<span style="color: #008080;">procedure</span> <span style="color: #000000;">draw_line</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
 
<span style="color: #7060A8;">cdCanvasVertex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;">-</span><span style="color: #000000;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">+</span><span style="color: #000000;">h</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">height</span><span style="color: #0000FF;">-</span><span style="color: #000000;">cy</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">iy</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">h</span><span style="color: #0000FF;">)</span>
procedure turn(integer angle)
<span style="color: #000000;">cx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">cos</span><span style="color: #0000FF;">(</span><span style="color: #000000;">theta</span><span style="color: #0000FF;">*</span><span style="color: #004600;">CD_DEG2RAD</span><span style="color: #0000FF;">)</span>
theta = mod(theta+angle,360)
<span style="color: #000000;">cy</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">*</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">theta</span><span style="color: #0000FF;">*</span><span style="color: #004600;">CD_DEG2RAD</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
 
procedure curve(integer order, atom l, integer angle)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">turn</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
if order=0 then
<span style="color: #000000;">theta</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mod</span><span style="color: #0000FF;">(</span><span style="color: #000000;">theta</span><span style="color: #0000FF;">+</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">,</span><span style="color: #000000;">360</span><span style="color: #0000FF;">)</span>
draw_line(l)
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
else
curve(order-1, l/2, -angle)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">curve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">order</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
turn(angle)
<span style="color: #008080;">if</span> <span style="color: #000000;">order</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
curve(order-1, l/2, angle)
<span style="color: #000000;">draw_line</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
turn(angle)
<span style="color: #008080;">else</span>
curve(order-1, l/2, -angle)
<span style="color: #000000;">curve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">turn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
end procedure
<span style="color: #000000;">curve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
 
<span style="color: #000000;">turn</span><span style="color: #0000FF;">(</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
procedure sierpinski_arrowhead_curve(integer order, atom l)
<span style="color: #000000;">curve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">angle</span><span style="color: #0000FF;">)</span>
-- If order is even we can just draw the curve.
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if and_bits(order,1)=0 then
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
curve(order, l, +60)
else -- order is odd
<span style="color: #008080;">procedure</span> <span style="color: #000000;">sierpinski_arrowhead_curve</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">order</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
turn( +60)
<span style="color: #000080;font-style:italic;">-- If order is even we can just draw the curve.</span>
curve(order, l, -60)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">curve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">+</span><span style="color: #000000;">60</span><span style="color: #0000FF;">)</span>
draw_line(l)
<span style="color: #008080;">else</span> <span style="color: #000080;font-style:italic;">-- order is odd</span>
end procedure
<span style="color: #000000;">turn</span><span style="color: #0000FF;">(</span> <span style="color: #0000FF;">+</span><span style="color: #000000;">60</span><span style="color: #0000FF;">)</span>
 
<span style="color: #000000;">curve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">60</span><span style="color: #0000FF;">)</span>
function redraw_cb(Ihandle /*ih*/, integer /*posx*/, integer /*posy*/)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
{width, height} = IupGetIntInt(canvas, "DRAWSIZE")
<span style="color: #000000;">draw_line</span><span style="color: #0000FF;">(</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
cdCanvasActivate(cddbuffer)
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
for order=lo to hi do
cx = width/2
<span style="color: #008080;">function</span> <span style="color: #000000;">redraw_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000080;font-style:italic;">/*posx*/</span><span style="color: #0000FF;">,</span> <span style="color: #000080;font-style:italic;">/*posy*/</span><span style="color: #0000FF;">)</span>
cy = height
<span style="color: #0000FF;">{</span><span style="color: #000000;">width</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">height</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetIntInt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"DRAWSIZE"</span><span style="color: #0000FF;">)</span>
h = cx/2
<span style="color: #7060A8;">cdCanvasActivate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
theta = 0
<span style="color: #008080;">for</span> <span style="color: #000000;">order</span><span style="color: #0000FF;">=</span><span style="color: #000000;">lo</span> <span style="color: #008080;">to</span> <span style="color: #000000;">hi</span> <span style="color: #008080;">do</span>
iy = iff(and_bits(order,1)?-1:+1)
<span style="color: #000000;">cx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">width</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span>
cdCanvasBegin(cddbuffer, CD_OPEN_LINES)
<span style="color: #000000;">cy</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">height</span>
sierpinski_arrowhead_curve(order, cx)
<span style="color: #000000;">h</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span>
cdCanvasEnd(cddbuffer)
<span style="color: #000000;">theta</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
end for
<span style="color: #000000;">iy</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)?-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">:+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
cdCanvasFlush(cddbuffer)
<span style="color: #7060A8;">cdCanvasBegin</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_OPEN_LINES</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #000000;">sierpinski_arrowhead_curve</span><span style="color: #0000FF;">(</span><span style="color: #000000;">order</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cx</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #7060A8;">cdCanvasEnd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
function map_cb(Ihandle ih)
<span style="color: #7060A8;">cdCanvasFlush</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
cdcanvas = cdCreateCanvas(CD_IUP, ih)
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
cddbuffer = cdCreateCanvas(CD_DBUFFER, cdcanvas)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
cdCanvasSetBackground(cddbuffer, CD_WHITE)
cdCanvasSetForeground(cddbuffer, CD_BLUE)
<span style="color: #008080;">function</span> <span style="color: #000000;">map_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
return IUP_DEFAULT
<span style="color: #000000;">cdcanvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_IUP</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ih</span><span style="color: #0000FF;">)</span>
end function
<span style="color: #000000;">cddbuffer</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">cdCreateCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">CD_DBUFFER</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cdcanvas</span><span style="color: #0000FF;">)</span>
 
<span style="color: #7060A8;">cdCanvasSetBackground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_WHITE</span><span style="color: #0000FF;">)</span>
function key_cb(Ihandle /*ih*/, atom c)
<span style="color: #7060A8;">cdCanvasSetForeground</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CD_BLUE</span><span style="color: #0000FF;">)</span>
if c=K_ESC then return IUP_CLOSE end if
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
if find(c,"+=-_") then
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
bool bShift = IupGetInt(NULL,"SHIFTKEY")
if c='+' or c='=' then
<span style="color: #008080;">function</span> <span style="color: #000000;">key_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
if bShift then
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_ESC</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CLOSE</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
lo = min(lo+1,hi)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"+=-_"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
else
<span style="color: #004080;">bool</span> <span style="color: #000000;">bShift</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupGetInt</span><span style="color: #0000FF;">(</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"SHIFTKEY"</span><span style="color: #0000FF;">)</span>
hi = min(10,hi+1)
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'+'</span> <span style="color: #008080;">or</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'='</span> <span style="color: #008080;">then</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">bShift</span> <span style="color: #008080;">then</span>
elsif c='-' or c='_' then
<span style="color: #000000;">lo</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lo</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hi</span><span style="color: #0000FF;">)</span>
if bShift then
<span lo style="color: max(1,lo-1)#008080;">else</span>
<span style="color: #000000;">hi</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hi</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
else
<span style="color: #008080;">end</span> hi<span style="color: max(lo,hi-1)#008080;">if</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'-'</span> <span style="color: #008080;">or</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'_'</span> <span style="color: #008080;">then</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">bShift</span> <span style="color: #008080;">then</span>
end if
<span style="color: #000000;">lo</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lo</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
IupSetStrAttribute(dlg, "TITLE", "Sierpinski arrowhead curve (%d..%d)",{lo,hi})
<span style="color: #008080;">else</span>
cdCanvasClear(cddbuffer)
<span style="color: #000000;">hi</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lo</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hi</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
IupUpdate(canvas)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return IUP_CONTINUE
<span style="color: #7060A8;">IupSetStrAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Sierpinski arrowhead curve (%d..%d)"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">lo</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hi</span><span style="color: #0000FF;">})</span>
end function
<span style="color: #7060A8;">cdCanvasClear</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cddbuffer</span><span style="color: #0000FF;">)</span>
 
<span style="color: #7060A8;">IupUpdate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
procedure main()
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
IupOpen()
<span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
canvas = IupCanvas(NULL)
IupSetAttribute(canvas, "RASTERSIZE", "770x770")
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
IupSetCallback(canvas, "MAP_CB", Icallback("map_cb"))
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
IupSetCallback(canvas, "ACTION", Icallback("redraw_cb"))
 
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #004600;">NULL</span><span style="color: #0000FF;">)</span>
dlg = IupDialog(canvas)
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"RASTERSIZE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"770x770"</span><span style="color: #0000FF;">)</span>
IupSetAttribute(dlg, "TITLE", "Sierpinski arrowhead curve (6..6)")
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"MAP_CB"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"map_cb"</span><span style="color: #0000FF;">))</span>
IupSetCallback(dlg, "K_ANY", Icallback("key_cb"))
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"ACTION"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"redraw_cb"</span><span style="color: #0000FF;">))</span>
 
IupMap(dlg)
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
IupShowXY(dlg,IUP_CENTER,IUP_CENTER)
<span style="color: #7060A8;">IupSetAttribute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"TITLE"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Sierpinski arrowhead curve (6..6)"</span><span style="color: #0000FF;">)</span>
IupMainLoop()
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"K_ANY"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"key_cb"</span><span style="color: #0000FF;">))</span>
IupClose()
end procedure
<span style="color: #7060A8;">IupMap</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">)</span>
 
<span style="color: #7060A8;">IupShowXY</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span><span style="color: #004600;">IUP_CENTER</span><span style="color: #0000FF;">,</span><span style="color: #004600;">IUP_CENTER</span><span style="color: #0000FF;">)</span>
main()</lang>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
 
<span style="color: #7060A8;">IupMainLoop</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;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
 
=={{header|Processing}}==
<langsyntaxhighlight lang="java">final PVector t = new PVector(20, 30, 60);
 
void setup() {
Line 871 ⟶ 1,487:
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 898 ⟶ 1,514:
 
return s
</syntaxhighlight>
</lang>
 
'''The sketch can be run online''' :<BR> [https://Trinket.io/processing/de6eddb155 here.]
 
=={{header|Python}}==
<syntaxhighlight lang="python">
import matplotlib.pyplot as plt
import math
 
 
def nextPoint(x, y, angle):
a = math.pi * angle / 180
x2 = (int)(round(x + (1 * math.cos(a))))
y2 = (int)(round(y + (1 * math.sin(a))))
return x2, y2
 
 
def expand(axiom, rules, level):
for l in range(0, level):
a2 = ""
for c in axiom:
if c in rules:
a2 += rules[c]
else:
a2 += c
axiom = a2
return axiom
 
 
def draw_lsystem(axiom, rules, angle, iterations):
xp = [1]
yp = [1]
direction = 0
for c in expand(axiom, rules, iterations):
if c == "F":
xn, yn = nextPoint(xp[-1], yp[-1], direction)
xp.append(xn)
yp.append(yn)
elif c == "-":
direction = direction - angle
if direction < 0:
direction = 360 + direction
elif c == "+":
direction = (direction + angle) % 360
 
plt.plot(xp, yp)
plt.show()
 
 
if __name__ == '__main__':
# Sierpinski Arrowhead Curve L-System Definition
s_axiom = "XF"
s_rules = {"X": "YF+XF+Y",
"Y": "XF-YF-X"}
s_angle = 60
 
draw_lsystem(s_axiom, s_rules, s_angle, 7)
</syntaxhighlight>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
{{Trans|C}}
<langsyntaxhighlight lang="prolog">main:-
write_sierpinski_arrowhead('sierpinski_arrowhead.svg', 600, 8).
 
Line 946 ⟶ 1,617:
curve(Stream, Order1, Length2, Cursor3, Cursor4, Angle),
turn(Cursor4, Angle, Cursor5),
curve(Stream, Order1, Length2, Cursor5, Cursor1, Angle1).</langsyntaxhighlight>
 
{{out}}
[[Media:Sierpinski_arrowhead_prolog.svg]]
See: [https://slack-files.com/T0CNUL56D-F01DPA9DNFP-53faf39637 sierpinski_arrowhead.svg] (offsite SVG image)
 
=={{header|Quackery}}==
 
Using an L-system.
 
<syntaxhighlight lang="quackery"> [ $ "turtleduck.qky" loadfile ] now!
[ stack ] is switch.arg ( --> [ )
[ switch.arg put ] is switch ( x --> )
[ switch.arg release ] is otherwise ( --> )
[ switch.arg share
!= iff ]else[ done
otherwise ]'[ do ]done[ ] is case ( x --> )
[ $ "" swap witheach
[ nested quackery join ] ] is expand ( $ --> $ )
[ $ "L" ] is L ( $ --> $ )
[ $ "R" ] is R ( $ --> $ )
[ $ "BLALB" ] is A ( $ --> $ )
[ $ "ARBRA" ] is B ( $ --> $ )
$ "A"
6 times expand
turtle
10 frames
witheach
[ switch
[ char L case [ -1 6 turn ]
char R case [ 1 6 turn ]
otherwise [ 4 1 walk ] ] ]
1 frames</syntaxhighlight>
 
{{output}}
 
[[File:Quackery Sierpinski arrowhead.png]]
 
=={{header|Raku}}==
Line 955 ⟶ 1,670:
{{works with|Rakudo|2020.02}}
 
<syntaxhighlight lang="raku" perl6line>use SVG;
 
role Lindenmayer {
Line 992 ⟶ 1,707:
: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,032 ⟶ 1,747:
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,075 ⟶ 1,790:
{{libheader|JRubyArt}}
For grammar see Hilbert Curve
<langsyntaxhighlight lang="ruby">
load_libraries :grammar
attr_reader :points
Line 1,159 ⟶ 1,874:
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,239 ⟶ 1,954:
fn main() {
write_sierpinski_arrowhead("sierpinski_arrowhead.svg", 600, 8).unwrap();
}</langsyntaxhighlight>
 
{{out}}
[[Media:Sierpinski_arrowhead_rust.svg]]
See: [https://slack-files.com/T0CNUL56D-F0173HHBYL9-af5ef387c5 sierpinski_arrowhead.svg] (offsite SVG image)
 
=={{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,264 ⟶ 1,979:
)
 
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,270 ⟶ 1,985:
{{trans|Go}}
{{libheader|DOME}}
<langsyntaxhighlight ecmascriptlang="wren">import "graphics" for Canvas, Color, Point
import "dome" for Window
 
Line 1,328 ⟶ 2,043:
}
}
}</langsyntaxhighlight>
 
{{out}}
[[File:Wren-Sierpinski_arrowhead_curve.png|400px]]
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">int PosX, PosY;
real Dir;
 
proc Curve(Order, Length, Angle);
int Order; real Length, Angle;
[if Order = 0 then
[PosX:= PosX + fix(Length*Cos(Dir));
PosY:= PosY - fix(Length*Sin(Dir));
Line(PosX, PosY, $E \yellow\);
]
else [Curve(Order-1, Length/2.0, -Angle);
Dir:= Dir + Angle;
Curve(Order-1, Length/2.0, +Angle);
Dir:= Dir + Angle;
Curve(Order-1, Length/2.0, -Angle);
];
];
 
def Order=5, Length=300.0, Pi=3.141592654, Sixty=Pi/3.0;
[SetVid($12); \VGA graphics: 640x480x8
PosX:= 640/4; PosY:= 3*480/4;
Move(PosX, PosY);
Dir:= 0.0;
if (Order&1) = 0 then
Curve(Order, Length, +Sixty)
else [Dir:= Dir + Sixty;
Curve(Order, Length, -Sixty);
];
]</syntaxhighlight>
 
{{out}}
<pre>
http://www.xpl0.org/rcarrow.gif
</pre>
 
=={{header|zkl}}==
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,363 ⟶ 2,117:
}
img.writeJPGFile("sierpinskiArrowheadCurve.zkl.jpg");
}</langsyntaxhighlight>
{{out}}
Offsite image at [http://www.zenkinetic.com/Images/RosettaCode/sierpinskiArrowheadCurve.zkl.jpg Sierpinski arrowhead curve order 7]
3,038

edits