25 releases

0.4.1 Mar 20, 2024
0.3.0 Dec 27, 2023
0.2.2 May 20, 2023
0.1.11 Feb 5, 2023
0.1.4 Nov 6, 2022

#131 in Math

Download history 213/week @ 2023-12-20 38/week @ 2023-12-27 91/week @ 2024-01-03 9/week @ 2024-01-10 4/week @ 2024-01-24 4/week @ 2024-02-14 28/week @ 2024-02-21 11/week @ 2024-02-28 4/week @ 2024-03-06 25/week @ 2024-03-13 164/week @ 2024-03-20 24/week @ 2024-03-27 49/week @ 2024-04-03

263 downloads per month
Used in 2 crates (via buffer-graphics-lib)

Custom license

135KB
3.5K SLoC

Crates.io Documentation

Shapes for Graphics

Contains code to make and alter various shapes.

Primarily designed to be used with Buffer Graphics and Pixels graphics lib

Usage

Add this line to Cargo.toml

graphics-shapes = "0.4.0"
# or with both features (serde is enabled by default)
graphics-shapes = {version = "0.4.0", features = ["mint"] }

Examples

let rect = Rect::new((10,10),(20,20));
assert!(rect.contains(coord!(15,15)));
let triangle = Triangle::new((34,5),(12,30),(9,10));
let rotated = triangle.rotate(45);

let start = coord!(20,130);
let dist = start.distance((30,130));

Shapes

  • Line
  • Rect
  • Triangle
  • Circle
  • Ellipse
  • Polygon

Shared methods

  • contains (Coord | Shape) - Returns true if param is entirely inside
  • intersects (Shape) - Returns true if param is partially inside/touching
  • outline_pixels - Returns a list of points that can be used to draw a stroke version
  • filled_pixels - Returns a list of points that can be used to draw a filled version
  • rotate, scale, transform - Copy and change the shape

Per shape methods

All the shapes have methods to create similar sized shapes of different types, e.g. Circle::to_outer_rect(), Rect::to_triangles()

Working with multiple shapes

Each Shape is a separate struct so to store them without putting them in a Box you can use ShapeBox which implements Shape and so is fully compatible with other Shapes and their methods.

Assertions

This library uses debug assertions for some methods.

Features

Default features: "serde"

Serde

serde adds serde::{Serialize, Deserialize} to Coord, Line, Rect, Circle, Triangle, Ellipse, Polygon

Mint

mint adds a From impl for Point2<isize> to Coord

Known issues

  • Ellipses don't render correctly when rotated

Dependencies

~0.4–1MB
~24K SLoC