MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Simple fractal

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121812] Re: Simple fractal
  • From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
  • Date: Mon, 3 Oct 2011 04:23:21 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201110020636.CAA28033@smc.vnet.net>

Hi,

not the fastest/cleverest/best solution but hopefully an understandable
one:

First a function which takes a starting point and a scale and creates an
arbitrary *Triangle* object which contains the vertex points and the
scaling factor:

MakeTriangle[pos0_, scale_] := 
  Triangle[Table[
    pos0 + scale {Cos[i*120*Degree], Sin[i*120*Degree]}, {i, 1, 3}], 
   scale];

Second a function which executes exactly one recursion step. It takes a
triangle and gives the three new triangles which are one recursion depth
deeper:

TriangleRecursionStep[Triangle[points_, scale_]] := 
 Function[MakeTriangle[#, scale]] /@ points

Third you make a recursion which starts with an initial triangle and a
wanted recursion depth. Note how the nested resulting list of triangles
is build implicitely after all recursions are done.

TriangleRecursion[triangle_, 0] := triangle;
TriangleRecursion[tri_Triangle, depth_Integer /; depth > 0] := {tri, 
  TriangleRecursion[#, depth - 1] & /@ TriangleRecursionStep[tri]}

To display the result you can easily replace a Triangle[...] object by
some graphical representation of your personal liking:

Graphics[{TriangleRecursion[MakeTriangle[{0, 0}, 0.5], 5] /. 
   Triangle[
     points_, _] :> {With[{col = 
        ColorData["DarkBands", RandomReal[]]}, {EdgeForm[{Opacity[1], 
         Thin, Dashed, col}], FaceForm[{Opacity[0.1], col}], 
       Polygon[points]}]}}]

Cheers
Patrick

Easy to copy full code:

MakeTriangle[pos0_, scale_] := 
     Triangle[
   Table[pos0 + scale*{Cos[i*120*Degree], Sin[i*120*Degree]}, 
         {i, 1, 3}], scale];
TriangleRecursionStep[Triangle[points_, scale_]] := 
   (MakeTriangle[#1, scale] & ) /@ points;
TriangleRecursion[triangle_, 0] := triangle; 
TriangleRecursion[tri_Triangle, depth_Integer /; depth > 0] := 
     {tri, (TriangleRecursion[#1, depth - 1] & ) /@ 
    TriangleRecursionStep[
           tri]};
Graphics[{TriangleRecursion[MakeTriangle[{0, 0}, 0.5], 5] /. 
       Triangle[points_, _] :> 
         {With[{col = ColorData["DarkBands", RandomReal[]]}, 
             {EdgeForm[{Opacity[1], Thin, Dashed, col}], 
               FaceForm[{Opacity[0.1], col}], Polygon[points]}]}}]




On Sun, 2011-10-02 at 02:36 -0400, Tom De Vries wrote:
> I am trying to create a very simple fractal, but I am not having any success.
> 
> I'd like to learn how to do this recursively, and hoping someone can
> give me a bit of a push in the right direction.
> 
> It's a simple triangle with triangles at each corner, with triangles
> at each corner, etc.
> 
> Below,  a simple "level 1" graphic.
> 
> scale = 0.5;
> 
> list1 = Table[{0, 0} + {Cos[i*120*Degree], Sin[i*120*Degree]}, {i, 1,
> sides + 1}];
> 
> list2 = (Table[#1 + scale*{Cos[i*120*Degree], Sin[i*120*Degree]}, {i,
> 1, sides + 1}] & ) /@  list1;
> 
> Graphics[{Line[list1], Line[list2]}]
> 
> 
> Ideally I'd like to have a Manipulate with a slider for the level of
> the fractal,  the scale applied to each level, and the distance each
> corner triangle is from the centre triangle.
> 
> Sorry,  I know this is simple programming,  but I don't have any ideas
> how to start.
> 
> Tom De Vries
> 





  • References:
  • Prev by Date: Re: DynamicModule Pure Function
  • Next by Date: Re: A collection of Mathematica learning resources
  • Previous by thread: Re: Simple fractal
  • Next by thread: Re: Simple fractal