Sierpinski triangle/Graphical: Difference between revisions

m
(Added Fōrmulæ entry)
 
(6 intermediate revisions by 2 users not shown)
Line 226:
 
}
</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 Triangle 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 triangle 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 = ( "F-G-G"
, ( "F" -> "F-G+F+G-F"
, "G" -> "GG"
)
);
STRING curve = ssc EVAL order;
curve INTERPRET ( ( CHAR c )VOID:
IF c = "F" OR c = "G" 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 +:= 120 MODAB 360
ELIF c = "-" THEN
angle -:= 120 MODAB 360
FI
);
put( svg file, ( "'/>", newline, "</svg>", newline ) );
close( svg file )
FI # sierpinski square # ;
 
sierpinski triangle curve( "sierpinski_triangle.svg", 1200, 12, 5, 200, 400 )
 
END
</syntaxhighlight>
 
Line 1,338 ⟶ 1,405:
'''Solution'''
 
=== By L-system ===
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]].
 
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 triangle is:
 
The programscript that creates a Sierpiński triangle is:
 
[[File:Fōrmulæ - L-system - Sierpiński triangle 01.png]]
Line 1,346 ⟶ 1,415:
[[File:Fōrmulæ - L-system - Sierpiński triangle 02.png]]
 
=== By chaos game ===
'''Rounded version'''
 
There is a function written in Fōrmulæ to generate fractals by chaos game in the page [[Chaos game#Fōrmulæ | chaos game]].
 
The script that creates a Sierpiński triangle is:
 
[[File:Fōrmulæ - Chaos game 05.png]]
 
[[File:Fōrmulæ - Chaos game 06.png]]
 
=== By Kronecker product ===
 
There is a function written in Fōrmulæ to create generic Kronecker product based fractal in the page [[Kronecker product based fractals#Fōrmulæ | Kronecker product based fractals]].
 
The script that creates a Sierpiński triangle is:
 
[[File:Fōrmulæ - Kronecker product based fractals 08.png]]
 
[[File:Fōrmulæ - Kronecker product based fractals 09.png]]
 
=== By elementary cellular automaton ===
 
There is a function written in Fōrmulæ to create images for the elementary cellular automaton in the page [[Elementary cellular automaton#Fōrmulæ | Elementary cellular automaton]].
 
All the rules 18, 22 , 23, 60, 82, 90, 102, 126, 129, 146, 153, 154, 161, 165, 167, 181, 182, 195, 210 and 218 produce Sierpiński triangles:
 
[[File:Fōrmulæ - L-systemElementary cellular automaton - Sierpiński triangle (rounded) 01.png]]
 
[[File:Fōrmulæ - L-systemElementary cellular automaton - Sierpiński triangle (rounded) 02.png]]
 
=={{header|gnuplot}}==
2,120

edits