Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/omaraaa/VecFns
Automatic Vector Math Functions In Zig
https://github.com/omaraaa/VecFns
gamedev library math zig
Last synced: 29 days ago
JSON representation
Automatic Vector Math Functions In Zig
- Host: GitHub
- URL: https://github.com/omaraaa/VecFns
- Owner: omaraaa
- License: mit-0
- Created: 2021-12-22T12:14:36.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-08-26T21:11:37.000Z (over 1 year ago)
- Last Synced: 2024-08-03T23:23:41.546Z (4 months ago)
- Topics: gamedev, library, math, zig
- Language: Zig
- Homepage:
- Size: 37.1 KB
- Stars: 23
- Watchers: 1
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-zig - omaraaa/VecFns
- awesome-zig - VecFns🗒️Automatic Vector Math Functions In Zig
README
# VecFns
Automatic vector math functions for your vector structs.### Usage
Just add `VecFns` with `usingnamespace` to your struct, you will get the usual math functions for your vector. All fields of the struct should be the same type, and also be numeric.
```zig
const VecFns = @import("./vec_fns.zig").VecFns;const MyVec = struct {
pub usingnamespace VecFns(@This());
a: i32,
b: i32,
c: i32
};const Vec2f = struct {
pub usingnamespace VecFns(@This());
x: f64,
y: f64,
};```
### Functions
The following functions will be in the namespace of your struct type after including `VecFns`.
```zig
// Applies a function `f` to the vector. `args` is any additional arguments to be passed to `f`.
fn map(self: Self, comptime f: anytype, args: anytype) Self// Same as `map` but without args argument.
fn apply(self: Self, comptime f: anytype) Self// Applies a function `f` to 2 vectors.
fn map2(a: anytype, b: anytype, comptime f: anytype, args: anytype) Self// Same as `map`, reduces the vector into a single value.
fn reduce(self: Self, comptime f: anytype, args: anytype) T// Adds either 2 vectors or a vector and a scalar value.
fn add(self: Self, other: anytype) Self// Subtracts either 2 vectors or a vector and a scalar value.
fn sub(self: Self, other: anytype) Self// Multiplies either 2 vectors or a vector and a scalar value.
fn mul(self: Self, other: anytype) Self// Divides either 2 vectors or a vector and a scalar value.
fn div(self: Self, other: anytype) Self// Same as div, but uses `@divExact`
fn divExact(self: Self, other: anytype) Self// Same as div, but uses `@divFloor`
fn divFloor(self: Self, other: anytype) Self// Returns the sum of the vector.
fn sum(self: Self) T// Given 2 vectors or a vector and a scalar value, returns a vector with the maximum between the 2.
fn max(self: Self, other: Self) Self// Given 2 vectors or a vector and a scalar value, returns a vector with the minimum between the 2.
fn min(self: Self, other: Self) Self// Checks if the vector is equal to `other` (can be a vector or a scalar)
fn eq(self: Self, other: anytype) bool// Attempts to covert/cast the vector into another vector type.
fn into(self: Self, comptime VType: type) VType// Joins two vector into a vector size 2\*N.
fn join(self: Self, other: Self) [2 * N]T// Returns the zero vector.
fn zero() Self// Returns a vector where all values are `n`.
fn all(n: anytype) Self// Coverts the vector into an array of type `[N]T`.
fn toArray(self: Self) [N]T// Create a vector from an array.
fn fromArray(array: [N]T) Self// Length of the vector. (Float only)
fn len(self: Self) T// Distance between 2 vectors. (Float only)
fn distance(self: Self, other: anytype) T// Normalizes the vector values to [0, 1]. (Float only)
fn norm(self: Self) Self