Dragon curve: Difference between revisions

Content deleted Content added
imported>Thebeez
Chkas (talk | contribs)
 
(8 intermediate revisions by 5 users not shown)
Line 351:
 
=={{header|ALGOL 68}}==
 
===Animated===
 
{{trans|python}}
<!-- {{works with|ALGOL 68|Standard - but ''draw'' is not part of the standard prelude}} -->
Line 523 ⟶ 526:
|}
Note: each Dragon curve is composed of many smaller dragon curves (shown in a different colour).
 
===L-System===
 
Alternative (monochrome) version using the L-System library.
 
{{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 # Dragon 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 dragon 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"
, ( "F" -> "F+S"
, "S" -> "F-S"
)
);
STRING curve = ssc EVAL order;
curve INTERPRET ( ( CHAR c )VOID:
IF c = "F" OR c = "S" 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 -:= 90 MODAB 360
FI
);
put( svg file, ( "'/>", newline, "</svg>", newline ) );
close( svg file )
FI # sierpinski square # ;
 
dragon curve( "dragon.svg", 1200, 5, 12, 400, 200 )
 
END
</syntaxhighlight>
 
=={{header|AmigaE}}==
Line 1,185 ⟶ 1,257:
EndIf
' draw a line
Proc _Line (a, b, Set (a, a + (((-1 * FUNC(_COS(r)))*a@)/10000)), Set (b, b + ((FUNC(_SIN(r))*a@)/10000)))
Return
 
Line 1,253 ⟶ 1,325:
_Canvas ' set up a canvas x wide and y high
Param (2)
 
Write @o(0), "<svg width=\q";a@;"\q height=\q";b@;"\q viewBox=\q0 0 ";a@;" ";b@;
Write @o(0), "\q xmlns=\qhttp://www.w3.org/2000/svg\q ";
Line 1,278 ⟶ 1,351:
IF A@ THEN PUSH -POP()
RETURN
' return COS(x*10K), scaled by 10K
_COS PARAM(1) : PUSH ABS(A@%62832) : IF TOS()>31416 THEN PUSH 62832-POP()
Line 2,638 ⟶ 2,710:
=={{header|EasyLang}}==
 
[https://easylang.devonline/show/#cod=jU7NDoIwDL73Kb7Em4ZZSDDxwMMQNnHJ3HQjCD69ZWDiwYO9tF/7/bQLLkRwzeSsN0+rhytY1TShQVXTLO3EdAujwYSZWt87IzumHegeQwcd2z54JPsycGaEhoIiAPaS8cJFrglFgy4krCb7rNluMw6NYP/rtjyWw2U2Ln3W38FHpEccUOXEAiXKjbTaSa4WzzP/Iy2yVpGijXZilJU4vgE= Run it]
 
<syntaxhighlight lang="text">
color 050
linewidth 0.5
Line 3,063 ⟶ 3,135:
 
'''Solution'''
 
=== Recursive ===
 
[[File:Fōrmulæ - Dragon curve 01.png]]
Line 3,071 ⟶ 3,145:
 
[[File:Fōrmulæ - Dragon curve 03.png]]
 
=== 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 Dragon curve is:
 
[[File:Fōrmulæ - L-system - Dragon curve 01.png]]
 
[[File:Fōrmulæ - L-system - Dragon curve 02.png]]
 
Rounded version:
 
[[File:Fōrmulæ - L-system - Dragon curve (rounded) 01.png]]
 
[[File:Fōrmulæ - L-system - Dragon curve (rounded) 02.png]]
 
=={{header|Gnuplot}}==
Line 5,265 ⟶ 5,355:
close;
end.</syntaxhighlight>
 
 
=={{header|PascalABC.NET}}==
<syntaxhighlight lang="delphi">
uses Turtle;
 
var
Atom,FStr,XStr,YStr: string;
angle,len,x0,y0: real;
n: integer;
 
procedure Init1; // Dragon
begin
(Atom,FStr,XStr,YStr) := ('fx','f','x+yf+','-fx-y');
(angle,len,n,x0,y0) := (90,3,15,300,450);
end;
 
procedure RunStr(s: string; n: integer);
begin
foreach var c in s do
case c of
'+': Turn(angle);
'-': Turn(-angle);
'f','F': if n>0 then RunStr(FStr,n-1) else Forw(len);
'x','X': if n>0 then RunStr(XStr,n-1);
'y','Y': if n>0 then RunStr(YStr,n-1);
else Print('error')
end;
end;
 
begin
Init1;
ToPoint(x0,y0);
SetWidth(0.5);
Down;
RunStr(Atom,n);
Up;
end.
</syntaxhighlight>
 
 
Line 7,351 ⟶ 7,480:
{{trans|Kotlin}}
{{libheader|DOME}}
<syntaxhighlight lang="ecmascriptwren">import "graphics" for Canvas, Color
import "dome" for Window