on master help sign in
minor markdown fixes in comments
3965802Evan Czaplicki
libraries/MJS.elm+59−92
@@ -1,10 +1,7 @@
1module MJS where
1module MJS where
2
2
3−{-| MJS
4−
5−A high performance linear algebra library using
3+{-| A high performance linear algebra library using native JS arrays. Geared
6−native JS arrays. Geared towards 3D graphics
7−and use with Graphics.WebGL.
4+towards 3D graphics and use with `Graphics.WebGL`.
8
5
9Both vectors and matrices are immutable.
6Both vectors and matrices are immutable.
10
7
@@ -40,126 +37,106 @@ data V2 = V2
40data V3 = V3
37data V3 = V3
41data V4 = V4
38data V4 = V4
42
39
43−{-|
44−Creates a new 3-element vector with the given values
40+{-| Creates a new 3-element vector with the given values
45-}
41-}
46v3 : Float -> Float -> Float -> V3
42v3 : Float -> Float -> Float -> V3
47v3 = Native.MJS.v3
43v3 = Native.MJS.v3
48
44
49−{-|
50−Convert to a native tuple
45+{-| Convert to a native tuple
51-}
46-}
52toTuple3 : V3 -> (Float,Float,Float)
47toTuple3 : V3 -> (Float,Float,Float)
53toTuple3 = Native.MJS.toTuple3
48toTuple3 = Native.MJS.toTuple3
54
49
55−{-|
56−Vector addition: a + b
50+{-| Vector addition: a + b
57-}
51-}
58add : V3 -> V3 -> V3
52add : V3 -> V3 -> V3
59add = Native.MJS.v3add
53add = Native.MJS.v3add
60
54
61−{-|
62−Vector subtraction: a - b
55+{-| Vector subtraction: a - b
63-}
56-}
64sub : V3 -> V3 -> V3
57sub : V3 -> V3 -> V3
65sub = Native.MJS.v3sub
58sub = Native.MJS.v3sub
66
59
67−{-|
68−Vector negation: -a
60+{-| Vector negation: -a
69-}
61-}
70neg : V3 -> V3
62neg : V3 -> V3
71neg = Native.MJS.v3neg
63neg = Native.MJS.v3neg
72
64
73−{-|
74−The normalized direction from a to b: (a - b) / |a - b|
65+{-| The normalized direction from a to b: (a - b) / |a - b|
75-}
66-}
76direction : V3 -> V3 -> V3
67direction : V3 -> V3 -> V3
77direction = Native.MJS.v3direction
68direction = Native.MJS.v3direction
78
69
79−{-|
80−The length of the given vector: |a|
70+{-| The length of the given vector: |a|
81-}
71-}
82length : V3 -> Float
72length : V3 -> Float
83length = Native.MJS.v3length
73length = Native.MJS.v3length
84
74
85−{-|
86−The square of the length of the given vector: |a|*|a|
75+{-| The square of the length of the given vector: |a| * |a|
87-}
76-}
88lengthSquared : V3 -> Float
77lengthSquared : V3 -> Float
89lengthSquared = Native.MJS.v3lengthSquared
78lengthSquared = Native.MJS.v3lengthSquared
90
79
91−{-|
92−A unit vector with the same direction as the given vector: a / |a|
80+{-| A unit vector with the same direction as the given vector: a / |a|
93-}
81-}
94normalize : V3 -> V3
82normalize : V3 -> V3
95normalize = Native.MJS.v3normalize
83normalize = Native.MJS.v3normalize
96
84
97−{-|
98−Multiply the vector by a scalar: a * k
85+{-| Multiply the vector by a scalar: a * k
99-}
86-}
100scale : V3 -> Float -> V3
87scale : V3 -> Float -> V3
101scale = Native.MJS.v3scale
88scale = Native.MJS.v3scale
102
89
103−{-|
104−The dot product of a and b
90+{-| The dot product of a and b
105-}
91-}
106dot : V3 -> V3 -> Float
92dot : V3 -> V3 -> Float
107dot = Native.MJS.v3dot
93dot = Native.MJS.v3dot
108
94
109−{-|
110−The cross product of a and b
95+{-| The cross product of a and b
111-}
96-}
112cross : V3 -> V3 -> V3
97cross : V3 -> V3 -> V3
113cross = Native.MJS.v3cross
98cross = Native.MJS.v3cross
114
99
115−{-|
116−Methods for working with 4x4 matrices.
117−-}
118data M4x4 = M4x4
100data M4x4 = M4x4
119
101
120−{-|
121−Multiple the vector by the 4x4 matrix: m * v
102+{-| Multiply a vector by a 4x4 matrix: m * v
122-}
103-}
123mul4x4 : M4x4 -> V3 -> V3
104mul4x4 : M4x4 -> V3 -> V3
124mul4x4 = Native.MJS.v3mul4x4
105mul4x4 = Native.MJS.v3mul4x4
125
106
126−{-|
127−A matrix with all 0s, except 1s on the diagonal
107+{-| A matrix with all 0s, except 1s on the diagonal.
128-}
108-}
129identity : M4x4
109identity : M4x4
130identity = Native.MJS.m4x4identity
110identity = Native.MJS.m4x4identity
131
111
132−{-|
133−Computes the inverse of the given matrix m, assuming that
112+{-| Computes the inverse of the given matrix m, assuming that the matrix is
134−the matrix is orthonormal
113+orthonormal.
135-}
114-}
136inverseOrthonormal : M4x4 -> M4x4
115inverseOrthonormal : M4x4 -> M4x4
137inverseOrthonormal = Native.MJS.m4x4inverseOrthonormal
116inverseOrthonormal = Native.MJS.m4x4inverseOrthonormal
138
117
139−{-|
140−Creates a matrix for a projection frustum with the given parameters.
118+{-| Creates a matrix for a projection frustum with the given parameters.
141
119
142Parameters:
120Parameters:
143
121
144− left - the left coordinate of the frustum
122+ * left - the left coordinate of the frustum
145− right- the right coordinate of the frustum
123+ * right- the right coordinate of the frustum
146− bottom - the bottom coordinate of the frustum
124+ * bottom - the bottom coordinate of the frustum
147− top - the top coordinate of the frustum
125+ * top - the top coordinate of the frustum
148− znear - the near z distance of the frustum
126+ * znear - the near z distance of the frustum
149− zfar - the far z distance of the frustum
127+ * zfar - the far z distance of the frustum
150-}
128-}
151makeFrustrum : Float -> Float -> Float -> Float -> Float -> Float -> Float -> M4x4
129makeFrustrum : Float -> Float -> Float -> Float -> Float -> Float -> Float -> M4x4
152makeFrustrum = Native.MJS.m4x4makeFrustrum
130makeFrustrum = Native.MJS.m4x4makeFrustrum
153
131
154−{-|
155−Creates a matrix for a perspective projection with the given parameters.
132+{-| Creates a matrix for a perspective projection with the given parameters.
156
133
157−Parameters:
134+Parameters:
158
135
159− fovy - field of view in the y axis, in degrees
136+ * fovy - field of view in the y axis, in degrees
160− aspect - aspect ratio
137+ * aspect - aspect ratio
161− znear - the near z distance of the projection
138+ * znear - the near z distance of the projection
162− zfar - the far z distance of the projection
139+ * zfar - the far z distance of the projection
163-}
140-}
164makePerspective : Float -> Float -> Float -> Float -> M4x4
141makePerspective : Float -> Float -> Float -> Float -> M4x4
165makePerspective = Native.MJS.m4x4makePerspective
142makePerspective = Native.MJS.m4x4makePerspective
@@ -169,76 +146,68 @@ Creates a matrix for an orthogonal frustum projection with the given parameters.
169
146
170Parameters:
147Parameters:
171
148
172− left - the left coordinate of the frustum
149+ * left - the left coordinate of the frustum
173− right- the right coordinate of the frustum
150+ * right- the right coordinate of the frustum
174− bottom - the bottom coordinate of the frustum
151+ * bottom - the bottom coordinate of the frustum
175− top - the top coordinate of the frustum
152+ * top - the top coordinate of the frustum
176− znear - the near z distance of the frustum
153+ * znear - the near z distance of the frustum
177− zfar - the far z distance of the frustum
154+ * zfar - the far z distance of the frustum
178-}
155-}
179makeOrtho : Float -> Float -> Float -> Float -> Float -> Float -> Float -> M4x4
156makeOrtho : Float -> Float -> Float -> Float -> Float -> Float -> Float -> M4x4
180makeOrtho = Native.MJS.m4x4makeOrtho
157makeOrtho = Native.MJS.m4x4makeOrtho
181
158
182−{-|
183−Creates a matrix for a 2D orthogonal frustum projection with the given parameters.
159+{-| Creates a matrix for a 2D orthogonal frustum projection with the given
184−znear and zfar are assumed to be -1 and 1, respectively.
160+parameters. `znear` and `zfar` are assumed to be -1 and 1, respectively.
185
161
186Parameters:
162Parameters:
187
163
188− left - the left coordinate of the frustum
164+ * left - the left coordinate of the frustum
189− right- the right coordinate of the frustum
165+ * right- the right coordinate of the frustum
190− bottom - the bottom coordinate of the frustum
166+ * bottom - the bottom coordinate of the frustum
191− top - the top coordinate of the frustum
167+ * top - the top coordinate of the frustum
192-}
168-}
193makeOrtho2D : Float -> Float -> Float -> Float -> M4x4
169makeOrtho2D : Float -> Float -> Float -> Float -> M4x4
194makeOrtho2D = Native.MJS.m4x4makeOrtho2D
170makeOrtho2D = Native.MJS.m4x4makeOrtho2D
195
171
196−{-|
197−Matrix multiplcation: a * b
172+{-| Matrix multiplcation: a * b
198-}
173-}
199mul : M4x4 -> M4x4 -> M4x4
174mul : M4x4 -> M4x4 -> M4x4
200mul = Native.MJS.m4x4mul
175mul = Native.MJS.m4x4mul
201
176
202−{-|
203−Matrix multiplication, assuming a and b are affine: a * b
177+{-| Matrix multiplication, assuming a and b are affine: a * b
204-}
178-}
205mulAffine : M4x4 -> M4x4 -> M4x4
179mulAffine : M4x4 -> M4x4 -> M4x4
206mulAffine = Native.MJS.m4x4mulAffine
180mulAffine = Native.MJS.m4x4mulAffine
207
181
208−{-|
209−Creates a transformation matrix for rotation by angle radians about the 3-element vector axis.
182+{-| Creates a transformation matrix for rotation in radians about the
183+3-element vector axis.
210-}
184-}
211makeRotate : Float -> V3 -> M4x4
185makeRotate : Float -> V3 -> M4x4
212makeRotate = Native.MJS.m4x4makeRotate
186makeRotate = Native.MJS.m4x4makeRotate
213
187
214−{-|
215−Concatenates a rotation of angle radians about the axis to the given matrix.
188+{-| Concatenates a rotation of angle radians about the axis to the given matrix.
216-}
189-}
217rotate : Float -> V3 -> M4x4 -> M4x4
190rotate : Float -> V3 -> M4x4 -> M4x4
218rotate = Native.MJS.m4x4rotate
191rotate = Native.MJS.m4x4rotate
219
192
220−{-|
221−Creates a transformation matrix for scaling by 3 scalar values, one for
193+{-| Creates a transformation matrix for scaling by 3 scalar values, one for
222each of the x, y, and z directions.
194each of the x, y, and z directions.
223-}
195-}
224makeScale3 : Float -> Float -> Float -> M4x4
196makeScale3 : Float -> Float -> Float -> M4x4
225makeScale3 = Native.MJS.m4x4makeScale3
197makeScale3 = Native.MJS.m4x4makeScale3
226
198
227−{-|
228−Creates a transformation matrix for scaling each of the x, y, and z axes by the amount
199+{-| Creates a transformation matrix for scaling each of the x, y, and z axes by
229−given in the corresponding element of the 3-element vector.
200+the amount given in the corresponding element of the 3-element vector.
230-}
201-}
231makeScale : V3 -> M4x4
202makeScale : V3 -> M4x4
232makeScale = Native.MJS.m4x4makeScale
203makeScale = Native.MJS.m4x4makeScale
233
204
234−{-|
235−Concatenates a scaling to the given matrix.
205+{-| Concatenates a scaling to the given matrix.
236-}
206-}
237scaleM3 : Float -> Float -> Float -> M4x4 -> M4x4
207scaleM3 : Float -> Float -> Float -> M4x4 -> M4x4
238scaleM3 = Native.MJS.m4x4scale3
208scaleM3 = Native.MJS.m4x4scale3
239
209
240−{-|
241−Concatenates a scaling to the given matrix.
210+{-| Concatenates a scaling to the given matrix.
242-}
211-}
243scaleM : V3 -> M4x4 -> M4x4
212scaleM : V3 -> M4x4 -> M4x4
244scaleM = Native.MJS.m4x4scale
213scaleM = Native.MJS.m4x4scale
@@ -250,9 +219,8 @@ each of the x, y, and z directions.
250makeTranslate3 : Float -> Float -> Float -> M4x4
219makeTranslate3 : Float -> Float -> Float -> M4x4
251makeTranslate3 = Native.MJS.m4x4makeTranslate3
220makeTranslate3 = Native.MJS.m4x4makeTranslate3
252
221
253−{-|
254−Creates a transformation matrix for translating each of the x, y, and z axes by the amount
222+{-| Creates a transformation matrix for translating each of the x, y, and z
255−given in the corresponding element of the 3-element vector.
223+axes by the amount given in the corresponding element of the 3-element vector.
256-}
224-}
257makeTranslate : V3 -> M4x4
225makeTranslate : V3 -> M4x4
258makeTranslate = Native.MJS.m4x4makeTranslate
226makeTranslate = Native.MJS.m4x4makeTranslate
@@ -273,7 +241,7 @@ translate = Native.MJS.m4x4translate
273Creates a transformation matrix for a camera.
241Creates a transformation matrix for a camera.
274
242
275Parameters:
243Parameters:
276−
244+
277 * eye - The location of the camera
245 * eye - The location of the camera
278 * center - The location of the focused object
246 * center - The location of the focused object
279 * up - The "up" direction according to the camera
247 * up - The "up" direction according to the camera
@@ -281,14 +249,13 @@ Parameters:
281makeLookAt : V3 -> V3 -> V3 -> M4x4
249makeLookAt : V3 -> V3 -> V3 -> M4x4
282makeLookAt = Native.MJS.m4x4makeLookAt
250makeLookAt = Native.MJS.m4x4makeLookAt
283
251
284−{-|
285−"Flip" the matrix across the diagonal by swapping row index and column index.
252+{-| "Flip" the matrix across the diagonal by swapping row index and column
253+index.
286-}
254-}
287transpose : M4x4 -> M4x4
255transpose : M4x4 -> M4x4
288transpose = Native.MJS.m4x4transpose
256transpose = Native.MJS.m4x4transpose
289
257
290−{-|
291−Creates a transform from a basis consisting of 3 linearly independent vectors.
258+{-| Creates a transform from a basis consisting of 3 linearly independent vectors.
292-}
259-}
293makeBasis : V3 -> V3 -> V3 -> M4x4
260makeBasis : V3 -> V3 -> V3 -> M4x4
294makeBasis = Native.MJS.m4x4makeBasis
261makeBasis = Native.MJS.m4x4makeBasis