mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +08:00
New fractals folder (#4277)
* reupload * delete file * Move koch_snowflake.py to fractals-folder * Move mandelbrot.py to fractals-folder * Move sierpinski_triangle.py to fractals-folder
This commit is contained in:
116
fractals/koch_snowflake.py
Normal file
116
fractals/koch_snowflake.py
Normal file
@ -0,0 +1,116 @@
|
||||
"""
|
||||
Description
|
||||
The Koch snowflake is a fractal curve and one of the earliest fractals to
|
||||
have been described. The Koch snowflake can be built up iteratively, in a
|
||||
sequence of stages. The first stage is an equilateral triangle, and each
|
||||
successive stage is formed by adding outward bends to each side of the
|
||||
previous stage, making smaller equilateral triangles.
|
||||
This can be achieved through the following steps for each line:
|
||||
1. divide the line segment into three segments of equal length.
|
||||
2. draw an equilateral triangle that has the middle segment from step 1
|
||||
as its base and points outward.
|
||||
3. remove the line segment that is the base of the triangle from step 2.
|
||||
(description adapted from https://en.wikipedia.org/wiki/Koch_snowflake )
|
||||
(for a more detailed explanation and an implementation in the
|
||||
Processing language, see https://natureofcode.com/book/chapter-8-fractals/
|
||||
#84-the-koch-curve-and-the-arraylist-technique )
|
||||
|
||||
Requirements (pip):
|
||||
- matplotlib
|
||||
- numpy
|
||||
"""
|
||||
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import matplotlib.pyplot as plt # type: ignore
|
||||
import numpy
|
||||
|
||||
# initial triangle of Koch snowflake
|
||||
VECTOR_1 = numpy.array([0, 0])
|
||||
VECTOR_2 = numpy.array([0.5, 0.8660254])
|
||||
VECTOR_3 = numpy.array([1, 0])
|
||||
INITIAL_VECTORS = [VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1]
|
||||
|
||||
# uncomment for simple Koch curve instead of Koch snowflake
|
||||
# INITIAL_VECTORS = [VECTOR_1, VECTOR_3]
|
||||
|
||||
|
||||
def iterate(initial_vectors: list[numpy.ndarray], steps: int) -> list[numpy.ndarray]:
|
||||
"""
|
||||
Go through the number of iterations determined by the argument "steps".
|
||||
Be careful with high values (above 5) since the time to calculate increases
|
||||
exponentially.
|
||||
>>> iterate([numpy.array([0, 0]), numpy.array([1, 0])], 1)
|
||||
[array([0, 0]), array([0.33333333, 0. ]), array([0.5 , \
|
||||
0.28867513]), array([0.66666667, 0. ]), array([1, 0])]
|
||||
"""
|
||||
vectors = initial_vectors
|
||||
for i in range(steps):
|
||||
vectors = iteration_step(vectors)
|
||||
return vectors
|
||||
|
||||
|
||||
def iteration_step(vectors: list[numpy.ndarray]) -> list[numpy.ndarray]:
|
||||
"""
|
||||
Loops through each pair of adjacent vectors. Each line between two adjacent
|
||||
vectors is divided into 4 segments by adding 3 additional vectors in-between
|
||||
the original two vectors. The vector in the middle is constructed through a
|
||||
60 degree rotation so it is bent outwards.
|
||||
>>> iteration_step([numpy.array([0, 0]), numpy.array([1, 0])])
|
||||
[array([0, 0]), array([0.33333333, 0. ]), array([0.5 , \
|
||||
0.28867513]), array([0.66666667, 0. ]), array([1, 0])]
|
||||
"""
|
||||
new_vectors = []
|
||||
for i, start_vector in enumerate(vectors[:-1]):
|
||||
end_vector = vectors[i + 1]
|
||||
new_vectors.append(start_vector)
|
||||
difference_vector = end_vector - start_vector
|
||||
new_vectors.append(start_vector + difference_vector / 3)
|
||||
new_vectors.append(
|
||||
start_vector + difference_vector / 3 + rotate(difference_vector / 3, 60)
|
||||
)
|
||||
new_vectors.append(start_vector + difference_vector * 2 / 3)
|
||||
new_vectors.append(vectors[-1])
|
||||
return new_vectors
|
||||
|
||||
|
||||
def rotate(vector: numpy.ndarray, angle_in_degrees: float) -> numpy.ndarray:
|
||||
"""
|
||||
Standard rotation of a 2D vector with a rotation matrix
|
||||
(see https://en.wikipedia.org/wiki/Rotation_matrix )
|
||||
>>> rotate(numpy.array([1, 0]), 60)
|
||||
array([0.5 , 0.8660254])
|
||||
>>> rotate(numpy.array([1, 0]), 90)
|
||||
array([6.123234e-17, 1.000000e+00])
|
||||
"""
|
||||
theta = numpy.radians(angle_in_degrees)
|
||||
c, s = numpy.cos(theta), numpy.sin(theta)
|
||||
rotation_matrix = numpy.array(((c, -s), (s, c)))
|
||||
return numpy.dot(rotation_matrix, vector)
|
||||
|
||||
|
||||
def plot(vectors: list[numpy.ndarray]) -> None:
|
||||
"""
|
||||
Utility function to plot the vectors using matplotlib.pyplot
|
||||
No doctest was implemented since this function does not have a return value
|
||||
"""
|
||||
# avoid stretched display of graph
|
||||
axes = plt.gca()
|
||||
axes.set_aspect("equal")
|
||||
|
||||
# matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all
|
||||
# y-coordinates as inputs, which are constructed from the vector-list using
|
||||
# zip()
|
||||
x_coordinates, y_coordinates = zip(*vectors)
|
||||
plt.plot(x_coordinates, y_coordinates)
|
||||
plt.show()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
||||
processed_vectors = iterate(INITIAL_VECTORS, 5)
|
||||
plot(processed_vectors)
|
150
fractals/mandelbrot.py
Normal file
150
fractals/mandelbrot.py
Normal file
@ -0,0 +1,150 @@
|
||||
"""
|
||||
The Mandelbrot set is the set of complex numbers "c" for which the series
|
||||
"z_(n+1) = z_n * z_n + c" does not diverge, i.e. remains bounded. Thus, a
|
||||
complex number "c" is a member of the Mandelbrot set if, when starting with
|
||||
"z_0 = 0" and applying the iteration repeatedly, the absolute value of
|
||||
"z_n" remains bounded for all "n > 0". Complex numbers can be written as
|
||||
"a + b*i": "a" is the real component, usually drawn on the x-axis, and "b*i"
|
||||
is the imaginary component, usually drawn on the y-axis. Most visualizations
|
||||
of the Mandelbrot set use a color-coding to indicate after how many steps in
|
||||
the series the numbers outside the set diverge. Images of the Mandelbrot set
|
||||
exhibit an elaborate and infinitely complicated boundary that reveals
|
||||
progressively ever-finer recursive detail at increasing magnifications, making
|
||||
the boundary of the Mandelbrot set a fractal curve.
|
||||
(description adapted from https://en.wikipedia.org/wiki/Mandelbrot_set )
|
||||
(see also https://en.wikipedia.org/wiki/Plotting_algorithms_for_the_Mandelbrot_set )
|
||||
"""
|
||||
|
||||
|
||||
import colorsys
|
||||
|
||||
from PIL import Image # type: ignore
|
||||
|
||||
|
||||
def get_distance(x: float, y: float, max_step: int) -> float:
|
||||
"""
|
||||
Return the relative distance (= step/max_step) after which the complex number
|
||||
constituted by this x-y-pair diverges. Members of the Mandelbrot set do not
|
||||
diverge so their distance is 1.
|
||||
|
||||
>>> get_distance(0, 0, 50)
|
||||
1.0
|
||||
>>> get_distance(0.5, 0.5, 50)
|
||||
0.061224489795918366
|
||||
>>> get_distance(2, 0, 50)
|
||||
0.0
|
||||
"""
|
||||
a = x
|
||||
b = y
|
||||
for step in range(max_step):
|
||||
a_new = a * a - b * b + x
|
||||
b = 2 * a * b + y
|
||||
a = a_new
|
||||
|
||||
# divergence happens for all complex number with an absolute value
|
||||
# greater than 4
|
||||
if a * a + b * b > 4:
|
||||
break
|
||||
return step / (max_step - 1)
|
||||
|
||||
|
||||
def get_black_and_white_rgb(distance: float) -> tuple:
|
||||
"""
|
||||
Black&white color-coding that ignores the relative distance. The Mandelbrot
|
||||
set is black, everything else is white.
|
||||
|
||||
>>> get_black_and_white_rgb(0)
|
||||
(255, 255, 255)
|
||||
>>> get_black_and_white_rgb(0.5)
|
||||
(255, 255, 255)
|
||||
>>> get_black_and_white_rgb(1)
|
||||
(0, 0, 0)
|
||||
"""
|
||||
if distance == 1:
|
||||
return (0, 0, 0)
|
||||
else:
|
||||
return (255, 255, 255)
|
||||
|
||||
|
||||
def get_color_coded_rgb(distance: float) -> tuple:
|
||||
"""
|
||||
Color-coding taking the relative distance into account. The Mandelbrot set
|
||||
is black.
|
||||
|
||||
>>> get_color_coded_rgb(0)
|
||||
(255, 0, 0)
|
||||
>>> get_color_coded_rgb(0.5)
|
||||
(0, 255, 255)
|
||||
>>> get_color_coded_rgb(1)
|
||||
(0, 0, 0)
|
||||
"""
|
||||
if distance == 1:
|
||||
return (0, 0, 0)
|
||||
else:
|
||||
return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(distance, 1, 1))
|
||||
|
||||
|
||||
def get_image(
|
||||
image_width: int = 800,
|
||||
image_height: int = 600,
|
||||
figure_center_x: float = -0.6,
|
||||
figure_center_y: float = 0,
|
||||
figure_width: float = 3.2,
|
||||
max_step: int = 50,
|
||||
use_distance_color_coding: bool = True,
|
||||
) -> Image.Image:
|
||||
"""
|
||||
Function to generate the image of the Mandelbrot set. Two types of coordinates
|
||||
are used: image-coordinates that refer to the pixels and figure-coordinates
|
||||
that refer to the complex numbers inside and outside the Mandelbrot set. The
|
||||
figure-coordinates in the arguments of this function determine which section
|
||||
of the Mandelbrot set is viewed. The main area of the Mandelbrot set is
|
||||
roughly between "-1.5 < x < 0.5" and "-1 < y < 1" in the figure-coordinates.
|
||||
|
||||
>>> get_image().load()[0,0]
|
||||
(255, 0, 0)
|
||||
>>> get_image(use_distance_color_coding = False).load()[0,0]
|
||||
(255, 255, 255)
|
||||
"""
|
||||
img = Image.new("RGB", (image_width, image_height))
|
||||
pixels = img.load()
|
||||
|
||||
# loop through the image-coordinates
|
||||
for image_x in range(image_width):
|
||||
for image_y in range(image_height):
|
||||
|
||||
# determine the figure-coordinates based on the image-coordinates
|
||||
figure_height = figure_width / image_width * image_height
|
||||
figure_x = figure_center_x + (image_x / image_width - 0.5) * figure_width
|
||||
figure_y = figure_center_y + (image_y / image_height - 0.5) * figure_height
|
||||
|
||||
distance = get_distance(figure_x, figure_y, max_step)
|
||||
|
||||
# color the corresponding pixel based on the selected coloring-function
|
||||
if use_distance_color_coding:
|
||||
pixels[image_x, image_y] = get_color_coded_rgb(distance)
|
||||
else:
|
||||
pixels[image_x, image_y] = get_black_and_white_rgb(distance)
|
||||
|
||||
return img
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
|
||||
# colored version, full figure
|
||||
img = get_image()
|
||||
|
||||
# uncomment for colored version, different section, zoomed in
|
||||
# img = get_image(figure_center_x = -0.6, figure_center_y = -0.4,
|
||||
# figure_width = 0.8)
|
||||
|
||||
# uncomment for black and white version, full figure
|
||||
# img = get_image(use_distance_color_coding = False)
|
||||
|
||||
# uncomment to save the image
|
||||
# img.save("mandelbrot.png")
|
||||
|
||||
img.show()
|
76
fractals/sierpinski_triangle.py
Normal file
76
fractals/sierpinski_triangle.py
Normal file
@ -0,0 +1,76 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
"""Author Anurag Kumar | anuragkumarak95@gmail.com | git/anuragkumarak95
|
||||
|
||||
Simple example of Fractal generation using recursive function.
|
||||
|
||||
What is Sierpinski Triangle?
|
||||
>>The Sierpinski triangle (also with the original orthography Sierpinski), also called
|
||||
the Sierpinski gasket or the Sierpinski Sieve, is a fractal and attractive fixed set
|
||||
with the overall shape of an equilateral triangle, subdivided recursively into smaller
|
||||
equilateral triangles. Originally constructed as a curve, this is one of the basic
|
||||
examples of self-similar sets, i.e., it is a mathematically generated pattern that can
|
||||
be reproducible at any magnification or reduction. It is named after the Polish
|
||||
mathematician Wacław Sierpinski, but appeared as a decorative pattern many centuries
|
||||
prior to the work of Sierpinski.
|
||||
|
||||
Requirements(pip):
|
||||
- turtle
|
||||
|
||||
Python:
|
||||
- 2.6
|
||||
|
||||
Usage:
|
||||
- $python sierpinski_triangle.py <int:depth_for_fractal>
|
||||
|
||||
Credits: This code was written by editing the code from
|
||||
http://www.riannetrujillo.com/blog/python-fractal/
|
||||
|
||||
"""
|
||||
import sys
|
||||
import turtle
|
||||
|
||||
PROGNAME = "Sierpinski Triangle"
|
||||
|
||||
points = [[-175, -125], [0, 175], [175, -125]] # size of triangle
|
||||
|
||||
|
||||
def getMid(p1, p2):
|
||||
return ((p1[0] + p2[0]) / 2, (p1[1] + p2[1]) / 2) # find midpoint
|
||||
|
||||
|
||||
def triangle(points, depth):
|
||||
|
||||
myPen.up()
|
||||
myPen.goto(points[0][0], points[0][1])
|
||||
myPen.down()
|
||||
myPen.goto(points[1][0], points[1][1])
|
||||
myPen.goto(points[2][0], points[2][1])
|
||||
myPen.goto(points[0][0], points[0][1])
|
||||
|
||||
if depth > 0:
|
||||
triangle(
|
||||
[points[0], getMid(points[0], points[1]), getMid(points[0], points[2])],
|
||||
depth - 1,
|
||||
)
|
||||
triangle(
|
||||
[points[1], getMid(points[0], points[1]), getMid(points[1], points[2])],
|
||||
depth - 1,
|
||||
)
|
||||
triangle(
|
||||
[points[2], getMid(points[2], points[1]), getMid(points[0], points[2])],
|
||||
depth - 1,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
raise ValueError(
|
||||
"right format for using this script: "
|
||||
"$python fractals.py <int:depth_for_fractal>"
|
||||
)
|
||||
myPen = turtle.Turtle()
|
||||
myPen.ht()
|
||||
myPen.speed(5)
|
||||
myPen.pencolor("red")
|
||||
triangle(points, int(sys.argv[1]))
|
Reference in New Issue
Block a user