elm-explorations / linear-algebra / Math.Vector2

A high performance linear algebra library using native JS arrays. Geared towards 3D graphics and use with Graphics.WebGL. All vectors are immutable.

Create


type Vec2

Two dimensional vector type

vec2 : Basics.Float -> Basics.Float -> Vec2

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

Get and Set

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

getX : Vec2 -> Basics.Float

Extract the x component of a vector.

getY : Vec2 -> Basics.Float

Extract the y component of a vector.

setX : Basics.Float -> Vec2 -> Vec2

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

setY : Basics.Float -> Vec2 -> Vec2

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

Operations

add : Vec2 -> Vec2 -> Vec2

Vector addition: a + b

sub : Vec2 -> Vec2 -> Vec2

Vector subtraction: a - b

negate : Vec2 -> Vec2

Vector negation: -a

scale : Basics.Float -> Vec2 -> Vec2

Multiply the vector by a scalar: s * v

dot : Vec2 -> Vec2 -> Basics.Float

The dot product of a and b

normalize : Vec2 -> Vec2

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

direction : Vec2 -> Vec2 -> Vec2

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

length : Vec2 -> Basics.Float

The length of the given vector: |a|

lengthSquared : Vec2 -> Basics.Float

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

distance : Vec2 -> Vec2 -> Basics.Float

The distance between two vectors.

distanceSquared : Vec2 -> Vec2 -> Basics.Float

The square of the distance between two vectors.

Conversions

toRecord : Vec2 -> { x : Basics.Float, y : Basics.Float }

Convert a vector to a record.

fromRecord : { x : Basics.Float, y : Basics.Float } -> Vec2

Convert a record to a vector.