penkit.fractal package

Submodules

penkit.fractal.l_systems module

The l_systems module contains an implementation of Lindemeyer systems.

penkit.fractal.l_systems.l_system(axiom, transformations, iterations=1, angle=45, resolution=1)[source]

Generates a texture by running transformations on a turtle program.

First, the given transformations are applied to the axiom. This is repeated iterations times. Then, the output is run as a turtle program to get a texture, which is returned.

For more background see: https://en.wikipedia.org/wiki/L-system

Parameters:
  • axiom (str) – the axiom of the Lindenmeyer system (a string)
  • transformations (dict) – a dictionary mapping each char to the string that is substituted for it when the rule is applied
  • iterations (int) – the number of times to apply the transformations
  • angle (float) – the angle to use for turns when interpreting the string as a turtle graphics program
  • resolution (int) – the number of midpoints to create in each turtle step
Returns:

A texture

penkit.fractal.l_systems.transform_multiple(sequence, transformations, iterations)[source]

Chains a transformation a given number of times.

Parameters:
  • sequence (str) – a string or generator onto which transformations are applied
  • transformations (dict) – a dictionary mapping each char to the string that is substituted for it when the rule is applied
  • iterations (int) – how many times to repeat the transformation
Yields:

str – the next character in the output sequence.

penkit.fractal.l_systems.transform_sequence(sequence, transformations)[source]

Applies a given set of substitution rules to the given string or generator.

For more background see: https://en.wikipedia.org/wiki/L-system

Parameters:
  • sequence (str) – a string or generator onto which transformations are applied
  • transformations (dict) – a dictionary mapping each char to the string that is substituted for it when the rule is applied
Yields:

str – the next character in the output sequence.

Examples

>>> ''.join(transform_sequence('ABC', {}))
'ABC'
>>> ''.join(transform_sequence('ABC', {'A': 'AC', 'C': 'D'}))
'ACBD'

Module contents

penkit.fractal.flowsnake(iterations=4, resolution=1)[source]

Generates a Peano-Gosper curve using an L-System.

For more information see: https://en.wikipedia.org/wiki/Gosper_curve

Parameters:
  • iterations (int) – the number of times to iterate the transformation
  • resolution (int) – the number of midpoints along each line
Returns:

A texture

penkit.fractal.hilbert_curve(iterations=5, resolution=1)[source]

Generates a Hilbert space-filling curve using an L-System.

For more information see: https://en.wikipedia.org/wiki/Hilbert_curve

Parameters:
  • iterations (int) – the number of times to iterate the transformation
  • resolution (int) – the number of midpoints along each line
Returns:

A texture

penkit.fractal.tree(iterations=4, resolution=1, angle=22.5)[source]

Generates an organic-looking tree.

Parameters:
  • iterations (int) – the number of times to iterate the transformation
  • resolution (int) – the number of midpoints along each line
  • angle (float) – the angle of branching
Returns:

A texture