### 15 releases

0.0.16 | Aug 18, 2024 |
---|---|

0.0.15 | Aug 4, 2024 |

0.0.11 | Jul 30, 2024 |

0.0.2 | Feb 26, 2024 |

#**421** in Algorithms

**208** downloads per month

Used in impact-rs

**MIT**license

25KB

469 lines

# Fixed32 Math

is a Rust crate that provides efficient 2D vector and rectangle operations using fixed-point arithmetic.
Designed for applications where fixed precision is preferred, this crate is ideal for scenarios such as graphics programming,
game development, and embedded systems where deterministic results are crucial.`fixed32_math`

## Overview

### Vector

The

struct represents a 2D vector and supports various operations including addition, subtraction, scaling,
normalization, and more. The vector components use fixed-point arithmetic via the `Vector`

crate.`fixed32`

#### Example Usage

`use` `fixed32``::`Fp`;`
`use` `your_crate_name``::`Vector`;`
`//` Create vectors using the `From` trait
`let` v1 `=` `Vector``::`from`(``(``2``,` `3``)``)``;` `//` Automatically converts (2, 3) to Vector with Fp::from
`let` v2 `=` `Vector``::`from`(``(``1``,` `4``)``)``;`
`//` Vector operations
`let` sum `=` v1 `+` v2`;`
`let` dot_product `=` v1`.``dot``(``&`v2`)``;`
`let` normalized `=` v1`.``normalize``(``)``.``unwrap``(``)``;`
`let` rotated `=` v1`.``rotate``(``Fp``::`from`(``90``)``.``to_radians``(``)``)``;`

### Rect

The

struct represents an axis-aligned rectangle defined by its lower left position and size.
It supports operations such as intersection, union, expansion, and contraction, as well as checking
if a point or another rectangle is contained within it.`Rect`

#### Example Usage

`use` `fixed32``::`Fp`;`
`use` `your_crate_name``::``{`Vector`,` Rect`}``;`
`//` Create Rect instances using `From` trait
`let` rect1 `=` `Rect``::`from`(``(``0``,` `0``,` `10``,` `10``)``)``;`
`let` rect2 `=` `Rect``::`from`(``(``5``,` `5``,` `15``,` `15``)``)``;`
`//` Rectangle operations
`let` intersection `=` rect1`.``intersection``(``&`rect2`)``;`
`let` union `=` rect1`.``union``(``&`rect2`)``;`
`let` contains_point `=` rect1`.``contains_point``(``&``Vector``::`from`(``(``2``,` `2``)``)``)``;`
`let` expanded_rect `=` rect1`.``expanded``(``Vector``::`from`(``(``2``,` `2``)``)``)``;`

#### Using `From`

Trait

`From`The

trait is implemented for convenient creation of `From`

and `Rect`

from tuples of integers:`Vector`

`//` Create a Rect from a tuple of integers
`let` rect `=` `Rect``::`from`(``(``1``,` `2``,` `3``,` `4``)``)``;`
`let` rect2 `=` `Rect``::`from`(``(``1.``24``,` `2.``34``,` `3.``98``,` `4.``01``)``)``;`
`//` Create a Vector from a tuple of integers
`let` vector `=` `Vector``::`from`(``(``2``,` `2``)``)``;`

This feature allows you to create

and `Rect`

instances in a more concise manner without needing to
manually convert integer and float values to fixed-point numbers (`Vector`

).`fixed32 ::`Fp

## Features

**Fixed-Point Arithmetic**: Uses

for all calculations, avoiding floating-point inaccuracies.`fixed32`Fp`::`**Comprehensive Operations**: Includes basic arithmetic operations, normalization, rotation, and more for vectors.**Rectangle Operations**: Includes area, perimeter, intersection, union, and containment checks for rectangles.

## Installation

Add the following to your

:`Cargo.toml`

`[``dependencies``]`
`fixed32_math ``=` `"`0.0.16`"`

## License

This project is licensed under the MIT License. See the LICENSE file for details.

#### Dependencies

~39KB