on dbfc352 help sign in
elk/libraries/Math/Vector2.elm
104 lines 2.8 kB history raw
1module Math.Vector2 where
2{-| A high performance linear algebra library using native JS arrays. Geared
3towards 3D graphics and use with `Graphics.WebGL`. All vectors are immutable.
5# Create
6@docs v2
8# Get and Set
9The set functions create a new copy of the vector, updating a single field.
11@docs getX, getY, setX, setY
13# Operations
14@docs add, sub, negate, scale, dot, normalize, direction,
15 length, lengthSquared, distance, distanceSquared
17# Conversions
18toTuple, fromTuple, toRecord, fromRecord
19-}
21import Native.Math.Vector2
23data Vec2 = Vec2
25{-| Creates a new 2-element vector with the given values. -}
26v2 : Float -> Float -> Vec2
27v2 = Native.Math.Vector2.v2
29{-| Extract the x component of a vector. -}
30getX : Vec2 -> Float
31getX = Native.Math.Vector2.getX
33{-| Extract the y component of a vector. -}
34getY : Vec2 -> Float
35getY = Native.Math.Vector2.getY
37{-| Update the x component of a vector, returning a new vector. -}
38setX : Float -> Vec2 -> Vec2
39setX = Native.Math.Vector2.setX
41{-| Update the y component of a vector, returning a new vector. -}
42setY : Float -> Vec2 -> Vec2
43setY = Native.Math.Vector2.setY
45{-| Convert a vector to a tuple. -}
46toTuple : Vec2 -> (Float,Float)
47toTuple = Native.Math.Vector2.toTuple
49{-| Convert a vector to a record. -}
50toRecord : Vec2 -> { x:Float, y:Float }
51toRecord = Native.Math.Vector2.toRecord
53{-| Convert a tuple to a vector. -}
54fromTuple : (Float,Float) -> Vec2
55fromTuple = Native.Math.Vector2.fromTuple
57{-| Convert a record to a vector. -}
58fromRecord : { x:Float, y:Float } -> Vec2
59fromRecord = Native.Math.Vector2.fromRecord
61{-| Vector addition: a + b -}
62add : Vec2 -> Vec2 -> Vec2
63add = Native.Math.Vector2.add
65{-| Vector subtraction: a - b -}
66sub : Vec2 -> Vec2 -> Vec2
67sub = Native.Math.Vector2.sub
69{-| Vector negation: -a -}
70negate : Vec2 -> Vec2
71negate = Native.Math.Vector2.neg
73{-| The normalized direction from a to b: (a - b) / |a - b| -}
74direction : Vec2 -> Vec2 -> Vec2
75direction = Native.Math.Vector2.direction
77{-| The length of the given vector: |a| -}
78length : Vec2 -> Float
79length = Native.Math.Vector2.length
81{-| The square of the length of the given vector: |a| * |a| -}
82lengthSquared : Vec2 -> Float
83lengthSquared = Native.Math.Vector2.lengthSquared
85{-| The distance between two vectors. -}
86distance : Vec2 -> Vec2 -> Float
87distance = Native.Math.Vector2.distance
89{-| The square of the distance between two vectors. -}
90distanceSquared : Vec2 -> Vec2 -> Float
91distanceSquared = Native.Math.Vector2.distanceSquared
93{-| A unit vector with the same direction as the given vector: a / |a| -}
94normalize : Vec2 -> Vec2
95normalize = Native.Math.Vector2.normalize
97{-| Multiply the vector by a scalar: a * k -}
98scale : Vec2 -> Float -> Vec2
99scale = Native.Math.Vector2.scale
101{-| The dot product of a and b -}
102dot : Vec2 -> Vec2 -> Float
103dot = Native.Math.Vector2.dot