on bf13e7d help sign in
elk/libraries/Math/Vector.elm
126 lines 2.8 kB history raw
1module Math.Vector where
3{-| A high performance linear algebra library using native JS arrays. Geared
4towards 3D graphics and use with `Graphics.WebGL`.
6All vectors are immutable.
8# Create
9@docs v3, i, j, k
11# Get and Set
12The set functions create a new copy of the vector, updating a single field.
14@docs getX, getY, getZ, setX, setY, setZ, toTuple, toRecord
16# Operations
17@docs add, sub, neg, direction, length, lengthSquared, normalize, scale, dot, cross
19-}
21import Native.MJS
23data Vec2 = Vec2
24data Vec3 = Vec3
25data Vec4 = Vec4
27{-| Creates a new 3-element vector with the given values.
28-}
29v3 : Float -> Float -> Float -> Vec3
30v3 = Native.MJS.v3
32{-| The unit vector î which points in the x direction: `v3 1 0 0` -}
33i : Vec3
34i = Native.MJS.v3 1 0 0
36{-| The unit vector ĵ which points in the y direction: `v3 0 1 0` -}
37j : Vec3
38j = Native.MJS.v3 0 1 0
40{-| The unit vector &kcirc; which points in the z direction: `v3 0 0 1` -}
41k : Vec3
42k = Native.MJS.v3 0 0 1
44{-| Extract the x component of a vector. -}
45getX : Vec3 -> Float
46getX = Native.MJS.getX
48{-| Extract the y component of a vector. -}
49getY : Vec3 -> Float
50getY = Native.MJS.getY
52{-| Extract the z component of a vector. -}
53getZ : Vec3 -> Float
54getZ = Native.MJS.getZ
56{-| Update the x component of a vector. -}
57setX : Vec3 -> Float
58setX = Native.MJS.setX
60{-| Update the y component of a vector. -}
61setY : Vec3 -> Float
62setY = Native.MJS.setY
64{-| Update the z component of a vector. -}
65setZ : Vec3 -> Float
66setZ = Native.MJS.setZ
68{-| Convert a vector to a tuple.
69-}
70toTuple : Vec3 -> (Float,Float,Float)
71toTuple = Native.MJS.toTuple3
73{-| Convert a vector to a record.
74-}
75toRecord : Vec3 -> { x:Float, y:Float, z:Float }
76toRecord = Native.MJS.toRecord3
78{-| Vector addition: a + b
79-}
80add : Vec3 -> Vec3 -> Vec3
81add = Native.MJS.v3add
83{-| Vector subtraction: a - b
84-}
85sub : Vec3 -> Vec3 -> Vec3
86sub = Native.MJS.v3sub
88{-| Vector negation: -a
89-}
90neg : Vec3 -> Vec3
91neg = Native.MJS.v3neg
93{-| The normalized direction from a to b: (a - b) / |a - b|
94-}
95direction : Vec3 -> Vec3 -> Vec3
96direction = Native.MJS.v3direction
98{-| The length of the given vector: |a|
99-}
100length : Vec3 -> Float
101length = Native.MJS.v3length
103{-| The square of the length of the given vector: |a| * |a|
104-}
105lengthSquared : Vec3 -> Float
106lengthSquared = Native.MJS.v3lengthSquared
108{-| A unit vector with the same direction as the given vector: a / |a|
109-}
110normalize : Vec3 -> Vec3
111normalize = Native.MJS.v3normalize
113{-| Multiply the vector by a scalar: a * k
114-}
115scale : Vec3 -> Float -> Vec3
116scale = Native.MJS.v3scale
118{-| The dot product of a and b
119-}
120dot : Vec3 -> Vec3 -> Float
121dot = Native.MJS.v3dot
123{-| The cross product of a and b
124-}
125cross : Vec3 -> Vec3 -> Vec3
126cross = Native.MJS.v3cross