Sierpinski arrowhead curve: Difference between revisions

m
m (Changed name of file in Media tag.)
 
(16 intermediate revisions by 8 users not shown)
Line 34:
V size = 600
V iterations = 8
V outfile = File(‘sierpinski_arrowhead.svg’, ‘w’WRITE)
 
outfile.write(‘<svg xmlns='http://www.w3.org/2000/svg' width='’size‘' height='’size"'>\n")
Line 120:
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}}==
Line 621 ⟶ 687:
 
</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}}==
Line 667 ⟶ 762:
| x || iterate L-system
|}
 
 
=={{header|Forth}}==
Line 800 ⟶ 894:
5 arrowhead</syntaxhighlight>
 
=={{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 872 ⟶ 1,023:
dc.SavePNG("sierpinski_arrowhead_curve.png")
}</syntaxhighlight>
 
=={{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}}==
Line 953 ⟶ 1,133:
}
</syntaxhighlight>
{{ out }}
[[Media:sierpinskiArrowheadJavaSierpinskiArrowheadJava.svg]]
 
=={{header|jq}}==
Line 1,805 ⟶ 1,985:
{{trans|Go}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color, Point
import "dome" for Window
 
Line 1,864 ⟶ 2,044:
}
}</syntaxhighlight>
 
{{out}}
[[File:Wren-Sierpinski_arrowhead_curve.png|400px]]
 
=={{header|XPL0}}==
3,028

edits