From 770d9ce7e58329d17d4c4a7069f5097893de329f Mon Sep 17 00:00:00 2001 From: Simon Waldherr Date: Thu, 24 Oct 2024 20:45:58 +0200 Subject: [PATCH 1/3] add Barnsley Fern --- fractals/barnsley_farn.py | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 fractals/barnsley_farn.py diff --git a/fractals/barnsley_farn.py b/fractals/barnsley_farn.py new file mode 100644 index 000000000000..39db8c5e9bc8 --- /dev/null +++ b/fractals/barnsley_farn.py @@ -0,0 +1,53 @@ +""" +Barnsley Fern + +The Barnsley fern is a fractal that uses an iterated function system (IFS) +to generate a realistic-looking fern shape. + +Reference: +https://en.wikipedia.org/wiki/Barnsley_fern + +Requirements: + - matplotlib + - numpy +""" + +import numpy as np +import matplotlib.pyplot as plt + + +def barnsley_farn(n_points=100000): + """ + Generates the points of the Barnsley fern. + """ + x, y = 0, 0 + points = np.zeros((n_points, 2)) + + for i in range(n_points): + r = np.random.random() + if r < 0.01: + x, y = 0, 0.16 * y + elif r < 0.86: + x, y = 0.85 * x + 0.04 * y, -0.04 * x + 0.85 * y + 1.6 + elif r < 0.93: + x, y = 0.2 * x - 0.26 * y, 0.23 * x + 0.22 * y + 1.6 + else: + x, y = -0.15 * x + 0.28 * y, 0.26 * x + 0.24 * y + 0.44 + points[i] = [x, y] + + return points + + +def plot_barnsley_farn(points): + """ + Plots the Barnsley fern using matplotlib. + """ + x, y = points[:, 0], points[:, 1] + plt.scatter(x, y, s=0.1, color='green') + plt.title("Barnsley Fern") + plt.show() + + +if __name__ == "__main__": + fern_points = barnsley_farn() + plot_barnsley_farn(fern_points) From 15bf85ecbf95b469a305edc9b27a4f76a514cecc Mon Sep 17 00:00:00 2001 From: Simon Waldherr Date: Thu, 24 Oct 2024 21:43:50 +0200 Subject: [PATCH 2/3] apply coding style --- fractals/barnsley_farn.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fractals/barnsley_farn.py b/fractals/barnsley_farn.py index 39db8c5e9bc8..d2666157a2a6 100644 --- a/fractals/barnsley_farn.py +++ b/fractals/barnsley_farn.py @@ -1,7 +1,7 @@ """ Barnsley Fern -The Barnsley fern is a fractal that uses an iterated function system (IFS) +The Barnsley fern is a fractal that uses an iterated function system (IFS) to generate a realistic-looking fern shape. Reference: @@ -12,8 +12,8 @@ - numpy """ -import numpy as np import matplotlib.pyplot as plt +import numpy as np def barnsley_farn(n_points=100000): @@ -23,8 +23,10 @@ def barnsley_farn(n_points=100000): x, y = 0, 0 points = np.zeros((n_points, 2)) + rng = np.random.default_rng() + for i in range(n_points): - r = np.random.random() + r = rng.random() if r < 0.01: x, y = 0, 0.16 * y elif r < 0.86: @@ -43,7 +45,7 @@ def plot_barnsley_farn(points): Plots the Barnsley fern using matplotlib. """ x, y = points[:, 0], points[:, 1] - plt.scatter(x, y, s=0.1, color='green') + plt.scatter(x, y, s=0.1, color="green") plt.title("Barnsley Fern") plt.show() From b079fea0b92d85b88800345765eb1d33b72520dd Mon Sep 17 00:00:00 2001 From: Simon Waldherr Date: Thu, 24 Oct 2024 22:32:14 +0200 Subject: [PATCH 3/3] improve coding style --- fractals/barnsley_farn.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/fractals/barnsley_farn.py b/fractals/barnsley_farn.py index d2666157a2a6..3ad01f959784 100644 --- a/fractals/barnsley_farn.py +++ b/fractals/barnsley_farn.py @@ -16,40 +16,57 @@ import numpy as np -def barnsley_farn(n_points=100000): +def barnsley_fern(n_points: int = 100000) -> np.ndarray: """ Generates the points of the Barnsley fern. + + Args: + n_points (int): Number of points to generate. + + Returns: + np.ndarray: An array of shape (n_points, 2) containing the x and y coordinates. """ - x, y = 0, 0 - points = np.zeros((n_points, 2)) + x, y = 0.0, 0.0 + points = np.zeros((n_points, 2), dtype=np.float64) rng = np.random.default_rng() for i in range(n_points): r = rng.random() if r < 0.01: - x, y = 0, 0.16 * y + x, y = 0.0, 0.16 * y elif r < 0.86: - x, y = 0.85 * x + 0.04 * y, -0.04 * x + 0.85 * y + 1.6 + x_new = 0.85 * x + 0.04 * y + y = -0.04 * x + 0.85 * y + 1.6 + x = x_new elif r < 0.93: - x, y = 0.2 * x - 0.26 * y, 0.23 * x + 0.22 * y + 1.6 + x_new = 0.20 * x - 0.26 * y + y = 0.23 * x + 0.22 * y + 1.6 + x = x_new else: - x, y = -0.15 * x + 0.28 * y, 0.26 * x + 0.24 * y + 0.44 + x_new = -0.15 * x + 0.28 * y + y = 0.26 * x + 0.24 * y + 0.44 + x = x_new points[i] = [x, y] return points -def plot_barnsley_farn(points): +def plot_barnsley_fern(points: np.ndarray) -> None: """ Plots the Barnsley fern using matplotlib. + + Args: + points (np.ndarray): An array of shape (n_points, 2) containing coordinates. """ x, y = points[:, 0], points[:, 1] - plt.scatter(x, y, s=0.1, color="green") + plt.figure(figsize=(6, 10)) + plt.scatter(x, y, s=0.1, color="green", marker="o") plt.title("Barnsley Fern") + plt.axis("off") # Hide the axes for better visualization plt.show() if __name__ == "__main__": - fern_points = barnsley_farn() - plot_barnsley_farn(fern_points) + fern_points = barnsley_fern() + plot_barnsley_fern(fern_points)