jjant/linear-algebra/1.0.0

Vec2

type alias Vec2 =
{ x : Float
, y : Float
}

Create

vec2 : Float -> Float -> Vec2

Create a vector out of its x and y components.

Setters

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

setX : Float -> Vec2 -> Vec2
setY : Float -> Vec2 -> Vec2

Constants

zero : Vec2
up : Vec2
down : Vec2
left : Vec2

Operations

add : Vec2 -> Vec2 -> Vec2
sub : Vec2 -> Vec2 -> Vec2

sub v1 v2 computes v1 - v2.

Can be easily confused in pipelines like

v2
    |> sub v1 -- this is actually v1 - v2

I think both alternatives are confusing, so I chose this one rather arbitrarily.

negate : Vec2 -> Vec2

Given v, computes -v.

scale : Float -> Vec2 -> Vec2
dot : Vec2 -> Vec2 -> Float
normalize : Vec2 -> Vec2

Normalizes a vector v, returning v/|v|. If the vector is 0, it returns 0.

{ from : Vec2
, to : Vec2
}
-> Vec2

Returns a normalized vector pointing from from to to.

midpoint : Vec2 -> Vec2 -> Vec2

Returns the midpoint between two points. This operation is commutative.

{ from : Vec2
, to : Vec2
}
-> Float
-> Vec2

Linearly interpolate from to to with a [0, 1] parameter.

lerp { from = vec2 0 1, to = vec2 1 1 } 0.75 == vec2 0.75 1
angle : Vec2 -> Float
scaleX : Float -> Vec2 -> Vec2
scaleY : Float -> Vec2 -> Vec2
rotate : Float -> Vec2 -> Vec2

Rotates a given vector by an angle in radians, in counterclockwise fashion.

length : Vec2 -> Float

Computes |v| for a vector v.

If you need the square of this value, use Vec2.lengthSquared for efficiency.

lengthSquared : Vec2 -> Float

Computes |v|^2 for a vector v.

Faster than length because it saves a square root operation.

distance : Vec2 -> Vec2 -> Float
distanceSquared : Vec2 -> Vec2 -> Float
cross : Vec2 -> Vec2 -> Float

Returns the z-coordinate of the cross product when interpreting the 2D vectors as 3D vectors of the form vec3 v.x v.y 0:

cross v1 v2
    == (Vec3.cross
            (vec3 v1.x v1.y 0)
            (vec3 v2.x v2.y 0)
       ).z

This operation is useful for calculating the torque created by a force in 2D, for example.

toString : Vec2 -> String

Convenient for debugging purposes.

Homogeneous coordinates

point : Vec2 -> Vec3
vector : Vec2 -> Vec3
fromHomogeneous : Vec3 -> Vec2