Fork of vmath for nimskull
Go to file
Ryan Oldenburg f83eb0b7b2 more clear
2020-11-30 01:07:08 -06:00
.github/workflows workflow version 2020-11-14 16:35:46 -06:00
src more clear 2020-11-30 01:07:08 -06:00
tests add missing 2020-11-30 00:50:07 -06:00
.gitignore Improve docs and use unix line endings (#2) 2020-03-05 17:44:26 -08:00
README.md func->proc 2020-11-29 16:55:52 -06:00
vmath.nimble func->proc 2020-11-29 16:55:52 -06:00

VMath

nimble install vmath

Collection of math routines for 2d and 3d graphics.

Has functions for Vec2, Vec3, Vec4, Mat3, Mat4 and Quat.

API: vmath

import vmath

func between

Returns true if value is between min and max or equal to them.

func between(value, min, max: float32): bool

func sign

Returns the sign of a number, -1 or 1.

func sign(v: float32): float32

func quantize

Makes v be multipe of n. Rounding to integer quantize by 1.0.

func quantize(v: float32; n: float32): float32

func lerp

Interpolates value between a and b.

  • 0 -> a
  • 1 -> b
  • 0.5 -> between a and b
func lerp(a: float32; b: float32; v: float32): float32

type Vec2

2D vector

Vec2 = object
 x*: float32
 y*: float32

func vec2

func vec2(x, y: float32): Vec2

func vec2

func vec2(v: float32): Vec2

func vec2

func vec2(a: Vec2): Vec2

func +

func `+`(a: Vec2; b: Vec2): Vec2

func -

func `-`(a: Vec2; b: Vec2): Vec2

func *

func `*`(a: Vec2; b: float32): Vec2

func *

func `*`(a: float32; b: Vec2): Vec2

func /

func `/`(a: Vec2; b: float32): Vec2

func +=

func `+=`(a: var Vec2; b: Vec2)

func -=

func `-=`(a: var Vec2; b: Vec2)

func *=

func `*=`(a: var Vec2; b: float32)

func /=

func `/=`(a: var Vec2; b: float32)

func zero

func zero(a: var Vec2)

func -

func `-`(a: Vec2): Vec2

func hash

func hash(a: Vec2): Hash

func lengthSq

func lengthSq(a: Vec2): float32

func length

func length(a: Vec2): float32

func length=

func length=(a: var Vec2; b: float32)

func normalize

func normalize(a: Vec2): Vec2

func dot

func dot(a: Vec2; b: Vec2): float32

func dir

func dir(at: Vec2; to: Vec2): Vec2

func dir

func dir(th: float32): Vec2

func dist

func dist(at: Vec2; to: Vec2): float32

func distSq

func distSq(at: Vec2; to: Vec2): float32

func lerp

func lerp(a: Vec2; b: Vec2; v: float32): Vec2

func quantize

func quantize(v: Vec2; n: float32): Vec2

func inRect

Check to see if v is inside a rectange formed by a and b. It does not matter how a and b are arranged.

func inRect(v: Vec2; a: Vec2; b: Vec2): bool

func []

func `[]`(a: Vec2; i: int): float32

func []=

func `[]=`(a: var Vec2; i: int; b: float32)

proc randVec2

proc randVec2(): Vec2

func $

func `$`(a: Vec2): string {.raises: [ValueError].}

func fixAngle

Make angle be from -PI to PI radians.

func fixAngle(angle: float32): float32

func angle

Angle of a Vec2.

func angle(a: Vec2): float32

func angleBetween

Angle between 2 Vec2.

func angleBetween(a: Vec2; b: Vec2): float32

func angleBetween

Angle between angle a and angle b.

func angleBetween(a, b: float32): float32

func turnAngle

Move from angle a to angle b with step of v.

func turnAngle(a, b, speed: float32): float32

type Vec3

3D vector

Vec3 = object
 x*: float32
 y*: float32
 z*: float32

func vec3

func vec3(x, y, z: float32): Vec3

func vec3

func vec3(v: float32): Vec3

func vec3

func vec3(a: Vec3): Vec3

const X_DIR

X_DIR = (x: 1.0, y: 0.0, z: 0.0)

const Y_DIR

Y_DIR = (x: 0.0, y: 1.0, z: 0.0)

const Z_DIR

Z_DIR = (x: 0.0, y: 0.0, z: 1.0)

func +

func `+`(a: Vec3; b: Vec3): Vec3

func -

func `-`(a: Vec3; b: Vec3): Vec3

func -

func `-`(a: Vec3): Vec3

func *

func `*`(a: Vec3; b: float32): Vec3

func *

func `*`(a: float32; b: Vec3): Vec3

func /

func `/`(a: Vec3; b: float32): Vec3

func /

func `/`(a: float32; b: Vec3): Vec3

func +=

func `+=`(a: var Vec3; b: Vec3)

func -=

func `-=`(a: var Vec3; b: Vec3)

func *=

func `*=`(a: var Vec3; b: float32)

func /=

func `/=`(a: var Vec3; b: float32)

func zero

func zero(a: var Vec3)

func -

func `-`(a: var Vec3): Vec3

func hash

func hash(a: Vec3): Hash

func lengthSq

func lengthSq(a: Vec3): float32

func length

func length(a: Vec3): float32

func length=

func length=(a: var Vec3; b: float32)

func floor

func floor(a: Vec3): Vec3

func round

func round(a: Vec3): Vec3

func ceil

func ceil(a: Vec3): Vec3

func normalize

func normalize(a: Vec3): Vec3

func cross

func cross(a: Vec3; b: Vec3): Vec3

func computeNormal

func computeNormal(a, b, c: Vec3): Vec3

func dot

func dot(a: Vec3; b: Vec3): float32

func dir

func dir(at: Vec3; to: Vec3): Vec3

func dist

func dist(at: Vec3; to: Vec3): float32

func distSq

func distSq(at: Vec3; to: Vec3): float32

func lerp

func lerp(a: Vec3; b: Vec3; v: float32): Vec3

func quantize

func quantize(v: Vec3; n: float32): Vec3

func angleBetween

func angleBetween(a, b: Vec3): float32

func []

func `[]`(a: Vec3; i: int): float32

func []=

func `[]=`(a: var Vec3; i: int; b: float32)

func xy

func xy(a: Vec3): Vec2

func xz

func xz(a: Vec3): Vec2

func yx

func yx(a: Vec3): Vec2

func yz

func yz(a: Vec3): Vec2

func zx

func zx(a: Vec3): Vec2

func zy

func zy(a: Vec3): Vec2

func almostEquals

func almostEquals(a, b: Vec3; precision = 1e-006): bool

proc randVec3

Generates a random unit vector based on http://mathworld.wolfram.com/SpherePointPicking.html

proc randVec3(): Vec3

func $

func `$`(a: Vec3): string {.raises: [ValueError].}

type Vec4

4D Vector.

Vec4 = object
 x*: float32
 y*: float32
 z*: float32
 w*: float32

func vec4

func vec4(x, y, z, w: float32): Vec4

func vec4

func vec4(v: float32): Vec4

func +

func `+`(a: Vec4; b: Vec4): Vec4

func -

func `-`(a: Vec4; b: Vec4): Vec4

func -

func `-`(a: Vec4): Vec4

func *

func `*`(a: Vec4; b: float32): Vec4

func *

func `*`(a: float32; b: Vec4): Vec4

func /

func `/`(a: Vec4; b: float32): Vec4

func /

func `/`(a: float32; b: Vec4): Vec4

func +=

func `+=`(a: var Vec4; b: Vec4)

func -=

func `-=`(a: var Vec4; b: Vec4)

func *=

func `*=`(a: var Vec4; b: float32)

func /=

func `/=`(a: var Vec4; b: float32)

func zero

func zero(a: var Vec4)

func hash

func hash(a: Vec4): Hash

func []

func `[]`(a: Vec4; i: int): float32

func []=

func `[]=`(a: var Vec4; i: int; b: float32)

func lerp

func lerp(a: Vec4; b: Vec4; v: float32): Vec4

func xyz

func xyz(a: Vec4): Vec3

func $

func `$`(a: Vec4): string {.raises: [ValueError].}

func vec3

func vec3(a: Vec2; z = 0.0): Vec3

func vec4

func vec4(a: Vec3; w = 0.0): Vec4

func vec4

func vec4(a: Vec2; z = 0.0; w = 0.0): Vec4

type Mat3

3x3 Matrix

Mat3 = array[9, float32]

template []

template `[]`(a: Mat3; i, j: int): float32

template []=

template `[]=`(a: Mat3; i, j: int; v: float32)

func mat3

func mat3(a, b, c, d, e, f, g, h, i: float32): Mat3

func mat3

func mat3(a: Mat3): Mat3

func identity

func identity(a: var Mat3)

func mat3

func mat3(): Mat3

func transpose

func transpose(a: Mat3): Mat3

func $

func `$`(a: Mat3): string {.raises: [ValueError].}

func *

func `*`(a: Mat3; b: Mat3): Mat3

func scale

func scale(a: Mat3; v: Vec2): Mat3

func scale

func scale(a: Mat3; v: Vec3): Mat3

func translate

func translate(v: Vec2): Mat3

func scale

func scale(v: Vec2): Mat3

func rotationMat3

func rotationMat3(angle: float32): Mat3

func rotate

func rotate(a: Mat3; angle: float32): Mat3

func *

func `*`(a: Mat3; b: Vec2): Vec2

func *

func `*`(a: Mat3; b: Vec3): Vec3

func inverse

func inverse(a: Mat3): Mat3

type Mat4

4x4 Matrix - OpenGL row order

Mat4 = array[16, float32]

template []

template `[]`(a: Mat4; i, j: int): float32

template []=

template `[]=`(a: Mat4; i, j: int; v: float32)

func mat4

func mat4(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15: float32): Mat4

func mat4

func mat4(a: Mat4): Mat4

func identity

func identity(): Mat4

func mat4

func mat4(): Mat4

func transpose

func transpose(a: Mat4): Mat4

func determinant

func determinant(a: Mat4): float32

func inverse

func inverse(a: Mat4): Mat4

func *

func `*`(a, b: Mat4): Mat4

func *

func `*`(a: Mat4; b: Vec3): Vec3

func *

func `*`(a: Mat4; b: Vec4): Vec4

func right

func right(a: Mat4): Vec3

func right=

func right=(a: var Mat4; b: Vec3)

func up

func up(a: Mat4): Vec3

func up=

func up=(a: var Mat4; b: Vec3)

func forward

func forward(a: Mat4): Vec3

func forward=

func forward=(a: var Mat4; b: Vec3)

func pos

func pos(a: Mat4): Vec3

func pos=

func pos=(a: var Mat4; b: Vec3)

func rotationOnly

func rotationOnly(a: Mat4): Mat4

func dist

func dist(a, b: Mat4): float32

func translate

func translate(v: Vec3): Mat4

func scale

func scale(v: Vec3): Mat4

func close

func close(a: Mat4; b: Mat4): bool

func hrp

func hrp(m: Mat4): Vec3

func frustum

func frustum(left, right, bottom, top, near, far: float32): Mat4

func perspective

func perspective(fovy, aspect, near, far: float32): Mat4

func ortho

func ortho(left, right, bottom, top, near, far: float32): Mat4

func lookAt

func lookAt(eye, center, up: Vec3): Mat4

func mat3

Gets rotation and translation, ignoring z coordinates.

func mat3(m: Mat4): Mat3

func mat3Rotation

Gets the rotational part of the 4x4 matrix.

func mat3Rotation(m: Mat4): Mat3

func mat4

Takes a 2d Mat3 with position and converts to a 3d matrix.

func mat4(m: Mat3): Mat4

func mat4Rotation

Gets the rotational part of the 3x3 matrix into a 4x4 matrix.

func mat4Rotation(m: Mat3): Mat4

func $

func `$`(a: Mat4): string {.raises: [ValueError].}

type Quat

Quat = object
 x*: float32
 y*: float32
 z*: float32
 w*: float32

func quat

func quat(x, y, z, w: float32): Quat

func conjugate

func conjugate(q: Quat): Quat

func length

func length(q: Quat): float32

func normalize

func normalize(q: Quat): Quat

func xyz

func xyz(q: Quat): Vec3

func xyz=

func xyz=(q: var Quat; v: Vec3)

func -

func `-`(a: var Quat): Quat

func +

func `+`(a: Quat; b: Quat): Quat

func *

Multiply the quaternion by a quaternion.

func `*`(a, b: Quat): Quat

func *

Multiply the quaternion by a float32.

func `*`(q: Quat; v: float32): Quat

func *

Multiply the quaternion by a vector.

func `*`(q: Quat; v: Vec3): Vec3

func []=

func `[]=`(a: var Quat; i: int; b: float32)

func mat3

func mat3(q: Quat): Mat3

func mat4

func mat4(q: Quat): Mat4

func recifuncalSqrt

func recifuncalSqrt(x: float32): float32

proc quat

proc quat(m: Mat4): Quat

func fromAxisAngle

func fromAxisAngle(axis: Vec3; angle: float32): Quat

func toAxisAngle

func toAxisAngle(q: Quat; axis: var Vec3; angle: var float32)

func quat

func quat(heading, pitch, roll: float32): Quat

func quat

func quat(hpr: Vec3): Quat

func hrp

func hrp(q: Quat): Vec3

func dot

func dot(a: Quat; b: Quat): float32

func nlerp

func nlerp(a: Quat; b: Quat; v: float32): Quat

func $

func `$`(a: Quat): string {.raises: [ValueError].}

func rotate

func rotate(angle: float32; axis: Vec3): Mat4

func rotateX

func rotateX(angle: float32): Mat4

func rotateY

func rotateY(angle: float32): Mat4

func rotateZ

func rotateZ(angle: float32): Mat4

func scaleMat

func scaleMat(scale: Vec3): Mat4

func scaleMat

func scaleMat(scale: float32): Mat4

type Rect

Rect = object
 x*: float32
 y*: float32
 w*: float32
 h*: float32

func rect

func rect(x, y, w, h: float32): Rect

func rect

func rect(pos, size: Vec2): Rect

func xy

Gets the xy as a Vec2.

func xy(rect: Rect): Vec2

func xy=

Sets the xy from Vec2.

func xy=(rect: var Rect; v: Vec2)

func wh

Gets the wh as a Vec2.

func wh(rect: Rect): Vec2

func wh=

Sets the wh from Vec2.

func wh=(rect: var Rect; v: Vec2)

func *

  • all elements of a Rect.
func `*`(r: Rect; v: float): Rect

func /

/ all elements of a Rect.

func `/`(r: Rect; v: float): Rect

func +

Add two boxes together.

func `+`(a, b: Rect): Rect

func $

func `$`(a: Rect): string {.raises: [ValueError].}

func inside

Checks if pos is inside rect.

func inside(pos: Vec2; rect: Rect): bool

func overlap

Returns true if box a overlaps box b.

func overlap(a, b: Rect): bool

proc or

Union of two rectangles.

proc `or`(a, b: Rect): Rect

proc and

Intersection of two rectangles.

proc `and`(a, b: Rect): Rect