Sierpinski square curve: Difference between revisions

→‎{{header|Quackery}}: added link to description of method
(Added FreeBASIC)
(→‎{{header|Quackery}}: added link to description of method)
 
(7 intermediate revisions by 4 users not shown)
Line 13:
V angle = 0.0
 
V outfile = File(fname, ‘w’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")
Line 39:
{{out}}
Output is similar to C++.
 
=={{header|ALGOL 68}}==
Generates an SVG file. The SVG generating code is translated from the FreeBASIC sample (which is a translation of the 11l sample which is translated from the C++). Uses the Algol 68 library for L-System related Tasks on Rosetta Code.
{{libheader|ALGOL 68-l-system}}
Note: The source of the Algol 68 L-System library is available on a separate page on Rosetta Code - see the above link and follow the link to the Talk (Discussion) page.
<syntaxhighlight lang="algol68">
BEGIN # Sierpinski Square Curve in SVG - SVG generation translated from the #
# FreeBASIC sample (which is a translation of C++) #
# 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 square curve = ( STRING fname, INT size, length, order )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 := ( size - length ) / 2;
REAL y := length;
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+XF+F+XF"
, ( "X" -> "XF-F+F-XF+F+XF-F+F-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 +:= 90 MODAB 360
ELIF c = "-" THEN
angle +:= 270 MODAB 360
FI
);
put( svg file, ( "'/>", newline, "</svg>", newline ) );
close( svg file )
FI # sierpinski square # ;
 
sierpinski square curve( "sierpinski_square.svg", 635, 5, 5 )
 
END
</syntaxhighlight>
{{out}}
Similar to FreeBasic, 11l, C++, etc.
 
=={{header|ALGOL W}}==
Line 324 ⟶ 393:
{{out}}
[[Media:Sierpinski_square_cpp.svg]]
 
=={{header|EasyLang}}==
[https://easylang.online/show/#cod=jZK9bsMgFIV3nuIIWR2KjOw2HTKw+hkiRR6oQxJUgi1wE+ftq1sbJ3EzdEFwz8f9ObpdaBu4eI1m6ODM2ThI6MG2pwzh25mYbWtIBmDfBjj07UhRBID2GRQ4n57ENBmsR+xDc9QhTrkmfUIsFErE3nR4GzP6udiNBGD3KW5rKDTZgwpQMTUjECjrJfEZjP5C+RCW7NmVZnm5K5KkeNFdskT7X1Uyybrk3C7oS9IHXKH9Ac4fIMcMznpzsbv+iEK+U+DUng2B7B+G2f04Iq/4rdEBQqFpI3Y24JVq3bQradH6Zxp1kgoDMG7Ont9lp4+5oin+cmLBiXtOJmcmL6hrsakEHZzNy6SwBd9w8E2VV6LKJ2K8c9QsbeNquYds6fZHgbLAukApV4z9AA== Run it]
 
<syntaxhighlight>
proc lsysexp level . axiom$ rules$[] .
for l to level
an$ = ""
for c$ in strchars axiom$
for i = 1 step 2 to len rules$[]
if rules$[i] = c$
c$ = rules$[i + 1]
break 1
.
.
an$ &= c$
.
swap axiom$ an$
.
.
proc lsysdraw axiom$ x y ang lng . .
linewidth 0.3
move x y
for c$ in strchars axiom$
if c$ = "F"
x += cos dir * lng
y += sin dir * lng
line x y
elif c$ = "-"
dir -= ang
elif c$ = "+"
dir += ang
.
.
.
axiom$ = "F+XF+F+XF"
rules$[] = [ "X" "XF-F+F-XF+F+XF-F+F-X" ]
lsysexp 4 axiom$ rules$[]
lsysdraw axiom$ 50 10 90 1.4
</syntaxhighlight>
 
=={{header|Factor}}==
Line 427 ⟶ 536:
{{out}}
<pre>Output is similar to C++.</pre>
 
=={{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's square curve is:
 
[[File:Fōrmulæ - L-system - Sierpiński's square curve 01.png]]
 
[[File:Fōrmulæ - L-system - Sierpiński's square curve 02.png]]
 
=={{header|Go}}==
Line 931 ⟶ 1,054:
 
=={{header|Quackery}}==
 
Method is described at [[L-system#Quackery]].
 
<syntaxhighlight lang="quackery"> [ $ "turtleduck.qky" loadfile ] now!
1,462

edits