Steffen Müller > Math-Fractal-Curve > Math::Fractal::Curve



Annotate this POD

View/Report Bugs
Module Version: 1.03   Source  


Math::Fractal::Curve - Generate fractal curves


  use Math::Fractal::Curve;

  # This generates a von Koch-curve.
  my $generator = [
      [0,   0,         1/3, 0        ],
      [1/3, 0,         1/2, sqrt(5)/6],
      [1/2, sqrt(5)/6, 2/3, 0        ],
      [2/3, 0,         1,   0        ],
  # $generator may also be an anonymous subroutine that returns a
  # data structure like the above.
  # New curve generator
  my $curve_gen = Math::Fractal::Curve->new(generator => $generator);
  # New curve
  my $curve = $curve_gen->line(
      start => [-2, 1],
      end   => [2, -1],
  my $edges = $curve->fractal($depth);
  # (now containing array ref of array refs of x1,y1,x2,y2 coordinates)


This module is intended to generate 2-dimensional fractal curves such as the von Koch curve from simple generator functions.

The fractals are generated by recursively replacing a distance with the generator. Hence, the starting distance and the generator define such a fractal curve. Generators describe what a given distance is going to be replaced with in terms of lengths of the distance. For example, a generator of ([0, 0, 1/3, 0], [2/3, 0, 1, 0]) describes a Mid-third Cantor Set which means the the middle third of every distance in the set is deleted. Syntax for generator data structures in the context of this module is [[x1, y1, x2, y2], [X1, Y1, X2, Y2]] (array ref of array refs of edge coordinates) where xn,yn are the two coordinate pairs specifying the first edge a distance is to be replaced with and Xn,Yn are the second edge. There may be any number of edges.

For more telling examples, please have a thorough look at the examples subdirectory that came with this distribution or look through the examples page of this module on

Furthermore, the generator may be either one of the aformentioned nested array references, or it may be an anonymous subroutine that returns such a data structure. This enables you to generate probalistic fractal curves or fractal curves whose trajectories depend on the distance any generator is to replace, etc.

While the above feature makes the probablistic / dynamic curves non-fractal, they preserve some properties real fractals have. Please refer to the literature mentioned under "SEE ALSO" for more information. The examples subdirectory of the distribution also holds an example of a probalistic von Koch-curve and a Koch curve whose excavation-direction (the direction the triangle points at) depends on the orientation of the distance the generator is applied to (

Generator subroutines are passed the curve object as first argument. They may access any attributes of the curve segment they are applied to, but most interestingly, they may access their {start} and {end} attributes that hold array references [x,y] of the start- and end points of the distance they are being applied to.




Constructor new

The new() constructor requires one named argument:

  generator => GENERATOR

where GENERATOR may either be a generator-datastructure as described earlier or a subroutine reference (or closure) that returns such a data structure.

Furthermore, new accepts any key/value pairs that will be made attributes of the curve object.

new() is both a class- and an object method and thus can be used to clone existing curves. (And is internally used to do so.)

Method line

The line() method takes two required named arguments:

  start => [START_X, START_Y],
  end   => [END_X,   END_Y  ]

where START_X, START_Y and END_X, END_Y are the coordinates of the start- and end points of the distance to create the fractal curve from.

line() stores this data in the {start} and {end} attributes of the curve object.

Method recurse()

The recurse() method applies the generator to the curve's distance and returns a reference to an array of new curve objects that represent the newly generated edges.

Method fractal()

The fractal() method takes one argument: The recursion depth of the discrete fractal representation. Obviously, the complexity is Edges^Depth with Edges equal to the number of edges of the generator.

fractal() returns a reference to an array of array references. These referenced arrays contain (x1, y1, x2, y2) coordinates of edges.

Method edges()

The edges() method returns a reference to an array of array references. These referenced arrays contain (x1, y1, x2, y2) coordinates of the edges that are generated by the generator from the curve's starting edge.


Steffen Mueller, <>


Copyright (C) 2003-2006 Steffen Mueller, <>



New versions of this module can be found on or CPAN.

The idea and some background for this module came from:

Kenneth J. Falconer, "Fractal Geometry. Mathematical Foundations and Applications", (c) 1990 at Jon Wiley & Sons Ltd., Chichester

syntax highlighting: