0% found this document useful (0 votes)
19 views17 pages

Maxbox Starter132 Dragon Curve Sound2wordpress

I want to show the dragon curve in five language soluitons: 1. Pascal 2. Python 3. Turtle 4. Delphi 5. Java Script A dragon curve is any member of a family of self-similar fractal curves, which can be approximated by recursive methods such as Lindenmayer systems as a procedure.

Uploaded by

Max Kleiner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views17 pages

Maxbox Starter132 Dragon Curve Sound2wordpress

I want to show the dragon curve in five language soluitons: 1. Pascal 2. Python 3. Turtle 4. Delphi 5. Java Script A dragon curve is any member of a family of self-similar fractal curves, which can be approximated by recursive methods such as Lindenmayer systems as a procedure.

Uploaded by

Max Kleiner
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Dragon Curve

Aug 7, 2024 code, machine learning, maxbox, script

# script # maxbox # graphics # multilanguage, graphics, maxbox, multilanguage, script

I want to show the dragon curve in five language soluitons:

1. Pascal
2. Python
3. Turtle
4. Delphi
5. Java Script

A dragon curve is any member of a family of self-similar fractal curves, which can be
approximated by recursive methods such as Lindenmayer systems as a procedure in
Pascal shows:

1 procedure Dragon(n,a,t: Integer; d,x,y: Double; var b: TBitmap);


2 var a1, a2: integer;
3 begin
4 if n <= 1 then begin
5 with b.Canvas do begin
6 Pen.Color:= random(p);
7 MoveTo(Trunc(x + 0.5), Trunc(y + 0.5));
8 LineTo(Trunc(x + d *_cos[a]+0.5),Trunc(y + d *_sin[a]+0.5));
9 exit;
10 end;
11 end;
12 d:= d * s;
13 a1:= (a - t) and 7;
14 a2:= (a + t) and 7;
15 dragon(n - 1, a1, 1, d, x, y, b);
16 dragon(n - 1, a2, -1, d, x + d *_cos[a1], y + d *_sin[a1], b);
17 end;

Recursively a right curling dragon is a right dragon followed by a left dragon, at 90-
degree angle. And a left dragon is a left followed by a right. The same you get also with
Python and Turtle in maXbox:

