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