| Crates.io | chaos-game |
| lib.rs | chaos-game |
| version | 0.1.6 |
| created_at | 2025-08-04 02:33:07.907601+00 |
| updated_at | 2025-08-05 04:45:16.678253+00 |
| description | A simple command-line application for generating fractals using the 'Chaos Game' algorithm. |
| homepage | https://github.com/benjaminrall/chaos-game/ |
| repository | https://github.com/benjaminrall/chaos-game/ |
| max_upload_size | |
| id | 1780251 |
| size | 70,212 |
A simple command-line application written in Rust for generating fractals using the 'Chaos Game' algorithm.
This application generates fractals using the following simple, iterative algorithm:
By adjusting the number of vertices, the distance ratio, and optionally adding additional restrictions to the choice of vertices, a huge variety of intricate fractal patterns can be generated. More details on the algorithm can be found here.
Below are some example fractals generated with this application.
| Fractal | Parameters | Image |
|---|---|---|
| Sierpiński Triangle | $n=3$, $r=0.5$ | |
| Rainbow Hex Fractal | $n=6$, $r=0.5$ | |
| Spirals Fractal | $n=5$, $r=0.5$, Rule: Cannot pick the same vertex twice in a row. | |
| Star Fractal | $n=5$, $r=0.5$, Rule: If a vertex is picked twice in a row, the next pick cannot be a direct neighbour of it. |
To use the application, you can simply install it using Cargo:
cargo install chaos-game
You can then run it from the command line, providing arguments to customise the generated fractal.
This basic example generates the Sierpiński triangle, one of the most well-known fractals produced by the Chaos Game.
chaos-game -n 3 -r 0.5 -o sierpinski.png
You can see all available options by running the application with the --help flag.
| Option | Short Flag | Description | Default Value |
|---|---|---|---|
--sides |
-n |
The number of sides of the fractal polygon. | 3 |
--ratio |
-r |
The distance ratio for point interpolation (0.0 to 1.0). | 0.5 |
--iterations |
-i |
The total number of iterations to run the algorithm for. | 100,000,000 |
--output |
-o |
The output filename for the final PNG image. | output.png |
--coloured |
-c |
A flag to generate coloured fractals based on vertex angle. | false |
--colour-scale |
An aesthetic parameter to control image brightness. | 4.0 |
|
--image-size |
The width and height of the square image in pixels. | 1000 |
|
--rotation-offset |
A rotation offset for the polygon in degrees. | 0.0 |
|
--rule |
The name of the rule to use for selecting vertices. | "default" |
To set up the project for development and add your own custom rules, follow these steps:
Clone the repository
git clone https://github.com/benjaminrall/chaos-game.git
cd chaos-game
Run the application:
You can build and run the project directly with Cargo. Using the --release flag is recommended for
performance.
cargo run --release -- -n 6 -c -o example.png
The application is designed to be easily extensible with custom rules for generating more complex fractals. Rules consist of a function acting on some history of previous points and a proposed new point, and must return a boolean indicating whether the new point is valid.
To create your own rule, follow these steps:
Create a file for your new rule:
Create a new file in the chaos-game/src/rules/ directory (e.g., my_rule.rs)
Write your rule function:
Inside the new file, write a function that takes the history of previous points and a proposed new point, and
returns whether the point is valid. Decorate it with the #[rule] attribute, giving it a unique name and
specifying how much history it needs.
// chaos-game/src/rules/my_rule.rs
use std::collections::VecDeque;
use chaos_game_macros::rule;
use crate::types::Vertex;
// An example rule that doesn't allow the same vertex to be selected twice in a row
#[rule("my-rule", history = 1)]
fn no_repeats(previous_points: &VecDeque<&Vertex>, new_point: &Vertex) -> bool {
if previous_points.len() == 0 {
return true;
}
previous_points[0].index != new_point.index
}
Add your rule to the rules module:
Inside chaos-game/src/rules/mod.rs, define your rule as a module by its filename (e.g. mod my_rule;)
Use your new rule:
After completing this setup, the rule will be automatically registered and available to use from the command line:
cargo run --release -- -n 5 -c --rule my-rule
This project is licensed under the MIT License. See the LICENSE file for details.