This package implements common fractals in pgzero and pygame. Fractal generation code written in C for fast rendering.
Project description
common-fractals
This package includes classes for drawing common fractals onto pgzero and pygame.
Usage
This package has 5 fractals implemented:
- Koch Snowflake (KochSnowflake)
- Koch Anti-Snowflake (KochAntiSnowflake)
- Mandelbrot Set (MandelbrotSet)
- Newton Fractal (NewtonFractal)
- Sierpinski Triangle (SierpinskiTriangle)
This package also includes a base Fractal class which is the super class for all of the fractals mentioned above. The Fractal class does not change the effects or add methods to the sub classes.
Koch Snowflake
An example of drawing the Koch Snowflake in a pgzero game is shown below (this example includes all of the parameters available in the KochSnowflake class):
import pgzrun # Imports pgzrun necessary for running pgzero scripts. You may run your script with `pgzrun my_file.py` if you do not want to import pgzrun
from common_fractals import KochSnowflake # Imports KochSnowflake necessary for creating the Snowflake
WIDTH = 800 # Sets the width of the window
HEIGHT = 800 # Sets the height of the window
koch_snowflake = KochSnowflake(iterations = 20, side_length = 200, topleft = (100, 100)) # Creates the Snowflake. You should not call this in your draw function because that would decrease speed significantly
def draw(): # The draw function where all of the drawing components of the pgzero script are in
screen.fill((255, 255, 255)) # Fills the screen with white so that all components on the window are shown and nothing is overlapping with the previous frame
koch_snowflake.draw_pgzero(screen, color = "green", thickness = 2) # Draws the Snowflake. The screen parameter should be the screen you use to draw on the window
def update(): # The update function where all of the other logic (that does not involve sensing like mouse click) goes
pass # This is necessary even though it is empty so that we can force the game loop to update
pgzrun.go() # This is necessary to run the pgzero script. This is not necessary if you are running your file from the terminal with `pgzrun my_file.py`
koch_snowflake also has other drawing methods. Two for pgzero and two for pygame:
- draw_pgzero (shown in the example above)
- draw_pgzero_filled (does not have the thickness parameter)
- draw_pygame (has the thickness parameter)
- draw_pygame_filled (does not have the thickness parameter)
All of them have the same parameters. For pygame, the screen parameter is the surface of which you want to draw the Snowflake onto.
If you want to see a pygame example of the Koch Snowflake, see the Koch Anti-Snowflake. Everything is the same except the class names.
Koch Anti-Snowflake
An example of drawing the Koch Anti-Snowflake in a pygame game is shown below (this example includes all of the parameters available in the KochAntiSnowflake class):
import pygame # Imports pygame necessary for creating pygame games
from common_fractals import KochAntiSnowflake # Imports KochAntiSnowflake necessary for creating the Anti-Snowflake
pygame.init() # Initializes Pygame (sets up pygame)
screen = pygame.display.set_mode((800, 800)) # Creates the display window
koch_anti_snowflake = KochAntiSnowflake(iterations = 20, side_length = 200, topleft = (100, 100)) # Creates the Anti-Snowflake. You should not call this in your while loop because that would decrease speed significantly
running = True # Creates the variable which controls if the game loop is running or not.
while running: # Main loop
for event in pygame.event.get(): # Loops through all of the events currently happpening
if event.type == pygame.QUIT: # Checks if the user closes the window
running = False # Stops the main loop when the user closes the window
screen.fill((255, 255, 255)) # Fills the window with white so that all components on the window are shown and nothing is overlapping with the previous frame
koch_anti_snowflake.draw_pygame(screen, color = "green", thickness = 2) # Draws the Snowflake. The surface parameter should be the display screen or a different Surface
pygame.display.flip() # Updates the display loop
pygame.quit() # Quits pygame (closes the window) when the user closes it
koch_anti_snowflake has the same methods as the KochSnowflake.
If you want to see a pgzero example of the Koch Anti-Snowflake, see the Koch Snowflake. Everything is the same except the class names.
Mandelbrot Set
An example of drawing the Mandelbrot Set in a pgzero and pygame game is shown in the two examples below (this example includes all of the parameters available in the MandelbrotSet class):
Pgzero example
import pgzrun # Imports pgzrun necessary for running pgzero scripts. You may run your script with `pgzrun my_file.py` if you do not want to import pgzrun
from common_fractals import MandelbrotSet # Imports MandelbrotSet necessary for creating the Mandelbrot Set
WIDTH = 800 # Sets the width of the window
HEIGHT = 800 # Sets the height of the window
mandelbrot_set = MandelbrotSet(iterations = 10000, scale = 50, width = 600, height = 600, topleft = (100, 100)) # Creates the Mandelbrot Set. You should not call this in your draw function because that would decrease speed significantly
def draw(): # The draw function where all of the drawing components of the pgzero script are in
screen.fill((255, 255, 255)) # Fills the screen with white so that all components on the window are shown and nothing is overlapping with the previous frame
mandelbrot_set.draw_pgzero(screen, color = "green", thickness = 2) # Draws the Mandelbrot Set. The screen parameter should be the screen you use to draw on the window
def update(): # The update function where all of the other logic (that does not involve sensing like mouse click) goes
pass # This is necessary even though it is empty so that we can force the game loop to update
pgzrun.go() # This is necessary to run the pgzero script. This is not necessary if you are running your file from the terminal with `pgzrun my_file.py`
mandelbrot_set has the same methods as the KochSnowflake and KochAntiSnowflake.
Pygame example
import pygame # Imports pygame necessary for creating pygame games
from common_fractals import MandelbrotSet # Imports MandelbrotSet necessary for creating the Mandelbrot Set
pygame.init() # Initializes Pygame (sets up pygame)
screen = pygame.display.set_mode((800, 800)) # Creates the display window
mandelbrot_set = MandelbrotSet(iterations = 10000, scale = 50, width = 600, height = 600, topleft = (100, 100)) # Creates the Mandelbrot Set. You should not call this in your while loop because that would decrease speed significantly
running = True # Creates the variable which controls if the game loop is running or not.
while running: # Main loop
for event in pygame.event.get(): # Loops through all of the events currently happpening
if event.type == pygame.QUIT: # Checks if the user closes the window
running = False # Stops the main loop when the user closes the window
screen.fill((255, 255, 255)) # Fills the window with white so that all components on the window are shown and nothing is overlapping with the previous frame
mandelbrot_set.draw_pygame(screen, color = "green", thickness = 2) # Draws the Snowflake. The surface parameter should be the display screen or a different Surface
pygame.display.flip() # Updates the display loop
pygame.quit() # Quits pygame (closes the window) when the user closes it
mandelbrot_set has the same methods as the KochSnowflake and KochAntiSnowflake.
Newton Fractal
An example of drawing the Newton Fractal in a pgzero and pygame game is shown in the two examples below (this example includes all of the parameters available in the NewtonFractal class):
Pgzero Example
import pgzrun # Imports pgzrun necessary for running pgzero scripts. You may run your script with `pgzrun my_file.py` if you do not want to import pgzrun
from common_fractals import NewtonFractal # Imports NewtonFractal necessary for creating the Newton Fractal
WIDTH = 800 # Sets the width of the window
HEIGHT = 800 # Sets the height of the window
newton_fractal = NewtonFractal(iterations = 50, width = 600, height = 600, scale = 75, animate = True, show_roots = True, point_scale = 1 / 2,
root_scale: float = 1, func = lambda x: x ** 3 + 2 * (x ** 2) + 10, colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)], print_current_iter = True) # Creates the Newton Fractal. You should not call this in your draw function because that would decrease speed significantly. The length of the colors must be equal to or greater than the degree measure (the highest power in func. In this case, it is 3)
def draw(): # The draw function where all of the drawing components of the pgzero script are in
screen.fill((255, 255, 255)) # Fills the screen with white so that all components on the window are shown and nothing is overlapping with the previous frame
newton_fractal.draw_pgzero(screen, color = "green", thickness = 2) # Draws the Newton Fractal. The screen parameter should be the screen you use to draw on the window
def update(): # The update function where all of the other logic (that does not involve sensing like mouse click) goes
pass # This is necessary even though it is empty so that we can force the game loop to update
pgzrun.go() # This is necessary to run the pgzero script. This is not necessary if you are running your file from the terminal with `pgzrun my_file.py`
newton_fractal has the same methods as the KochSnowflake, KochAntiSnowflake, and MandelbrotSet.
Pygame Example
import pygame # Imports pygame necessary for creating pygame games
from common_fractals import NewtonFractal # Imports NewtonFractal necessary for creating the Newton Fractal
pygame.init() # Initializes Pygame (sets up pygame)
screen = pygame.display.set_mode((800, 800)) # Creates the display window
newton_fractal = NewtonFractal(iterations = 50, width = 600, height = 600, scale = 75, animate = True, show_roots = True, point_scale = 1 / 2,
root_scale: float = 1, func = lambda x: x ** 3 + 2 * (x ** 2) + 10, colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255)], print_current_iter = True) # Creates the Newton Fractal. You should not call this in your draw function because that would decrease speed significantly. The length of the colors must be equal to or greater than the degree measure (the highest power in func. In this case, it is 3)
running = True # Creates the variable which controls if the game loop is running or not.
while running: # Main loop
for event in pygame.event.get(): # Loops through all of the events currently happpening
if event.type == pygame.QUIT: # Checks if the user closes the window
running = False # Stops the main loop when the user closes the window
screen.fill((255, 255, 255)) # Fills the window with white so that all components on the window are shown and nothing is overlapping with the previous frame
newton_fractal.draw_pgzero(screen, color = "green", thickness = 2) # Draws the Newton Fractal. The surface parameter should be the display screen or a different Surface
pygame.display.flip() # Updates the display loop
pygame.quit() # Quits pygame (closes the window) when the user closes it
newton_fractal has the same methods as the KochSnowflake, KochAntiSnowflake, and MandelbrotSet.
Versions
(Latest) Version 0.0.1: Base Code added.
Coming Soon
Version 0.0.2: C code for Koch Snowflake (KochSnowflake) added.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file common_fractals-0.0.1.tar.gz.
File metadata
- Download URL: common_fractals-0.0.1.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20e29cd19298b3b666d8f7d18312eafa0bdf54b035658d8879c3e81184be4048
|
|
| MD5 |
cd1ae8273f5881a0e8caae8c5a3bd426
|
|
| BLAKE2b-256 |
fdfe14ace01f1b1e378386b805f4db2053d33040faed73d619b962314717585a
|
File details
Details for the file common_fractals-0.0.1-py3-none-any.whl.
File metadata
- Download URL: common_fractals-0.0.1-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df169c45029901bfdc17eda8b46bf9289386060b4d02c2d374e47d64a7369f59
|
|
| MD5 |
b5cfd7e8d6e5337d7abf4a760c02d811
|
|
| BLAKE2b-256 |
49c504604ace5f8eb9d478922284e7c9e7941dd4fc7243da678b047479a566aa
|