bracket-geometry
This crate provides geometry support for the overall bracket-lib system, and is useful stand-alone. It provides some geometric primitives (Point, Point3D, Rect), support functions and distance calculations. It also includes Bresenham's line algorithm, a vector line algorithm, and Bresenham's Circle algorithm.
It uses UltraViolet for fast processing, and includes conversion functions to/from native UltraViolet types.
Using bracket-geometry
You can include it in your project by adding the following to your Cargo.toml file:
[]
= "0.7.0"
Point2D
A basic 2D (integer) point. You can create points with:
new: Create a point with any integer-compatible type (it will bei32internally, but usesTryFrom).zero: a(0,0)point.from_tuple: takes a(i32, i32)tuple.
It also provides conversion to other types:
to_indexfor simple array striding (use thebracket-algorithm-traitscrate for better implementations).to_tupleconverts to an(i32, i32)tuple.to_unsigned_tupleconverts to a(usize, usize)tuple.to_vec2converts to an UltraVioletVec2.to_vec2iconverts to an UltraVioletVec2i.
From is also implemented for these.
Point3D
A basic 3D (integer) point. You can create points with:
new: Create a point from any integer-compatible type (x/y/z).from_tuple: Creates a point from an(i32,i32,i32)tuple.
It also provides conversion to UltraViolet's Vec3 and Vec3i types.
Rectangle (Rect)
Represents a rectangle in 2D space. You can create rectangles with:
with_size: provide an X/Y and a width/height.with_exact: provide all four coordinates.zero: a zeroed rectangle.
It provides quite a few helper functions:
intersect- does this rectangle intersect with another rectangle?center- the middle point of the rectangle.point_in_rect- is a point inside the rectangle (including edges)?for_each- call a passed lambda/callback for each point in the rectangle.point_set- returns aHashSetof all points in the rectangle.widthandheightreturn the current dimensions of the rectangle.
Line Plotting
Line plotting is provided using Bresenham and vector algorithms. You can return points in the line as either a vector of Point objects, or an iterator.
For example, returning a line as a vector:
use *;
let bresenham_line = line2d;
println!;
Or iterating along the points:
use *;
for point in new
You can substitute LineAlg::Bresenham with LineAlg::Vector to use a simple vector-based projection instead (this is faster on some systems).
Circle Plotting
Bresenham's circle algorithm is also included. For example:
use *;
for point in new
Distance Heuristics
There's a full set of distance algorithms available:
use *;
println!;
println!;
println!;
println!;
Feature Flags
If you enable serde, it provides serialization/de-serialization via the Serde library for the Point, Point3D and Rect types.
Examples
The following examples are included; they use crossterm to print to your terminal. Run examples with cargo run --example <name>:
bresenham_circle- draws a circle.bresenham_line- draws a line.vector_line- draws a line, using an algorithm that doesn't smooth corners.distance- calculates distance between two points with all supported algorithms and outputs it.