in reply to Order Out Of Chaos

Oooh! Pretty! That's a fractal right? What's it called?

use strict; use Tk; my $phi = 0; my $thisX = 185; my $thisY = 500; my $nextX = 185; my $nextY = 500; my $mw = MainWindow->new; $mw->geometry( "700x700" ); my $c1 = $mw->Canvas( -background => 'black', -height => 700, -width => 700) ->pack; use constant PRINT => 'P'; use constant LEFT => '<'; use constant RIGHT => '>'; my $ax = "P>" x 4 . "P"; $ax =~ s/P/P<P>P>P<P/g for 1 .. 4; for (split //, $ax) { if ($_ eq RIGHT) { # rotate right $phi += 90; $phi -= 360 if $phi >= 360; } elsif ($_ eq LEFT) { # rotate left $phi -= 90; $phi += 360 if $phi < 0; } elsif ($_ eq PRINT) { # print $thisX = $nextX; $thisY = $nextY; if ($phi == 0) { $nextX += 4 } elsif ($phi == 90) { $nextY -= 4 } elsif ($phi == 180) { $nextX -= 4 } elsif ($phi == 270) { $nextY += 4 } $c1->createLine( $thisX, $thisY, $nextX, $nextY, -fill => 'white' ); $mw->update; } } $c1->createText( 350, 350, -text => "P E R L \n M O N K S", -fill => 'white', -width => 300); MainLoop;
__SIG__ printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;

Replies are listed 'Best First'.
Re: Re: Order Out Of Chaos
by Lysander (Monk) on Oct 16, 2002 at 15:39 UTC
    You're right, diotalevi, it is a fractal. Good eyes! Specifically, it's a Lindenmayer system (L-system) type of fractal. A definition of this type of fractal is:

    "A L-system or Lindenmayer system is a formal grammar for generating strings. (That is, it is a collection of rules such as replace X with XYX.) By recursively applying the rules of the L-system to an initial string, a string with fractal structure can be created. Interpreting this string as a set of graphical commands allows the fractal to be displayed. L-systems are very useful for generating realistic plant structures."

    The above definition comes from this FAQ.

    I just recently started reading about fractals, and since this type is string-based, I thought it would be fun to try it out with Perl. The starting shape that I used looked something like this (please excuse the ASCII art):

    ***  ***
      *  *
      ****
    
    Hope this helps, and thanks for the comments.
      So then snowflakes, themselves, are fractals? I always thought that fractals were designs that really had no symmetrical pattern like that, but more random patterns. Cool, I learned something. I like that. Word!
      _____________________________________________________
      mojobozo
      word (wûrd)
      interj. Slang. Used to express approval or an affirmative response to
      something. Sometimes used with up. Source
Re: Re: Order Out Of Chaos
by mojobozo (Monk) on Oct 16, 2002 at 14:55 UTC
    Very nice indeed! But, to me, it looks more like a snowflake or tile than a fractal.
    _____________________________________________________
    mojobozo
    word (wûrd)
    interj. Slang. Used to express approval or an affirmative response to
    something. Sometimes used with up. Source

      Try playing with that for 1 .. 4 loop. I really don't know fractals so to my uneducated eye it looks like more detail after more iterations. I pushed it up to 5 but it didn't fit ony my screen anymore.

      __SIG__ printf "You are here %08x\n", unpack "L!", unpack "P4", pack "L!", B::svref_2object(sub{})->OUTSIDE;