The ifsFractals Python Module has similar functionality as this site. It can be useful to create larger versions of fractals, or perhaps automate the generation of several fractals at once. The ifsFractal module can also export to this website's playground.
pip install ifsFractals
# import ifsFractals module import ifsFractals as ifs # define transformations T_1 = ifs.Scale(1/2) T_2 = ifs.Translate(1/2, 0) @ ifs.Scale(1/2) T_3 = ifs.Translate(1/4, 1/2) @ ifs.Scale(1/2) T = [T_1, T_2, T_3] # create fractal object fractal = ifs.Fractal(T) # add points and plot fractal.add_points(100_000) fractal.display()
The module offers several built-in transformations, which are represented as 3x3 matrices. These transformations can be combined to create a sequence of transformations for fractal generation. The available transformations are:
Scale(s: float)
: Uniform scaling by a factor s
in both x and y directions.Translate(a: float, b: float)
: Translation by a
units along the x-axis and b
units along the y-axis.Rotate(theta: float)
: Rotation around the origin by an angle theta
(in radians).ShearX(t: float)
: Shearing of points along the x-axis by a factor t
.ShearY(t: float)
: Shearing of points along the y-axis by a factor t
.ScaleX(s: float)
: Scaling along the x-axis by a factor s
.ScaleY(s: float)
: Scaling along the y-axis by a factor s
.ScaleXY(s: float, t: float)
: Scaling by different factors s
and t
in the x and y directions, respectively.The module provides several built-in figures, represented as arrays of points. These figures can be used in fractal generation. The available figures are:
Box
: A square figure represented as an array of points.rect(n: int)
: Function to generate a rectangular figure by scaling the Box
figure along the y-axis by a factor 1/n
.Line
: A line segment represented as an array of points.XBox
: A Box with an X shape, represented as an array of points.transform(figures: List[np.ndarray], transformations: List[np.ndarray]) -> List[np.ndarray]
Transforms a list of figures using a list of transformations.
figures
: (List[np.ndarray]) List of numpy arrays representing the initial figures.transformations
: (List[np.ndarray]) List of numpy arrays representing the transformations to apply to the figures.Returns: (List[np.ndarray]) List of numpy arrays representing the transformed figures.
generate_figures(n: int, figures: List[np.ndarray], transformations: List[np.ndarray]) -> List[np.ndarray]
Generates a list of figures by iteratively applying transformations to the initial figures.
n
: (int) Number of iterations to apply transformations.figures
: (List[np.ndarray]) List of numpy arrays representing the initial figures.transformations
: (List[np.ndarray]) List of numpy arrays representing the transformations to apply to the figures.Returns: (List[np.ndarray]) List of numpy arrays representing the generated figures.
plot_figures(figures: List[np.ndarray], size: int=4, width: float=1.5, color: str='blue')
Plots the given list of figures using matplotlib.
figures
: (List[np.ndarray]) List of numpy arrays representing the figures to plot.size
: (Optional int)Size of the plot (default: 4).width
: (Optional float)Width of the lines in the plot (default: 1.5).color
: (Optional str) Color of the lines in the plot (default: 'blue').Note: This function plots the figures using matplotlib and does not return anything.
The module provides functions to generate sets of points and calculate bounds for fractals.
generate_points(n: int, transformations: List[np.ndarray], weights: Optional[List[float]], head_start: int)
Function to generate a set of points by applying random transformations to an initial point.
n
: (int) Number of points to generate.transformations
: (List[np.ndarray]) List of transformations represented as matrices.weights
: (Optional[List[float]]) List of weights for each transformation.head_start
: (int) Number of initial iterations to skip before generating points.plot_points(points: List[np.ndarray], size: Optional int)
Function to plot a set of points.
points
: (List[np.ndarray]) List of points to plot.size
: (Optinal int) size of plot.find_bounds(transformations: List[np.ndarray], weights: Optional[List[float]])
Function to find the bounds (min and max values) of the generated points based on transformations and weights.
transformations
: (List[np.ndarray]) List of transformations represented as matrices.weights
: (Optional[List[float]]) List of weights for each transformation.The Fractal
class represents an IFS fractal and provides methods for creating, plotting, and saving the fractal.
__init__(self, transformations: List[np.ndarray], weights: Optional[List[float]], size: int, color: Tuple[int, int, int])
Class constructor to initialize the Fractal object.
transformations
: (List[np.ndarray]) List of transformations represented as matrices.weights
: (Optional[List[float]]) List of weights for each transformation.size
: (int) Size of the fractal.color
: (Tuple[int, int, int]) RGB color tuple for the fractal.set_weights(self, weights: Optional[List[float]])
Method to set the weights for each transformation.
weights
: (Optional[List[float]]) List of weights for each transformation.plot_figures(self, depth: int, initial: np.ndarray, size: int, width: float, color: str)
Method to plot the figures generated by applying transformations to an initial figure.
depth
: (int) Number of iterations to apply transformations.initial
: (np.ndarray) Initial figure as an array of points.size
: (int) Size of the plot.width
: (float) Width of the lines in the plot.color
: (str) Color of the lines in the plot.check_transformations(self, verbose: bool) -> bool
Method to check if the transformations are valid contraction mappings.
verbose
: (bool) If True, prints the result of the check.Returns: (bool) True if all transformations are valid contraction mappings, False otherwise.
iterate(self)
Method to perform a single iteration of random transformation on the fractal point.
add_points(self, n: int)
Method to add n
points to the fractal by iterating random transformations on the fractal point.
n
: (int) Number of points to add.load_in_points(self, externalTup: Tuple[np.ndarray, np.ndarray])
Method to load a set of points into the fractal object for visualization.
externalTup
: (Tuple[np.ndarray, np.ndarray]) Tuple of two arrays representing x and y coordinates of external points to be loaded.save(self, path: str)
Method to save the plotted fractal as an image file.
path
: (str) Path to save the image file.display(self)
Method to display the plotted fractal as an image.
export(self)
Method to export the fractal as a JSON object, including the transformations and weights used.
link(self)
Method to generate a link to the IFS Fractals web application with the current fractal configuration.
link_web(self)
Method to display a clickable link to the IFS Fractals web application with the current fractal configuration.
embed_web(self)
Method to display the IFS Fractals web application with the current fractal configuration embedded as an IFrame.