• Skip to primary navigation
  • Skip to main content

Algorithm.co.il

  • About
  • Best Posts
  • Origami
  • Older Projects

Fractals in 10 minutes no. 3 – The Dragon

Posted on 2008-01-16 by lorg 3 Comments

When first I looked through the pages of the book “Hacker’s Delight”, I found myself looking at the chapter about bases. There I learned a very curious fact – with the digits of 0,1 and the base of -2, you can represent any integer. Right afterwards I learned something even more interesting – with the digits of 0,1 and the base of 1-i, you can represent and number of the form a+bi where a and b are integers. Having nothing to do with this curious fact, I let the subject go.
Some time later, I was reading through Knuth’s “Art of Computer Programming”, and found that with the base of (1-i)^-1, and digits of 0,1 you can generate the dragon fractal!

The dragon fractal

Generating the fractal is quite simple actually:

def create_dragon_set(n):
    """calculate the dragon set, according to Knuth"""
    s = set([0.0+0.0j])
    for i in range(n):
        new_power = (1.0-1.0j)**(-i)
        s |= set(x+new_power for x in s)
    return s

def create_dragon_set(n): """calculate the dragon set, according to Knuth""" s = set([0.0+0.0j]) for i in range(n): new_power = (1.0-1.0j)**(-i) s |= set(x+new_power for x in s) return s

(By the way, can you do it better?)

The annoying part is converting the complex numbers to drawable integer points. After doing so, I used PIL to draw the jpeg.
Here’s a link to the code.

Filed under: Favourites, Fractals, Math, Programming, Python

Reader Interactions

Comments

  1. Keymone says

    2008-01-24 at 6:28 am

    actually there are Dragon curves – very interesting fractals
    simplest way to get them is folding a paper string by half few times
    curious fact is that 4 Dragon curves can completely fill 2d space without any interference
    i was playing with .NET some time ago and have a sample program which generates Dragon curve for few steps ahead with specific folding direction – but it’s horribly slow because i was using strings to represent fractal :)

    Reply
  2. lorg says

    2008-01-24 at 7:50 am

    Well, because of your comment I also looked at the dragon curve page on wikipedia. It seems that:

    1. Dragon curves may be produced by lsystems (lstrings). On a side note, a version of the book ‘Jurassic Park’ had another level of the dragon curve on the openning page of each chapter. It was drawn by using lsystems. I remember that as a kid reading this book, I really liked it (and tried to write a program to draw it).

    2. As you said, about folding paper – well, that really makes me very happy to know. I never made this connection between fractals and folding. Excellent!

    3. It seems what I actually drew in this post is the twin-dragon, which is also named the Davis-Knuth dragon. It is constructed from two dragon curves.

    Thanks!

    Reply

Leave a Reply Cancel reply

© 2022 Algorithm.co.il - Algorithms, for the heck of it