Create fractal shapes with python part1 (Sierpinski Gasket)

1. What is a fractal figure?

A fractal figure is the same as the original figure no matter how much it is enlarged. The Sierpinski Gasket, which is a typical fractal figure, is shown below. fractal1.gif

2. Creating a Sierpinski Gasket

fractal.py


import matplotlib.pyplot as plt
import matplotlib.patches as pat
import math

#triangle = [(x1,y1),(x2,y2),(x3,y3)]

#Output the coordinates of the triangle in the input triangle
def return_triangle(triangle):
    x1 = (triangle[0][0] + triangle[1][0])/2
    y1 = (triangle[0][1] + triangle[1][1])/2
    x2 = (triangle[1][0] + triangle[2][0])/2
    y2 = (triangle[1][1] + triangle[2][1])/2
    x3 = (triangle[2][0] + triangle[0][0])/2
    y3 = (triangle[2][1] + triangle[0][1])/2
    new_triangle = [(x1,y1),(x2,y2),(x3,y3)]
    return new_triangle

#Output the distance between two points
def distance(p1,p2):
    return math.sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)

#Outputs a triangle consisting of the two points closest to the point p at the points p and the triangle
def select_neighbor_points(p, triangle):
    distance1 = distance(p, triangle[0])
    distance2 = distance(p, triangle[1])
    distance3 = distance(p, triangle[2])
    if distance1 > distance2:
        if distance1 > distance3:
            return [p, triangle[1], triangle[2]]
        else:
            return [p, triangle[0], triangle[1]]
    else:
        if distance2 > distance3:
            return(p, triangle[0],triangle[2])
        else:
            return(p, triangle[0],triangle[1])
            
#Generate a fractal figure. The larger the number of iterations, the more complicated it becomes
def produce_fractal1(triangle, iteration):
    if iteration == 0: return 0
    p1 = triangle[0]
    p2 = triangle[1]
    p3 = triangle[2]
    new_triangle = return_triangle(triangle)
    p = pat.Polygon(xy = new_triangle,fc = "white", ec = "black")
    ax.add_patch(p)
    produce_fractal1(select_neighbor_points(p1,new_triangle), iteration-1)
    produce_fractal1(select_neighbor_points(p2,new_triangle), iteration-1)
    produce_fractal1(select_neighbor_points(p3,new_triangle), iteration-1)

triangle = [(0.2, 0.2), (0.8, 0.2), (0.5, 0.8)] #Coordinates of the initial triangle
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(1,1,1)
p = pat.Polygon(xy = triangle,fc = "white", ec = "black")
ax.add_patch(p)
produce_fractal1(triangle,6)
fig.savefig("./fractal.png ") #Save image

It can be easily implemented by recursive processing. Execution was done by jupyter.

fractal.py


produce_fractal1(triangle,iteration)

Increasing the number of iterations creates triangles deeper.

3. Summary

By implementing recursive processing well, we were able to create a Sierpinski Gasket. By changing the contents of recursive processing, we can create other fractal figures.

Recommended Posts

Create fractal shapes with python part1 (Sierpinski Gasket)
Create test data like that with Python (Part 1)
Image processing with Python (Part 2)
Studying Python with freeCodeCamp part1
Bordering images with python Part 1
Scraping with Selenium + Python Part 1
Create 3d gif with python3
Studying Python with freeCodeCamp part2
Image processing with Python (Part 1)
Solving Sudoku with Python (Part 2)
Image processing with Python (Part 3)
Scraping with Selenium + Python Part 2
Playing handwritten numbers with python Part 1
Create Awaitable with Python / C API
[Automation with python! ] Part 1: Setting file
Create folders from '01' to '12' with python
Create a virtual environment with Python!
Create an Excel file with Python3
Automate simple tasks with Python Part0
[Automation with python! ] Part 2: File operation
Create github pages with lektor Part 1
Draw Lyapunov Fractal with Python, matplotlib
Excel aggregation with Python pandas Part 1
Create a Python function decorator with Class
Create wordcloud from your tweet with python3
Build a blockchain with Python ① Create a class
Play handwritten numbers with python Part 2 (identify)
FM modulation and demodulation with Python Part 3
Process Pubmed .xml data with python [Part 2]
Create a dummy image with Python + PIL.
Automate simple tasks with Python Part1 Scraping
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Quickly create an excel file with Python #python
Fractal to make and play with Python
100 Language Processing Knock with Python (Chapter 2, Part 2)
Create Python + uWSGI + Nginx environment with Docker
Working with Azure CosmosDB from Python Part.2
Excel aggregation with Python pandas Part 2 Variadic
100 Language Processing Knock with Python (Chapter 2, Part 1)
FM modulation and demodulation with Python Part 2
[Part1] Scraping with Python → Organize to csv!
Create an English word app with python
Create a frame with transparent background with tkinter [Python]
Machine learning starting with Python Personal memorandum Part2
Create an app that guesses students with python
Machine learning starting with Python Personal memorandum Part1
Create a LINE BOT with Minette for Python
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a python3 build environment with Sublime Text3
Create a color bar with Python + Qt (PySide)
Create Nginx + uWSGI + Python (Django) environment with docker
Create an image with characters in python (Japanese)
Steps to create a Twitter bot with python
How to measure execution time with Python Part 1
Create a decision tree from 0 with Python (1. Overview)
Create a new page in confluence with Python
Create a Photoshop format file (.psd) with python