DEV Community

Cover image for Dragon Curve
Max Kleiner
Max Kleiner

Posted on

Dragon Curve

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:

procedure Dragon(n, a, t: Integer; d, x, y: Double; var b: TBitmap);
  var a1, a2: integer;
  begin
  if n <= 1 then begin
     with b.Canvas do begin
       Pen.Color := random(p);
       MoveTo(Trunc(x + 0.5), Trunc(y + 0.5));
       LineTo(Trunc(x + d * _cos[a] + 0.5), Trunc(y + d * _sin[a] + 0.5));
       exit;
     end;
   end;
   d := d * s;
   a1 := (a - t) and 7;
   a2 := (a + t) and 7;
  dragon(n - 1, a1, 1, d, x, y, b);
  dragon(n - 1, a2, -1, d, x + d * _cos[a1], y + d * _sin[a1], b);
end;

Enter fullscreen mode Exit fullscreen mode

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:

Const DRAGFUNC =
  'def dragon(level=4, size=200, direction=45):   '+LF+
  '  if level:                                     '+LF+
  '      right(direction)                          '+LF+
  '      dragon(level-1, size/1.41421356237, 45)   '+LF+
  '      left(direction * 2)                       '+LF+
  '      dragon(level-1, size/1.41421356237, -45)  '+LF+
  '      right(direction)                          '+LF+
  '  else:                                         '+LF+
  '      forward(size)                             ';

function PyCodeDragonTurtle(imgpath, aAPIKey: string): string;
begin
  with TPythonEngine.Create(Nil) do begin
  //pythonhome:= 'C:\Users\User\AppData\Local\Programs\Python\Python312\';
  try
    loadDLL;
    autofinalize:= false;
    ExecString('from turtle import right,left,forward,speed, exitonclick, hideturtle');
    ExecStr(DRAGFUNC); 
    ExecStr('speed(0)');  
    //ExecStr('hideturtle()'); 
    ExecStr('dragon(6)');
    ExecStr('exitonclick()');
    //result:= (EvalStr('r.json()')); *)
  except
    raiseError;        
  finally       
    Free;
  end; 
 end;
end; 

Enter fullscreen mode Exit fullscreen mode

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

Image description

The script you get at:
Multilanguage Script

with Depth = 9

Image description

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more