This Rust project generates an image composed of various geometrical shapes — such as lines, circles, rectangles, triangles, and points — and saves the result as a PNG file using the raster image processing library.
Here’s the generated image showing various shapes:
This project teaches and reinforces the following key Rust programming concepts:
- Modules and File Organization
- Traits and Trait Objects
- Structs with Associated Functions
- Randomized Geometry Generation
- Algorithm Implementation (DDA ,Midpoint Circle Algorithm)
├── src
│ ├── main.rs
│ └── geometrical_shapes.rs
├── Cargo.toml
└── image.png (generated)
Each shape is defined as a struct with associated constructors:
Point— created from(x, y)Line— from twoPointreferencesRectangle— from two diagonalPointreferencesTriangle— from threePointreferencesCircle— from aPointand a radius
All shapes implement the Drawable trait, allowing them to be drawn onto a raster::Image.
-
Clone the repository
-
Add dependencies to
Cargo.toml:
[dependencies]
raster = "0.2.0"
rand = "0.9.1"- Run the program
cargo run