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 twoPoint
referencesRectangle
β from two diagonalPoint
referencesTriangle
β from threePoint
referencesCircle
β from aPoint
and 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