escherlies / elm-vectors / Math.Vector3

A high performance linear algebra library using native Elm ADT.

Create


type Vec a
    = Vec a a a

Three dimensional vector type

vec3 : a -> a -> a -> Vec a

Creates a new 3-element vector with the given values.

Get and Set

The set functions create a new copy of the vector, updating a single field.

getX : Vec a -> a

Extract the x component of a vector.

getY : Vec a -> a

Extract the y component of a vector.

getZ : Vec a -> a

Extract the z component of a vector.

setX : a -> Vec a -> Vec a

Update the x component of a vector, returning a new vector.

setY : a -> Vec a -> Vec a

Update the y component of a vector, returning a new vector.

setZ : a -> Vec a -> Vec a

Update the z component of a vector, returning a new vector.

Operations

add : Vec number -> Vec number -> Vec number

Vector addition: a + b

sub : Vec number -> Vec number -> Vec number

Vector subtraction: a - b

negate : Vec number -> Vec number

Vector negation: -a

scale : number -> Vec number -> Vec number

Multiply the vector by a scalar: s * v

divBy : Basics.Float -> Vec Basics.Float -> Vec Basics.Float

Divide the vector by a scalar: s * v

dot : Vec number -> Vec number -> number

The dot product of a and b

mul : Vec number -> Vec number -> Vec number

Component-wise multiplication

normalize : Vec Basics.Float -> Vec Basics.Float

A unit vector with the same direction as the given vector: a / |a|

direction : Vec Basics.Float -> Vec Basics.Float -> Vec Basics.Float

The normalized direction from b to a: (a - b) / |a - b|

length : Vec Basics.Float -> Basics.Float

The length of the given vector: |a|

lengthSquared : Vec number -> number

The square of the length of the given vector: |a| * |a|

distance : Vec Basics.Float -> Vec Basics.Float -> Basics.Float

The distance between two vectors.

distanceSquared : Vec number -> Vec number -> number

The square of the distance between two vectors.

Transform

mapX : (a -> a) -> Vec a -> Vec a

Map a fn to the x value

mapY : (a -> a) -> Vec a -> Vec a

Map a fn to the y value

mapZ : (a -> a) -> Vec a -> Vec a

Map a fn to the z value

map : (a -> b) -> Vec a -> Vec b

Map a fn to the x and y value

apply : Vec (a -> b) -> Vec a -> Vec b

lift2 : (a -> b -> c) -> Vec a -> Vec b -> Vec c

Lift a binary function to vectors.

lift2 (+) (Vec 1 1 1) (Vec 2 2 2)
--> Vec 3 3 3 : Vec number

Keep in mind that this comes at a cost of doing a step extra and if you need performance, you might be better off deconstructing and applying the function manually.

lift3 : (a -> b -> c -> d) -> Vec a -> Vec b -> Vec c -> Vec d

Lift a binary function to vectors.

Conversions

toRecord : Vec a -> { x : a, y : a, z : a }

Convert a vector to a record.

fromRecord : { x : a, y : a, z : a } -> Vec a

Construct a vector from a record.

fromTuple : ( a, a, a ) -> Vec a

Construct a vector from a tuple.

toFloat : Vec Basics.Int -> Vec Basics.Float

Convert an integer vector into a float vector.

round : Vec Basics.Float -> Vec Basics.Int

Round the number components to the nearest integer.

floor : Vec Basics.Float -> Vec Basics.Int

Floor function, rounding down.

ceiling : Vec Basics.Float -> Vec Basics.Int

Ceiling function, rounding up.

truncate : Vec Basics.Float -> Vec Basics.Int

Truncate the number components, rounding towards zero.