Procedural generation is the creation of data or content algorithmically rather than manually.
This project was started as a creative programming experiment, combining computer graphics, mathematics, and programming. It also allowed me to gain hands-on experience with Pygame and procedural generation.
This project is a creative exploration of procedural generation, combining mathematical principles with computer graphics using Pygame. The algorithm generates a unique grid-based spiral using the
The algorithm iteratively generates line segments based on the
Definition 1.0) The
This metric is named for its resemblance to the grid-like paths in a city like Manhattan, where one can only travel along perpendicular directions.
The derived formula used has no specific name. In my research, some discrete or discrete geometry math books derive it, but no name is given. It is simply referred to as a property of the
Definition 1.1) To compute any point
Stated formula only works for the first quadrant. Getting the formula for other quadrants will be discussed in the mathematical derivations section. To see its code implementation see file grid_spiral_functional.py, line 32.
This art piece ( Figure 1.1 ) is generated by running an algorithm that utilizes the properties of the NUM_OF_STEPS. In each iteration, the tail of
Definition 1.2) Every phantom_line generated will be equal to the difference between the y-coordinates of
The program begins by computing the GenerateNewLines(), which will calculate the phantom_line (Definition 1.0) and then call ModifySpiralStep() to scale it by SPIRAL_STEP_SIZE and return it. Lastly, the returned length is cast into an integer, named radius, and passed to the derived
At the end of the main algorithm loop, each point myLines[], and the points
Finally, at the end of the program, the grid spiral is rendered using a loop to access the indices of myLines[], which stores the head and tail coordinates for each line segment previously generated. The function to draw a line in the Pygame library is:
pygame.draw.line(surface, colour, start_pos, end_pos, width=1)Since the coordinates are appended sequentially and stored in a 2D array (myLines[]), rendering the line segments is straightforward. A simple loop will access each index, which itself contains the head and tail coordinates for each line.
The second file Grid_Spiral_OOP.py is an implementation of the same algorithm described above, but following OOP principles. This was done to scale the program's ability to generate multiple grid spiral patterns while still being modular and clean to allow for maintenance and quick and easy modifications.
Fork the repository and clone it to your local machine:
git clone https://github.com/your-username/Grid-Spiral.git
cd Grid-SpiralEnsure you have Python installed, then install the required dependencies:
pip install -r requirements.txtExecute the script by running:
python Grid_spiral_functional.pyThis will open a Pygame window and render the generated grid spiral.
You can tweak the spiral generation by modifying these variables in Grid_spiral_functional.py:
NUM_OF_STEPS– Adjust the number of iterations to generate a larger or smaller spiral.SPIRAL_STEP_SIZE– Change the spacing between spiral segments.LINE_WIDTH– Modify the thickness of the lines.
For a more modular and scalable approach, run the OOP-based version:
python Grid_Spiral_OOP.pyThis project serves as a demonstration of mathematical visualization through procedural generation and provides an interactive way to explore the properties of the

