Skip to content

Barnsley Fern Generator in Python #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
Binary file added B/.DS_Store
Binary file not shown.
Binary file added B/Barnsley Fern/.DS_Store
Binary file not shown.
Empty file.
Binary file added B/Barnsley Fern/Fern.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 43 additions & 0 deletions B/Barnsley Fern/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Barnsley Fern using Turtle Graphics

This Python script generates the Barnsley Fern fractal using the Turtle Graphics library. The Barnsley Fern is a famous fractal pattern created through mathematical transformations.

![Barnsley Fern](Fern.png)

## How to Run

1. Ensure you have Python and the Turtle Graphics library installed in your environment.
2. Clone this repository or download the Python script to your computer.
3. Run the script using your preferred Python environment.

## About the Fern

The Barnsley Fern is created by applying four transformations with specific probabilities:

1. Transformation 1: Probability 1%
2. Transformation 2: Probability 85%
3. Transformation 3: Probability 7%
4. Transformation 4: Probability 7%

Each transformation corresponds to a specific set of equations that determine the position of the points.

## Customize the Fern

You can customize various aspects of the generated fern:

- Screen size and background color
- Dot color and size
- Number of iterations

## Example

```python
# Customize the screen size and other parameters
screen.setup(width=800, height=800)
screen.bgcolor("black")

# Customize the dot color and size
fern.dot(2, "green")

# Set the number of iterations
iterations = 100000
Binary file added S/.DS_Store
Binary file not shown.
Binary file added S/sierpinski_triangle/.DS_Store
Binary file not shown.
33 changes: 33 additions & 0 deletions S/sierpinski_triangle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Sierpiński Triangle Generator using Turtle Graphics

This Python script generates the Sierpiński Triangle fractal using the Turtle Graphics library. The Sierpiński Triangle is a classic example of a fractal pattern known for its self-replicating, intricate structure.

![Sierpiński Triangle](sierpinski_triangle.png)

## How to Run

1. Ensure you have Python and the Turtle Graphics library installed in your environment.
2. Clone this repository or download the Python script to your computer.
3. Run the script using your preferred Python environment.

## About the Sierpiński Triangle

The Sierpiński Triangle is constructed by recursively removing smaller equilateral triangles from a larger equilateral triangle. This process is repeated infinitely, resulting in a self-replicating pattern.

## Customize the Triangle

You can customize various aspects of the generated Sierpiński Triangle:

- Depth of recursion to control the complexity of the pattern.
- Initial size of the triangle.
- Choose whether to display a rainbow-colored triangle or a white one.

## Example

```python
# Customize the depth of recursion and other parameters
depth = 3
initial_size = 400
rainbow = False # Set to True for a rainbow-colored triangle

# Run the script to generate the Sierpiński Triangle
Binary file added S/sierpinski_triangle/sierpinski_triangle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
53 changes: 53 additions & 0 deletions S/sierpinski_triangle/sierpinski_triangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import turtle
import random

p = [[0, 280], [-242, -140], [242, -140]]
colors = [
((255, 0, 0), (0, 69)),
((255, 127, 0), (69, 69 * 2)),
((255, 255, 0), (69 * 2, 69 * 3)),
((0, 255, 0), (69 * 3, 69 * 4)),
((0, 0, 255), (69 * 4, 69 * 5)),
((75, 0, 130), (69 * 5, 69 * 6)),
((148, 0, 211), (69 * 6, 69 * 7))
]

def changeColor(x):
colorx = (x + 242)
for i, (color, (start, end)) in enumerate(colors):
if start <= colorx < end:
t.pencolor(color)
break

# Set True for rainbow-colored triangle
rainbow = False

# Initialize turtle
t = turtle.Turtle()
t.hideturtle()
t.speed(0)
t.penup()
t.pencolor('white')

# Initialize screen
s = turtle.getscreen()
s.setup(width=600, height=600)
s.bgcolor('black')

# Draw first 3 points
for i in range(3):
t.goto(p[i][0], p[i][1])
t.dot()

# Draw 10000 points
for i in range(10000):
r = random.randint(0, 2)
pos = t.pos()
# Midway points
x, y = (pos[0] + p[r][0]) / 2, (pos[1] + p[r][1]) / 2
t.goto(x, y)
if rainbow:
changeColor(x)
t.dot()

turtle.exitonclick()
30 changes: 30 additions & 0 deletions animation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

# Create a figure and axis
fig, ax = plt.subplots()

# Initialize an empty plot
line, = ax.plot([], [], lw=2)

# Set the axis limits
ax.set_xlim(0, 2 * 3.14)
ax.set_ylim(-1, 1)

# Function to initialize the plot
def init():
line.set_data([], [])
return line,

# Function to update the plot in each frame
def update(frame):
x = 2 * 3.14 * frame / 100
y = 0.5 * (1 + frame / 100)
line.set_data(x, y)
return line,

# Create an animation
ani = FuncAnimation(fig, update, frames=100, init_func=init, blit=True)

# Display the animation (this may vary depending on your Python environment)
plt.show()