1 Const DRAGFUNC =
2 'def dragon(level=4, size=200, direction=45): '+LF+
3 ' if level: '+LF+
4 ' right(direction) '+LF+
5 ' dragon(level-1, size/1.41421356237, 45) '+LF+
6 ' left(direction * 2) '+LF+
7 ' dragon(level-1, size/1.41421356237, -45) '+LF+
8 ' right(direction) '+LF+
9 ' else: '+LF+
10 ' forward(size) ';
11
12 function PyCodeDragonTurtle(imgpath, aAPIKey: string): string;
13 begin
14 with TPythonEngine.Create(Nil) do begin
15 //pythonhome:= 'C:\Users\User\AppData\Local\Programs\Python\Python
16 try
17 loadDLL;
18 autofinalize:= false;
19 ExecString('from turtle import right,left,forward,speed, exitonc
20 ExecStr(DRAGFUNC);
21 ExecStr('speed(0)');
22 //ExecStr('hideturtle()');
23 ExecStr('dragon(6)');
24 ExecStr('exitonclick()');
25 //result:= (EvalStr('r.json()')); *)
26 except
27 raiseError;
28 finally
29 Free;
30 end;
31 end;
32 end;

The dragon curve is probably most commonly thought of as the shape that is
generated from repeatedly folding a strip of paper in half.
 

Dragon as Pen.Color:= (p);

The script you get at:


Multilanguage Script

https://sourceforge.net/projects/maxbox/files/Examples/13_General/1320_dragon_curve_51_py.txt/download
with Depth = 9

and with Depth = 13

Python Pascal

Pen.Width:= 2; Pen.Color := cllime;

Turtle Sound
Try the sound that turtle with angle as amplitude and step as tone

1 procedure DrawDragon(step, adir: integer; leng:real);


2 begin
3 //myturtle:= TJvTurtle.create(self);
4 with myturtle do begin
5 if (step > -1) and (leng > 1) then begin
6 leng:= leng/sqrt(2);
7 Turn(45*adir);
8 DrawDragon(step-1, +1, leng);
9 Turn(-90*adir);
10 DrawDragon(step-1, -1, leng);
11 Turn(45*adir);
12 end else //*)
13 //Turtle.Draw(length);
14 moveforward(leng)
15 end;
16 end;

1 procedure DrawDragon2(step, adir: integer; len:real);


2 begin
3 //myturtle:= TJvTurtle.create(self);
4 with myturtle do begin
5 if (step >-1) and (len >1) then begin
6 len:= len /sqrt(2);
7 Turn(45*adir);
8 DrawDragon2(step-1, +1, len);
9 Turn(-90*adir);
10 DrawDragon2(step-1, -1, len);
11 Turn(45*adir);
12 end else //*)
13 moveforward(len )
14 end;
15 end;
1 procedure DrawDragon2Sound(step, adir: integer; len:real);
2 begin
3 //myturtle:= TJvTurtle.create(self);
4 with myturtle do begin
5 if (step >-1) and (len >1) then begin
6 len:= len /sqrt(2);
7 Turn(45*adir);
8 //beep2(440+45, 150)
9 DrawDragon2(step-1, +1, len);
10 Turn(-90*adir);
11 beep2(440-90, 150)
12 DrawDragon2(step-1, -1, len);
13 Turn(45*adir);
14 beep2(440+45, 150)
15 end else begin //*)
16 moveforward(len )
17 //beep2(440, 100)
18 end;
19 end;
20 end;

JavaScript Embedded

The Microsoft Edge WebView2 control allows you to embed web technologies (HTML,
CSS, and JavaScript) in your native apps. The WebView2 control uses Microsoft Edge as
the rendering engine to display the web content in native apps.

1 <!-- DragonCurve.html -->


2 <html>
3 <head>
4 <script type='text/javascript'>
5 function pDragon(cId) {
6 // Plotting Dragon curves. 2/25/17 aev
7 var n=document.getElementById('ord').value;
8 var sc=document.getElementById('sci').value;
9 var hsh=document.getElementById('hshi').value;
10 var vsh=document.getElementById('vshi').value;
11 var clr=document.getElementById('cli').value;
12 var c=c1=c2=c2x=c2y=x=y=0, d=1, n=1<<n;
13 var cvs=document.getElementById(cId);
14 var ctx=cvs.getContext("2d");
15 hsh=Number(hsh); vsh=Number(vsh);
16 x=y=cvs.width/2;
17 // Cleaning canvas, init plotting
18 ctx.fillStyle="white"; ctx.fillRect(0,0,cvs.width,cvs.height);
19 ctx.beginPath();
20 for(i=0; i<=n;) {
21 ctx.lineTo((x+hsh)*sc,(y+vsh)*sc);
22 c1=c&1; c2=c&2;
23 c2x=1*d; if(c2>0) {c2x=(-1)*d}; c2y=(-1)*c2x;
24 if(c1>0) {y+=c2y} else {x+=c2x}
25 i++; c+=i/(i&-i);
26 }
27 ctx.strokeStyle = clr; ctx.stroke();
28 }
29 </script>
30 </head>
31 <body>
32 <p><b>Please input order, scale, x-shift, y-shift, color:</></p>
33 <input id=ord value=11 type="number" min="7" max="25" size="2">
34 <input id=sci value=7.0 type="number" min="0.001" max="10" size="5">
35 <input id=hshi value=-265 type="number" min="-50000" max="50000" siz
36 <input id=vshi value=-260 type="number" min="-50000" max="50000" siz
37 <input id=cli value="red" type="text" size="14">
38 <button onclick="pDragon('canvId')">Plot it!</button>
39 <h3>Dragon curve</h3>
40 <canvas id="canvId" width=640 height=640 style="border: 2px inset;">
41 </body>
42 </html>

 

Modify and explore in EdgeView of maXbox5

The Script you find at:

https://sourceforge.net/projects/maxbox/files/Examples/13_General/1320_dragon_curve_51_py_turtle_js.txt/download
drawDragon2(13,1,500);

if (adir= -1) and (cn mod 20=1) then canvas.Pen.Color:= random(clwhite);

Colormaps
Colormaps if (adir= -1) and (cn mod 50=1) then
canvas.Pen.Color:= random(clwhite);

Colormaps 3

drawDragon2(14,1,500); myturtle.penWidth:= 2; (cn mod 70=1)


drawDragon2(15,1,500); myturtle.penWidth:= 2; (cn mod 70=1)

Sound Dragon Varias

canvas.Pen.Color:= random(clred);
Published by DEV

Image to MIDI: Image to MIDI (key13.uk)

Unreal Heal
From the Fourier series definition in Wikipedia.

Compact Code

This line we have to decompose in functional steps:

1 var
2 num, den: Extended;
3 den:= BinToInt('1'+value.Split(['.'])[1].Replace('1','0',[rfReplaceAl
 

So value.Split as an string object we do also have the function:

1 var tsa: TStringArray;


2 tsa:= flcStrSplit(value,'.');
3 sr:= stringreplace(tsa[1], '1','0',[rfReplaceAll])
4 den:= bintoint5('1'+sr);

And the whole function from:


https://sourceforge.net/projects/maxbox5/files/examples/1344_Functional_floattobin.pas/download
1 function BinToInt5(value: string): Int64;
2 var i,alen: integer; bit: Int64;
3 begin
4 Result:= 0;
5 alen:= Length(value);
6 for i:= alen downto 1 do begin
7 bit:= ord(value[i] = '1');
8 Result:= Result or (bit shl (alen - i));
9 end;
10 end;
11
12 function BinToFloat5(value: string): Extended;
13 var num, den: extended; tsa: TStringArray;
14 begin
15 //if value.IndexOf('.') = -1 then
16 if pos('.',value) = 0 then begin
17 result:= bintoint5(value);
18 exit; end;
19 num:= BinToInt5(StringReplace(value,'.', '', []));
20 //den:= BinToInt('1'+value.Split(['.'])[1].Replace('1','0',[rfRepl
21 tsa:= flcStrSplit(value,'.');
22 sr:= stringReplace(tsa[1],'1','0',[rfReplaceAll])
23 den:= bintoInt5('1'+sr);
24 Result:= num / den;
25 end;
 

Death Star

Display a region that consists of a large sphere with part of a smaller sphere removed
from it as a result of geometric subtraction.

https://sourceforge.net/projects/maxbox5/files/examples/1345_death_star_graph12.pas/download

(This will basically produce a shape like a “death star“.)

bmp := DeathStar(apos, aneg, 1.2, 0.3, alight);

With JavaScript in maXbox editor:


softwareschule.ch/examples/deathstar.htm

png as use another jpg or bmp viewer

1 with TJpegImage.Create do begin


2 bmp.TransparentColor:= clwhite;
3 Assign(bmp);
4 //bmp.TransparentColor:= clblack;
5 SaveToFile(exepath+'examples\deathstarout.jpg');
6 sleep(500);
7 openfile(exepath+'examples\deathstarout.jpg');
8 bmp.Free;
9 Free;
10 end;

Get more than one function result as the example of a vector below:

1 type
2 TTVector = array of double;
3
4 procedure TNormalize(var v: TTVector);
5 var alen: double;
6 begin
7 alen:= Sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
8 v[0]:= v[0] / alen;
9 v[1]:= v[1] / alen;
10 v[2]:= v[2] / alen;
11 end;

So we can pass a vector with call by reference:

1 alight:= [-50, 30, 50];


2 TNormalize(alight);
3 writeln(flots(alight[0]));
4 >>> -0.650944554904119

In Python we use a named tuple. Here the three values x,y,z of the namedtuple are
applied, in order, to the keyword arguments in the function. Those 3 arguments can be
addressed as positional arguments still, after all!

1 Execstr('Sphere = collections.namedtuple("Sphere", "cx cy cz r")');


2 Execstr('V3 = collections.namedtuple("V3", "x y z")');
3
4 def normalize(x, y, z,*args):
5 len = math.sqrt(x**2 + y**2 + z**2)
6 return V3(x / len, y / len, z / len)
7
8 execstr('light = normalize(-50, 30, 50, V3)');
9 println('tuples '+evalstr('light.x'))
10 >>> tuples -0.6509445549041194
 

Lunschania – Vals

Geniessen Sie ruhige und entspannte Tage in der Natur. Entdecken Sie in unmittelbarer
Nähe eines der höchstgelegenen Skigebiete Graubündens, traumhafte Wandergebiete,
urtümliche Alpen, herrliche Seen und Flüsse, hohe Berge und abwechslungsreiche
Biketouren.
Vals – Platz Chrüz Riedboda
Valser Rhein St. Peter und Paul Valéstrasse

Lunschania
Lunschania – Vals December 2024

Advertisements

Occasionally, some of your visitors may see an advertisement here,


as well as a Privacy & Cookies banner at the bottom of the page.
You can hide ads completely by upgrading to one of our paid plans.

UPGRADE NOW DISMISS MESSAGE


← Previous Next →

3 responses to “Dragon Curve”

maxbox4
December 27, 2024 at 11:59 am Edit

Music is the pleasure the human soul experiences from counting without being aware that it is counting
(Gottfried Leibniz)

 Like

Reply

maxbox4
December 27, 2024 at 12:03 pm Edit

I like the concept of sonification: translating data into sounds. There is a huge amount of contents in
the Internet about this technique and there are several packages in Delphi, Python, R, SonicPi to
help you to sonificate your data.

 Like
Reply

maxbox4
December 27, 2024 at 12:05 pm Edit

All you need is understanding how Mandelbrot Set is generated by the recursive equation. There are
hundreds of webpages where you can find it. I think there is no more to understand. Thanks for your
comment.

 Like
Reply

Leave a comment

Write a comment...

maxbox4 - Logged in via WordPress.com - Log out

Notify me of new posts


Receive web and mobile notifications for posts on this site.

Email me new posts

Instantly Daily Weekly

Email me new comments

Comment

Designed with WordPress

maXbox4ex Digitally signed by


maXbox4exe

e Date: 2024.12.27
12:13:16 +01'00'

You might also like