A fractal is a complex geometric shape that can be split into parts, each of which is a reduced-scale copy of the whole. This property is known as self-similarity. Fractals are often found in nature and can be mathematically generated through iterative processes.
Key characteristics of fractals include:
Self-Similarity: Fractals look similar at different scales. Each smaller section of the fractal resembles the overall shape.
Infinite Complexity: No matter how much you zoom in, there is always more detail to see. This infinite complexity means fractals have a detailed structure at arbitrarily small scales.
Fractional Dimensions: Fractals often have non-integer dimensions, which means they can occupy space in unconventional ways. For example, a fractal line might have a dimension between 1 and 2, indicating it is more than a line but less than a plane.
Formation Through Iteration: Many fractals are created by repeating a simple process over and over again. Examples include the Mandelbrot set and the Julia set.
Examples of fractals in nature include coastlines, mountains, clouds, snowflakes, and certain plants, like ferns and broccoli. Fractals are used in various fields, including computer graphics, natural sciences, and art, due to their unique properties and the patterns they create.
Julia Set Fractal
Only 40 lines of code
import matplotlib.pyplot as plt
import numpy as np
def julia_set(width, height, zoom, cX, cY, moveX, moveY, max_iter):
image = np.zeros((width, height))
# Calculate the scale based on the zoom factor
scaleX = 1.5 / (0.5 * zoom * width)
scaleY = 1.0 / (0.5 * zoom * height)
# Generate the Julia set
for x in range(width):
for y in range(height):
zx = 1.5 * (x - width / 2) / (0.5 * zoom * width) + moveX
zy = 1.0 * (y - height / 2) / (0.5 * zoom * height) + moveY
i = max_iter
while zx * zx + zy * zy < 4 and i > 1:
zy, zx = 2.0 * zx * zy + cY, zx * zx - zy * zy + cX
i -= 1
# Normalize and store the value
image[x, y] = i
return image
def main():
width, height = 800, 800
zoom = 1
cX, cY = -0.7, 0.27015
moveX, moveY = 0.0, 0.0
max_iter = 255
# Generate the Julia set image
fractal = julia_set(width, height, zoom, cX, cY, moveX, moveY, max_iter)
# Plot the fractal
plt.imshow(fractal.T, cmap='inferno', extent=[-1.5, 1.5, -1, 1])
plt.axis('off') # Turn off the axis
plt.show()
if __name__ == '__main__':
main()
Here is the output fractal: Julia Set
Julia_Set in pygame with more colors
import pygame
import numpy as np
# Julia set parameters
WIDTH, HEIGHT = 800, 600
ZOOM = 1
CX, CY = -0.7, 0.27015
MOVEX, MOVEY = 0.0, 0.0
MAX_ITER = 255
def draw_julia(surface, width, height, zoom, cX, cY, moveX, moveY, max_iter):
for x in range(width):
for y in range(height):
zx = 1.5 * (x - width / 2) / (0.5 * zoom * width) + moveX
zy = 1.0 * (y - height / 2) / (0.5 * zoom * height) + moveY
i = max_iter
while zx * zx + zy * zy < 4 and i > 1:
zy, zx = 2.0 * zx * zy + cY, zx * zx - zy * zy + cX
i -= 1
color = (i % 8 * 32, i % 16 * 16, i % 32 * 8)
surface.set_at((x, y), color)
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Julia Set')
surface = pygame.Surface((WIDTH, HEIGHT))
screen.blit(surface, (0, 0))
pygame.display.flip()
# Draw the Julia set progressively
draw_julia(surface, WIDTH, HEIGHT, ZOOM, CX, CY, MOVEX, MOVEY, MAX_ITER)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.blit(surface, (0, 0))
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
main()
Here is the beautiful fractal: Julia Set (code above)
Mandelbrot Set
The Mandelbrot Set is a complex and visually stunning fractal that can be generated using relatively simple code but involves intricate mathematical concepts that are perfect for students with knowledge of advanced calculus and geometry.
Description:
The Mandelbrot Set is defined by iterating the function fc(z) = z^2 + c
on the complex plane, where zzz and ccc are complex numbers. A point ccc is part of the Mandelbrot Set if the sequence fc(0), fc(fc(0)), fc(fc(fc(0))),… does not tend to infinity.
How to Create it with Code:
Complex Plane: Map a portion of the complex plane onto the pixels of the screen.
Iteration: For each pixel (which represents a complex number c), iterate the function fc(z) starting with z=0
Escape Condition: Track the number of iterations it takes for ∣z∣ to exceed a certain threshold (typically 2). If it doesn't exceed the threshold after a fixed number of iterations, the point is considered to be in the Mandelbrot Set.
Coloring: Use the number of iterations to determine the color of each pixel, creating a visually appealing fractal image.
Get the code:
import pygame
import numpy as np
# Screen dimensions
WIDTH, HEIGHT = 800, 800
# Mandelbrot set parameters
MAX_ITER = 100
X_MIN, X_MAX = -2.5, 1.5
Y_MIN, Y_MAX = -2.0, 2.0
def mandelbrot(c, max_iter):
z = 0
for n in range(max_iter):
if abs(z) > 2:
return n
z = z*z + c
return max_iter
def mandelbrot_set(width, height, x_min, x_max, y_min, y_max, max_iter):
r1 = np.linspace(x_min, x_max, width)
r2 = np.linspace(y_min, y_max, height)
n3 = np.empty((width, height))
for i in range(width):
for j in range(height):
n3[i, j] = mandelbrot(r1[i] + 1j*r2[j], max_iter)
return (r1, r2, n3)
def color_map(value, max_iter):
t = float(value) / max_iter
r = int(9 * (1 - t) * t * t * t * 255) % 8 * 32
g = int(15 * (1 - t) * (1 - t) * t * t * 255) % 16 * 16
b = int(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255) % 32 * 8
return (r, g, b)
def render(screen, width, height, mandelbrot_data):
r1, r2, n3 = mandelbrot_data
for x in range(width):
for y in range(height):
color = color_map(n3[x, y], MAX_ITER)
screen.set_at((x, y), color)
def main():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Mandelbrot Set")
mandelbrot_data = mandelbrot_set(WIDTH, HEIGHT, X_MIN, X_MAX, Y_MIN, Y_MAX, MAX_ITER)
render(screen, WIDTH, HEIGHT, mandelbrot_data)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.quit()
if __name__ == "__main__":
main()
Mandelbrot Fractal (code above)
Challenge Explore these fractals and see if you can write the code
Get Code for more fractals from: https://github.com/IGNITE-Pathways/fractals.git
Comments