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

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay