### 7 releases

0.8.7 | Oct 4, 2022 |
---|---|

0.8.3 | Jul 2, 2021 |

0.8.2 | Feb 10, 2021 |

0.8.1 | Apr 29, 2020 |

0.1.0 | Feb 21, 2020 |

#**308** in Game dev

**5,093** downloads per month

Used in **11** crates
(5 directly)

**MIT**license

72KB

2K
SLoC

# bracket-geometry

This crate provides geometry support for the overall

system, and is useful stand-alone. It provides some geometric primitives (`bracket-lib`

, `Point`

, `Point3D`

), support functions and distance calculations. It also includes Bresenham's line algorithm, a vector line algorithm, and Bresenham's Circle algorithm.`Rect`

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

file:`Cargo .toml`

`[``dependencies``]`
`bracket-geometry ``=` `"`~0.8`"`

## Point2D

A basic 2D (integer) point. You can create points with:

: Create a point with any integer-compatible type (it will be`new`

internally, but uses`i32`

).`TryFrom`

: a`zero`

point.`(``0``,``0``)`

: takes a`from_tuple`

tuple.`(``i32``,``i32``)`

It also provides conversion to other types:

for simple array striding (use the`to_index`

crate for better implementations).`bracket-algorithm-traits`

converts to an`to_tuple`

tuple.`(``i32``,``i32``)`

converts to a`to_unsigned_tuple`

tuple.`(``usize``,``usize``)`

converts to an UltraViolet`to_vec2`

.`Vec2`

converts to an UltraViolet`to_vec2i`

.`Vec2i`

is also implemented for these.`From`

## Point3D

A basic 3D (integer) point. You can create points with:

: Create a point from any integer-compatible type (x/y/z).`new`

: Creates a point from an`from_tuple`

tuple.`(``i32``,``i32``,``i32``)`

It also provides conversion to UltraViolet's

and `Vec3`

types.`Vec3i`

## Rectangle (`Rect`

)

`Rect`

Represents a rectangle in 2D space. You can create rectangles with:

: provide an X/Y and a width/height.`with_size`

: provide all four coordinates.`with_exact`

: a zeroed rectangle.`zero`

It provides quite a few helper functions:

- does this rectangle intersect with another rectangle?`intersect`

- the middle point of the rectangle.`center`

- is a point inside the rectangle (including edges)?`point_in_rect`

- call a passed lambda/callback for each point in the rectangle.`for_each`

- returns a`point_set`

of all points in the rectangle.`HashSet`

and`width`

return the current dimensions of the rectangle.`height`

## Line Plotting

Line plotting is provided using Bresenham and vector algorithms. You can return points in the line as either a vector of

objects, or an iterator.`Point`

For example, returning a line as a vector:

`use` `bracket_geometry``::``prelude``::``*``;`
`let` bresenham_line `=` `line2d``(``LineAlg``::`Bresenham`,` `Point``::`new`(``1``,``1``)``,` `Point``::`new`(``5``,``5``)``)``;`
`println!``(``"``{:?}``"``,` bresenham_line`)``;`

Or iterating along the points:

`use` `bracket_geometry``::``prelude``::``*``;`
`for` point `in` `Bresenham``::`new`(``Point``::`new`(``1``,``1``)``,` `Point``::`new`(``5``,``5``)``)` `{`
`println!``(``"``{:?}``"``,` point`)``;`
`}`

You can substitute

with `LineAlg ::`Bresenham

`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` `bracket_geometry``::``prelude``::``*``;`
`for` point `in` `BresenhamCircle``::`new`(``Point``::`new`(``10``,``10``)``,` `5``)` `{`
`println!``(``"``{:?}``"``,` point`)``;`
`}`

## Distance Heuristics

There's a full set of distance algorithms available:

`use` `bracket_geometry``::``prelude``::``*``;`
`println!``(``"``{:?}``"``,` `DistanceAlg``::`Pythagoras`.``distance2d``(``Point``::`new`(``0``,``0``)``,` `Point``::`new`(``5``,``5``)``)``)``;`
`println!``(``"``{:?}``"``,` `DistanceAlg``::`PythagorasSquared`.``distance2d``(``Point``::`new`(``0``,``0``)``,` `Point``::`new`(``5``,``5``)``)``)``;`
`println!``(``"``{:?}``"``,` `DistanceAlg``::`Manhattan`.``distance2d``(``Point``::`new`(``0``,``0``)``,` `Point``::`new`(``5``,``5``)``)``)``;`
`println!``(``"``{:?}``"``,` `DistanceAlg``::`Chebyshev`.``distance2d``(``Point``::`new`(``0``,``0``)``,` `Point``::`new`(``5``,``5``)``)``)``;`

## Feature Flags

If you enable

, it provides serialization/de-serialization via the `serde`

library for the `Serde`

, `Point`

and `Point3D`

types.`Rect`

## Examples

The following examples are included; they use

to print to your terminal. Run examples with `crossterm`

:`cargo`` run --example <name>`

- draws a circle.`bresenham_circle`

- draws a line.`bresenham_line`

- draws a line, using an algorithm that doesn't smooth corners.`vector_line`

- calculates distance between two points with all supported algorithms and outputs it.`distance`

#### Dependencies

~1–15MB

~195K SLoC