on master help sign in
Split Vec2, 3, and 4 out into their own libraries
commitdbfc352bcfdc3e0f3c9787570491a4e95bd5d87d authorEvan Czaplicki <info@elm-lang.org> dateTue 6 May 2014, 11:39 (13 years ago) parentbf13e7d
9 files changed+797−133
compiler/SourceSyntax/Literal.hs+3−3 libraries/Math/Matrix.elm → libraries/Math/Matrix4.elm+1−1 libraries/Math/Vector.elm−126 libraries/Math/Vector2.elm+104 libraries/Math/Vector3.elm+128 libraries/Math/Vector4.elm+120 libraries/Native/MJS.js+31−3 libraries/Native/Math/Vector2.js+186 libraries/Native/Math/Vector4.js+224
compiler/SourceSyntax/Literal.hs+3−3
@@ -29,9 +29,9 @@ glTipeName glTipe =
2929 case glTipe of
3030 Int -> "Int"
3131 Float -> "Float"
32− V2 -> "Math.Vector.Vec2"
33− V3 -> "Math.Vector.Vec3"
34− V4 -> "Math.Vector.Vec4"
32+ V2 -> "Math.Vector2.Vec2"
33+ V3 -> "Math.Vector3.Vec3"
34+ V4 -> "Math.Vector4.Vec4"
3535 M4 -> "Math.Matrix.Mat4"
3636 Texture -> "Graphics.WebGL.Texture"
3737
libraries/Math/Matrix.elm → libraries/Math/Matrix4.elmrenamed+1−1
@@ -24,7 +24,7 @@ existing matrix.
2424-}
2525
2626import Native.MJS
27−import Math.Vector (Vec3)
27+import Math.Vector3 (Vec3)
2828
2929data Mat4 = Mat4
3030
libraries/Math/Vector.elmdeleted−126
@@ -1,126 +0,0 @@
1−module Math.Vector where
2−
3−{-| A high performance linear algebra library using native JS arrays. Geared
4−towards 3D graphics and use with `Graphics.WebGL`.
5−
6−All vectors are immutable.
7−
8−# Create
9−@docs v3, i, j, k
10−
11−# Get and Set
12−The set functions create a new copy of the vector, updating a single field.
13−
14−@docs getX, getY, getZ, setX, setY, setZ, toTuple, toRecord
15−
16−# Operations
17−@docs add, sub, neg, direction, length, lengthSquared, normalize, scale, dot, cross
18−
19−-}
20−
21−import Native.MJS
22−
23−data Vec2 = Vec2
24−data Vec3 = Vec3
25−data Vec4 = Vec4
26−
27−{-| Creates a new 3-element vector with the given values.
28−-}
29−v3 : Float -> Float -> Float -> Vec3
30−v3 = Native.MJS.v3
31−
32−{-| The unit vector &icirc; which points in the x direction: `v3 1 0 0` -}
33−i : Vec3
34−i = Native.MJS.v3 1 0 0
35−
36−{-| The unit vector &jcirc; which points in the y direction: `v3 0 1 0` -}
37−j : Vec3
38−j = Native.MJS.v3 0 1 0
39−
40−{-| The unit vector &kcirc; which points in the z direction: `v3 0 0 1` -}
41−k : Vec3
42−k = Native.MJS.v3 0 0 1
43−
44−{-| Extract the x component of a vector. -}
45−getX : Vec3 -> Float
46−getX = Native.MJS.getX
47−
48−{-| Extract the y component of a vector. -}
49−getY : Vec3 -> Float
50−getY = Native.MJS.getY
51−
52−{-| Extract the z component of a vector. -}
53−getZ : Vec3 -> Float
54−getZ = Native.MJS.getZ
55−
56−{-| Update the x component of a vector. -}
57−setX : Vec3 -> Float
58−setX = Native.MJS.setX
59−
60−{-| Update the y component of a vector. -}
61−setY : Vec3 -> Float
62−setY = Native.MJS.setY
63−
64−{-| Update the z component of a vector. -}
65−setZ : Vec3 -> Float
66−setZ = Native.MJS.setZ
67−
68−{-| Convert a vector to a tuple.
69−-}
70−toTuple : Vec3 -> (Float,Float,Float)
71−toTuple = Native.MJS.toTuple3
72−
73−{-| Convert a vector to a record.
74−-}
75−toRecord : Vec3 -> { x:Float, y:Float, z:Float }
76−toRecord = Native.MJS.toRecord3
77−
78−{-| Vector addition: a + b
79−-}
80−add : Vec3 -> Vec3 -> Vec3
81−add = Native.MJS.v3add
82−
83−{-| Vector subtraction: a - b
84−-}
85−sub : Vec3 -> Vec3 -> Vec3
86−sub = Native.MJS.v3sub
87−
88−{-| Vector negation: -a
89−-}
90−neg : Vec3 -> Vec3
91−neg = Native.MJS.v3neg
92−
93−{-| The normalized direction from a to b: (a - b) / |a - b|
94−-}
95−direction : Vec3 -> Vec3 -> Vec3
96−direction = Native.MJS.v3direction
97−
98−{-| The length of the given vector: |a|
99−-}
100−length : Vec3 -> Float
101−length = Native.MJS.v3length
102−
103−{-| The square of the length of the given vector: |a| * |a|
104−-}
105−lengthSquared : Vec3 -> Float
106−lengthSquared = Native.MJS.v3lengthSquared
107−
108−{-| A unit vector with the same direction as the given vector: a / |a|
109−-}
110−normalize : Vec3 -> Vec3
111−normalize = Native.MJS.v3normalize
112−
113−{-| Multiply the vector by a scalar: a * k
114−-}
115−scale : Vec3 -> Float -> Vec3
116−scale = Native.MJS.v3scale
117−
118−{-| The dot product of a and b
119−-}
120−dot : Vec3 -> Vec3 -> Float
121−dot = Native.MJS.v3dot
122−
123−{-| The cross product of a and b
124−-}
125−cross : Vec3 -> Vec3 -> Vec3
126−cross = Native.MJS.v3cross
libraries/Math/Vector2.elmnew file+104
@@ -0,0 +1,104 @@
1+module Math.Vector2 where
2+{-| A high performance linear algebra library using native JS arrays. Geared
3+towards 3D graphics and use with `Graphics.WebGL`. All vectors are immutable.
4+
5+# Create
6+@docs v2
7+
8+# Get and Set
9+The set functions create a new copy of the vector, updating a single field.
10+
11+@docs getX, getY, setX, setY
12+
13+# Operations
14+@docs add, sub, negate, scale, dot, normalize, direction,
15+ length, lengthSquared, distance, distanceSquared
16+
17+# Conversions
18+toTuple, fromTuple, toRecord, fromRecord
19+-}
20+
21+import Native.Math.Vector2
22+
23+data Vec2 = Vec2
24+
25+{-| Creates a new 2-element vector with the given values. -}
26+v2 : Float -> Float -> Vec2
27+v2 = Native.Math.Vector2.v2
28+
29+{-| Extract the x component of a vector. -}
30+getX : Vec2 -> Float
31+getX = Native.Math.Vector2.getX
32+
33+{-| Extract the y component of a vector. -}
34+getY : Vec2 -> Float
35+getY = Native.Math.Vector2.getY
36+
37+{-| Update the x component of a vector, returning a new vector. -}
38+setX : Float -> Vec2 -> Vec2
39+setX = Native.Math.Vector2.setX
40+
41+{-| Update the y component of a vector, returning a new vector. -}
42+setY : Float -> Vec2 -> Vec2
43+setY = Native.Math.Vector2.setY
44+
45+{-| Convert a vector to a tuple. -}
46+toTuple : Vec2 -> (Float,Float)
47+toTuple = Native.Math.Vector2.toTuple
48+
49+{-| Convert a vector to a record. -}
50+toRecord : Vec2 -> { x:Float, y:Float }
51+toRecord = Native.Math.Vector2.toRecord
52+
53+{-| Convert a tuple to a vector. -}
54+fromTuple : (Float,Float) -> Vec2
55+fromTuple = Native.Math.Vector2.fromTuple
56+
57+{-| Convert a record to a vector. -}
58+fromRecord : { x:Float, y:Float } -> Vec2
59+fromRecord = Native.Math.Vector2.fromRecord
60+
61+{-| Vector addition: a + b -}
62+add : Vec2 -> Vec2 -> Vec2
63+add = Native.Math.Vector2.add
64+
65+{-| Vector subtraction: a - b -}
66+sub : Vec2 -> Vec2 -> Vec2
67+sub = Native.Math.Vector2.sub
68+
69+{-| Vector negation: -a -}
70+negate : Vec2 -> Vec2
71+negate = Native.Math.Vector2.neg
72+
73+{-| The normalized direction from a to b: (a - b) / |a - b| -}
74+direction : Vec2 -> Vec2 -> Vec2
75+direction = Native.Math.Vector2.direction
76+
77+{-| The length of the given vector: |a| -}
78+length : Vec2 -> Float
79+length = Native.Math.Vector2.length
80+
81+{-| The square of the length of the given vector: |a| * |a| -}
82+lengthSquared : Vec2 -> Float
83+lengthSquared = Native.Math.Vector2.lengthSquared
84+
85+{-| The distance between two vectors. -}
86+distance : Vec2 -> Vec2 -> Float
87+distance = Native.Math.Vector2.distance
88+
89+{-| The square of the distance between two vectors. -}
90+distanceSquared : Vec2 -> Vec2 -> Float
91+distanceSquared = Native.Math.Vector2.distanceSquared
92+
93+{-| A unit vector with the same direction as the given vector: a / |a| -}
94+normalize : Vec2 -> Vec2
95+normalize = Native.Math.Vector2.normalize
96+
97+{-| Multiply the vector by a scalar: a * k -}
98+scale : Vec2 -> Float -> Vec2
99+scale = Native.Math.Vector2.scale
100+
101+{-| The dot product of a and b -}
102+dot : Vec2 -> Vec2 -> Float
103+dot = Native.Math.Vector2.dot
104+
libraries/Math/Vector3.elmnew file+128
@@ -0,0 +1,128 @@
1+module Math.Vector3 where
2+{-| A high performance linear algebra library using native JS arrays. Geared
3+towards 3D graphics and use with `Graphics.WebGL`. All vectors are immutable.
4+
5+# Create
6+@docs v3, i, j, k
7+
8+# Get and Set
9+The set functions create a new copy of the vector, updating a single field.
10+
11+@docs getX, getY, getZ, setX, setY, setZ
12+
13+# Operations
14+@docs add, sub, negate, scale, dot, cross, normalize, direction,
15+ length, lengthSquared, distance, distanceSquared
16+
17+# Conversions
18+@docs toTuple, fromTuple, toRecord, fromRecord
19+-}
20+
21+import Native.MJS
22+
23+data Vec3 = Vec3
24+
25+{-| Creates a new 3-element vector with the given values.
26+-}
27+v3 : Float -> Float -> Float -> Vec3
28+v3 = Native.MJS.v3
29+
30+{-| The unit vector &icirc; which points in the x direction: `v3 1 0 0` -}
31+i : Vec3
32+i = Native.MJS.v3 1 0 0
33+
34+{-| The unit vector &jcirc; which points in the y direction: `v3 0 1 0` -}
35+j : Vec3
36+j = Native.MJS.v3 0 1 0
37+
38+{-| The unit vector &kcirc; which points in the z direction: `v3 0 0 1` -}
39+k : Vec3
40+k = Native.MJS.v3 0 0 1
41+
42+{-| Extract the x component of a vector. -}
43+getX : Vec3 -> Float
44+getX = Native.MJS.v3getX
45+
46+{-| Extract the y component of a vector. -}
47+getY : Vec3 -> Float
48+getY = Native.MJS.v3getY
49+
50+{-| Extract the z component of a vector. -}
51+getZ : Vec3 -> Float
52+getZ = Native.MJS.v3getZ
53+
54+{-| Update the x component of a vector, returning a new vector. -}
55+setX : Float -> Vec3 -> Vec3
56+setX = Native.MJS.v3setX
57+
58+{-| Update the y component of a vector, returning a new vector. -}
59+setY : Float -> Vec3 -> Vec3
60+setY = Native.MJS.v3setY
61+
62+{-| Update the z component of a vector, returning a new vector. -}
63+setZ : Float -> Vec3 -> Vec3
64+setZ = Native.MJS.v3setZ
65+
66+{-| Convert a vector to a tuple. -}
67+toTuple : Vec3 -> (Float,Float,Float)
68+toTuple = Native.MJS.toTuple3
69+
70+{-| Convert a vector to a record. -}
71+toRecord : Vec3 -> { x:Float, y:Float, z:Float }
72+toRecord = Native.MJS.toRecord3
73+
74+{-| Convert a tuple to a vector. -}
75+fromTuple : (Float,Float,Float) -> Vec3
76+fromTuple = Native.MJS.fromTuple3
77+
78+{-| Convert a record to a vector. -}
79+fromRecord : { x:Float, y:Float, z:Float } -> Vec3
80+fromRecord = Native.MJS.fromRecord3
81+
82+{-| Vector addition: a + b -}
83+add : Vec3 -> Vec3 -> Vec3
84+add = Native.MJS.v3add
85+
86+{-| Vector subtraction: a - b -}
87+sub : Vec3 -> Vec3 -> Vec3
88+sub = Native.MJS.v3sub
89+
90+{-| Vector negation: -a -}
91+negate : Vec3 -> Vec3
92+negate = Native.MJS.v3neg
93+
94+{-| The normalized direction from a to b: (a - b) / |a - b| -}
95+direction : Vec3 -> Vec3 -> Vec3
96+direction = Native.MJS.v3direction
97+
98+{-| The length of the given vector: |a| -}
99+length : Vec3 -> Float
100+length = Native.MJS.v3length
101+
102+{-| The square of the length of the given vector: |a| * |a| -}
103+lengthSquared : Vec3 -> Float
104+lengthSquared = Native.MJS.v3lengthSquared
105+
106+{-| The distance between two vectors. -}
107+distance : Vec3 -> Vec3 -> Float
108+distance = Native.MJS.v3distance
109+
110+{-| The square of the distance between two vectors. -}
111+distanceSquared : Vec3 -> Vec3 -> Float
112+distanceSquared = Native.MJS.v3distanceSquared
113+
114+{-| A unit vector with the same direction as the given vector: a / |a| -}
115+normalize : Vec3 -> Vec3
116+normalize = Native.MJS.v3normalize
117+
118+{-| Multiply the vector by a scalar: a * k -}
119+scale : Vec3 -> Float -> Vec3
120+scale = Native.MJS.v3scale
121+
122+{-| The dot product of a and b -}
123+dot : Vec3 -> Vec3 -> Float
124+dot = Native.MJS.v3dot
125+
126+{-| The cross product of a and b -}
127+cross : Vec3 -> Vec3 -> Vec3
128+cross = Native.MJS.v3cross
libraries/Math/Vector4.elmnew file+120
@@ -0,0 +1,120 @@
1+module Math.Vector4 where
2+{-| A high performance linear algebra library using native JS arrays. Geared
3+towards 3D graphics and use with `Graphics.WebGL`. All vectors are immutable.
4+
5+# Create
6+@docs v4
7+
8+# Get and Set
9+The set functions create a new copy of the vector, updating a single field.
10+
11+@docs getX, getY, setX, setY
12+
13+# Operations
14+@docs add, sub, negate, scale, dot, normalize, direction,
15+ length, lengthSquared, distance, distanceSquared
16+
17+# Conversions
18+toTuple, fromTuple, toRecord, fromRecord
19+-}
20+
21+import Native.Math.Vector4
22+
23+data Vec4 = Vec4
24+
25+{-| Creates a new 4-element vector with the given x, y, z, and w values. -}
26+v4 : Float -> Float -> Float -> Float -> Vec4
27+v4 = Native.Math.Vector4.v4
28+
29+{-| Extract the x component of a vector. -}
30+getX : Vec4 -> Float
31+getX = Native.Math.Vector4.getX
32+
33+{-| Extract the y component of a vector. -}
34+getY : Vec4 -> Float
35+getY = Native.Math.Vector4.getY
36+
37+{-| Extract the z component of a vector. -}
38+getZ : Vec4 -> Float
39+getZ = Native.Math.Vector4.getZ
40+
41+{-| Extract the w component of a vector. -}
42+getW : Vec4 -> Float
43+getW = Native.Math.Vector4.getW
44+
45+{-| Update the x component of a vector, returning a new vector. -}
46+setX : Float -> Vec4 -> Vec4
47+setX = Native.Math.Vector4.setX
48+
49+{-| Update the y component of a vector, returning a new vector. -}
50+setY : Float -> Vec4 -> Vec4
51+setY = Native.Math.Vector4.setY
52+
53+{-| Update the z component of a vector, returning a new vector. -}
54+setZ : Float -> Vec4 -> Vec4
55+setZ = Native.Math.Vector4.setZ
56+
57+{-| Update the w component of a vector, returning a new vector. -}
58+setW : Float -> Vec4 -> Vec4
59+setW = Native.Math.Vector4.setW
60+
61+{-| Convert a vector to a tuple. -}
62+toTuple : Vec4 -> (Float,Float,Float,Float)
63+toTuple = Native.Math.Vector4.toTuple
64+
65+{-| Convert a vector to a record. -}
66+toRecord : Vec4 -> { x:Float, y:Float, z:Float, w:Float }
67+toRecord = Native.Math.Vector4.toRecord
68+
69+{-| Convert a tuple to a vector. -}
70+fromTuple : (Float,Float,Float,Float) -> Vec4
71+fromTuple = Native.Math.Vector4.fromTuple
72+
73+{-| Convert a record to a vector. -}
74+fromRecord : { x:Float, y:Float, z:Float, w:Float } -> Vec4
75+fromRecord = Native.Math.Vector4.fromRecord
76+
77+{-| Vector addition: a + b -}
78+add : Vec4 -> Vec4 -> Vec4
79+add = Native.Math.Vector4.add
80+
81+{-| Vector subtraction: a - b -}
82+sub : Vec4 -> Vec4 -> Vec4
83+sub = Native.Math.Vector4.sub
84+
85+{-| Vector negation: -a -}
86+negate : Vec4 -> Vec4
87+negate = Native.Math.Vector4.neg
88+
89+{-| The normalized direction from a to b: (a - b) / |a - b| -}
90+direction : Vec4 -> Vec4 -> Vec4
91+direction = Native.Math.Vector4.direction
92+
93+{-| The length of the given vector: |a| -}
94+length : Vec4 -> Float
95+length = Native.Math.Vector4.length
96+
97+{-| The square of the length of the given vector: |a| * |a| -}
98+lengthSquared : Vec4 -> Float
99+lengthSquared = Native.Math.Vector4.lengthSquared
100+
101+{-| The distance between two vectors. -}
102+distance : Vec4 -> Vec4 -> Float
103+distance = Native.Math.Vector4.distance
104+
105+{-| The square of the distance between two vectors. -}
106+distanceSquared : Vec4 -> Vec4 -> Float
107+distanceSquared = Native.Math.Vector4.distanceSquared
108+
109+{-| A unit vector with the same direction as the given vector: a / |a| -}
110+normalize : Vec4 -> Vec4
111+normalize = Native.Math.Vector4.normalize
112+
113+{-| Multiply the vector by a scalar: a * k -}
114+scale : Vec4 -> Float -> Vec4
115+scale = Native.Math.Vector4.scale
116+
117+{-| The dot product of a and b -}
118+dot : Vec4 -> Vec4 -> Float
119+dot = Native.Math.Vector4.dot
120+
libraries/Native/MJS.js+31−3
@@ -141,6 +141,15 @@ Elm.Native.MJS.make = function(elm) {
141141 V3.getZ = function V3_getZ(a) {
142142 return a[2];
143143 }
144+ V3.setX = function V3_setX(x, a) {
145+ return new MJS_FLOAT_ARRAY_TYPE(x, a[1], a[2]);
146+ }
147+ V3.setY = function V3_setY(y, a) {
148+ return new MJS_FLOAT_ARRAY_TYPE(a[0], y, a[2]);
149+ }
150+ V3.setZ = function V3_setZ(z, a) {
151+ return new MJS_FLOAT_ARRAY_TYPE(a[0], a[1], z);
152+ }
144153
145154 V3.toTuple3 = function V3_toTuple3(a) {
146155 return {
@@ -319,6 +328,20 @@ Elm.Native.MJS.make = function(elm) {
319328 return a[0]*a[0] + a[1]*a[1] + a[2]*a[2];
320329 };
321330
331+ V3.distance = function V3_distance(a, b) {
332+ var dx = a[0] - b[0];
333+ var dy = a[1] - b[1];
334+ var dz = a[2] - b[2];
335+ return Math.sqrt(dx * dx + dy * dy + dz * dz);
336+ };
337+
338+ V3.distanceSquared = function V3_distanceSquared(a) {
339+ var dx = a[0] - b[0];
340+ var dy = a[1] - b[1];
341+ var dz = a[2] - b[2];
342+ return dx * dx + dy * dy + dz * dz;
343+ };
344+
322345 /*
323346 * Function: V3.normalize
324347 *
@@ -1712,9 +1735,12 @@ Elm.Native.MJS.make = function(elm) {
17121735 return {
17131736 V3: V3,
17141737 v3: F3(V3.$),
1715− getX: V3.getX,
1716− getY: V3.getY,
1717− getZ: V3.getZ,
1738+ v3getX: V3.getX,
1739+ v3getY: V3.getY,
1740+ v3getZ: V3.getZ,
1741+ v3setX: F2(V3.setX),
1742+ v3setY: F2(V3.setY),
1743+ v3setZ: F2(V3.setZ),
17181744 toTuple3: V3.toTuple3,
17191745 toRecord3: V3.toRecord3,
17201746 fromTuple3: V3.fromTuple3,
@@ -1725,6 +1751,8 @@ Elm.Native.MJS.make = function(elm) {
17251751 v3direction: F2(V3.direction),
17261752 v3length: V3.length,
17271753 v3lengthSquared: V3.lengthSquared,
1754+ v3distance: F2(V3.distance),
1755+ v3distanceSquared: F2(V3.distanceSquared),
17281756 v3normalize: V3.normalize,
17291757 v3scale: F2(V3.scale),
17301758 v3dot: F2(V3.dot),
libraries/Native/Math/Vector2.jsnew file+186
@@ -0,0 +1,186 @@
1+
2+/*
3+ * Copyright (c) 2010 Mozilla Corporation
4+ * Copyright (c) 2010 Vladimir Vukicevic
5+ *
6+ * Permission is hereby granted, free of charge, to any person
7+ * obtaining a copy of this software and associated documentation
8+ * files (the "Software"), to deal in the Software without
9+ * restriction, including without limitation the rights to use,
10+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
11+ * copies of the Software, and to permit persons to whom the
12+ * Software is furnished to do so, subject to the following
13+ * conditions:
14+ *
15+ * The above copyright notice and this permission notice shall be
16+ * included in all copies or substantial portions of the Software.
17+ *
18+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25+ * OTHER DEALINGS IN THE SOFTWARE.
26+ */
27+
28+/*
29+ * File: mjs
30+ *
31+ * Vector and Matrix math utilities for JavaScript, optimized for WebGL.
32+ * Edited to work with the Elm Programming Language
33+ */
34+
35+Elm.Native.Math = Elm.Native.Math || {};
36+Elm.Native.Math.Vector2 = {};
37+Elm.Native.Math.Vector2.make = function(elm) {
38+
39+ elm.Native = elm.Native || {};
40+ elm.Native.Math = elm.Native.Math || {};
41+ elm.Native.Math.Vector2 = elm.Native.Math.Vector2 || {};
42+ if (elm.Native.Math.Vector2.values) return elm.Native.Math.Vector2.values;
43+
44+ var MJS_FLOAT_ARRAY_TYPE = Float32Array;
45+
46+ var V2 = { };
47+
48+ if (MJS_FLOAT_ARRAY_TYPE == Array) {
49+ V2.$ = function V2_$(x, y) {
50+ return [x, y];
51+ };
52+ } else {
53+ V2.$ = function V2_$(x, y) {
54+ return new MJS_FLOAT_ARRAY_TYPE([x, y]);
55+ };
56+ }
57+
58+ V2.getX = function V2_getX(a) {
59+ return a[0];
60+ }
61+ V2.getY = function V2_getY(a) {
62+ return a[1];
63+ }
64+ V2.setX = function V2_setX(x, a) {
65+ return new MJS_FLOAT_ARRAY_TYPE(x, a[1]);
66+ }
67+ V2.setY = function V2_setY(y, a) {
68+ return new MJS_FLOAT_ARRAY_TYPE(a[0], y);
69+ }
70+
71+ V2.toTuple = function V2_toTuple(a) {
72+ return {
73+ ctor:"_Tuple2",
74+ _0:a[0],
75+ _1:a[1]
76+ };
77+ };
78+ V2.fromTuple = function V2_fromTuple(t) {
79+ return new MJS_FLOAT_ARRAY_TYPE([t._0, t._1]);
80+ };
81+
82+ V2.toRecord = function V2_toRecord(a) {
83+ return {
84+ _:{},
85+ x:a[0],
86+ y:a[1]
87+ };
88+ };
89+ V2.fromRecord = function V2_fromRecord(r) {
90+ return new MJS_FLOAT_ARRAY_TYPE([r.x, r.y]);
91+ };
92+
93+ V2.add = function V2_add(a, b) {
94+ var r = new MJS_FLOAT_ARRAY_TYPE(2);
95+ r[0] = a[0] + b[0];
96+ r[1] = a[1] + b[1];
97+ return r;
98+ };
99+
100+ V2.sub = function V2_sub(a, b) {
101+ var r = new MJS_FLOAT_ARRAY_TYPE(2);
102+ r[0] = a[0] - b[0];
103+ r[1] = a[1] - b[1];
104+ return r;
105+ };
106+
107+ V2.neg = function V2_neg(a) {
108+ var r = new MJS_FLOAT_ARRAY_TYPE(2);
109+ r[0] = - a[0];
110+ r[1] = - a[1];
111+ return r;
112+ };
113+
114+ V2.direction = function V2_direction(a, b) {
115+ var r = new MJS_FLOAT_ARRAY_TYPE(2);
116+ r[0] = a[0] - b[0];
117+ r[1] = a[1] - b[1];
118+ var im = 1.0 / V2.length(r);
119+ r[0] = r[0] * im;
120+ r[1] = r[1] * im;
121+ return r;
122+ };
123+
124+ V2.length = function V2_length(a) {
125+ return Math.sqrt(a[0]*a[0] + a[1]*a[1]);
126+ };
127+
128+ V2.lengthSquared = function V2_lengthSquared(a) {
129+ return a[0]*a[0] + a[1]*a[1];
130+ };
131+
132+ V2.distance = function V2_distance(a, b) {
133+ var dx = a[0] - b[0];
134+ var dy = a[1] - b[1];
135+ return Math.sqrt(dx * dx + dy * dy);
136+ };
137+
138+ V2.distanceSquared = function V2_distanceSquared(a) {
139+ var dx = a[0] - b[0];
140+ var dy = a[1] - b[1];
141+ return dx * dx + dy * dy;
142+ };
143+
144+ V2.normalize = function V2_normalize(a) {
145+ var r = new MJS_FLOAT_ARRAY_TYPE(2);
146+ var im = 1.0 / V2.length(a);
147+ r[0] = a[0] * im;
148+ r[1] = a[1] * im;
149+ return r;
150+ };
151+
152+ V2.scale = function V2_scale(a, k) {
153+ var r = new MJS_FLOAT_ARRAY_TYPE(2);
154+ r[0] = a[0] * k;
155+ r[1] = a[1] * k;
156+ return r;
157+ };
158+
159+ V2.dot = function V2_dot(a, b) {
160+ return a[0] * b[0] + a[1] * b[1];
161+ };
162+
163+ return {
164+ v2: F2(V2.$),
165+ getX: V2.getX,
166+ getY: V2.getY,
167+ setX: F2(V2.setX),
168+ setY: F2(V2.setY),
169+ toTuple: V2.toTuple,
170+ toRecord: V2.toRecord,
171+ fromTuple: V2.fromTuple,
172+ fromRecord: V2.fromRecord,
173+ add: F2(V2.add),
174+ sub: F2(V2.sub),
175+ neg: V2.neg,
176+ direction: F2(V2.direction),
177+ length: V2.length,
178+ lengthSquared: V2.lengthSquared,
179+ distance: F2(V2.distance),
180+ distanceSquared: F2(V2.distanceSquared),
181+ normalize: V2.normalize,
182+ scale: F2(V2.scale),
183+ dot: F2(V2.dot),
184+ };
185+
186+}
libraries/Native/Math/Vector4.jsnew file+224
@@ -0,0 +1,224 @@
1+
2+/*
3+ * Copyright (c) 2010 Mozilla Corporation
4+ * Copyright (c) 2010 Vladimir Vukicevic
5+ *
6+ * Permission is hereby granted, free of charge, to any person
7+ * obtaining a copy of this software and associated documentation
8+ * files (the "Software"), to deal in the Software without
9+ * restriction, including without limitation the rights to use,
10+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
11+ * copies of the Software, and to permit persons to whom the
12+ * Software is furnished to do so, subject to the following
13+ * conditions:
14+ *
15+ * The above copyright notice and this permission notice shall be
16+ * included in all copies or substantial portions of the Software.
17+ *
18+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25+ * OTHER DEALINGS IN THE SOFTWARE.
26+ */
27+
28+/*
29+ * File: mjs
30+ *
31+ * Vector and Matrix math utilities for JavaScript, optimized for WebGL.
32+ * Edited to work with the Elm Programming Language
33+ */
34+
35+Elm.Native.Math = Elm.Native.Math || {};
36+Elm.Native.Math.Vector4 = {};
37+Elm.Native.Math.Vector4.make = function(elm) {
38+
39+ elm.Native = elm.Native || {};
40+ elm.Native.Math = elm.Native.Math || {};
41+ elm.Native.Math.Vector4 = elm.Native.Math.Vector4 || {};
42+ if (elm.Native.Math.Vector4.values) return elm.Native.Math.Vector4.values;
43+
44+ var MJS_FLOAT_ARRAY_TYPE = Float32Array;
45+
46+ var V4 = { };
47+
48+ if (MJS_FLOAT_ARRAY_TYPE == Array) {
49+ V4.$ = function V4_$(x, y, z, w) {
50+ return [x, y, z, w];
51+ };
52+ } else {
53+ V4.$ = function V4_$(x, y, z, w) {
54+ return new MJS_FLOAT_ARRAY_TYPE([x, y, z, w]);
55+ };
56+ }
57+
58+ V4.getX = function V4_getX(a) {
59+ return a[0];
60+ }
61+ V4.getY = function V4_getY(a) {
62+ return a[1];
63+ }
64+ V4.getZ = function V4_getZ(a) {
65+ return a[2];
66+ }
67+ V4.getW = function V4_getW(a) {
68+ return a[3];
69+ }
70+ V4.setX = function V4_setX(x, a) {
71+ return new MJS_FLOAT_ARRAY_TYPE(x, a[1], a[2], a[3]);
72+ }
73+ V4.setY = function V4_setY(y, a) {
74+ return new MJS_FLOAT_ARRAY_TYPE(a[0], y, a[2], a[3]);
75+ }
76+ V4.setZ = function V4_setZ(z, a) {
77+ return new MJS_FLOAT_ARRAY_TYPE(a[0], a[1], z, a[3]);
78+ }
79+ V4.setW = function V4_setW(w, a) {
80+ return new MJS_FLOAT_ARRAY_TYPE(a[0], a[1], a[2], w);
81+ }
82+
83+ V4.toTuple = function V4_toTuple(a) {
84+ return {
85+ ctor:"_Tuple4",
86+ _0:a[0],
87+ _1:a[1],
88+ _2:a[2],
89+ _3:a[3]
90+ };
91+ };
92+ V4.fromTuple = function V4_fromTuple(t) {
93+ return new MJS_FLOAT_ARRAY_TYPE([t._0, t._1, t._2, t._3]);
94+ };
95+
96+ V4.toRecord = function V4_toRecord(a) {
97+ return {
98+ _:{},
99+ x:a[0],
100+ y:a[1],
101+ z:a[2],
102+ w:a[3]
103+ };
104+ };
105+ V4.fromRecord = function V4_fromRecord(r) {
106+ return new MJS_FLOAT_ARRAY_TYPE([r.x, r.y, r.z, r.w]);
107+ };
108+
109+ V4.add = function V4_add(a, b) {
110+ var r = new MJS_FLOAT_ARRAY_TYPE(4);
111+ r[0] = a[0] + b[0];
112+ r[1] = a[1] + b[1];
113+ r[2] = a[2] + b[2];
114+ r[3] = a[3] + b[3];
115+ return r;
116+ };
117+
118+ V4.sub = function V4_sub(a, b) {
119+ var r = new MJS_FLOAT_ARRAY_TYPE(4);
120+ r[0] = a[0] - b[0];
121+ r[1] = a[1] - b[1];
122+ r[2] = a[2] - b[2];
123+ r[3] = a[3] - b[3];
124+ return r;
125+ };
126+
127+ V4.neg = function V4_neg(a) {
128+ var r = new MJS_FLOAT_ARRAY_TYPE(4);
129+ r[0] = - a[0];
130+ r[1] = - a[1];
131+ r[2] = - a[2];
132+ r[3] = - a[3];
133+ return r;
134+ };
135+
136+ V4.direction = function V4_direction(a, b) {
137+ var r = new MJS_FLOAT_ARRAY_TYPE(4);
138+ r[0] = a[0] - b[0];
139+ r[1] = a[1] - b[1];
140+ r[2] = a[2] - b[2];
141+ r[3] = a[3] - b[3];
142+ var im = 1.0 / V4.length(r);
143+ r[0] = r[0] * im;
144+ r[1] = r[1] * im;
145+ r[2] = r[2] * im;
146+ r[3] = r[3] * im;
147+ return r;
148+ };
149+
150+ V4.length = function V4_length(a) {
151+ return Math.sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2] + a[3]*a[3]);
152+ };
153+
154+ V4.lengthSquared = function V4_lengthSquared(a) {
155+ return a[0]*a[0] + a[1]*a[1] + a[2]*a[2] + a[3]*a[3];
156+ };
157+
158+ V4.distance = function V4_distance(a, b) {
159+ var dx = a[0] - b[0];
160+ var dy = a[1] - b[1];
161+ var dz = a[2] - b[2];
162+ var dw = a[3] - b[3];
163+ return Math.sqrt(dx * dx + dy * dy + dz * dz + dw * dw);
164+ };
165+
166+ V4.distanceSquared = function V4_distanceSquared(a) {
167+ var dx = a[0] - b[0];
168+ var dy = a[1] - b[1];
169+ var dz = a[2] - b[2];
170+ var dw = a[3] - b[3];
171+ return dx * dx + dy * dy + dz * dz + dw * dw;
172+ };
173+
174+ V4.normalize = function V4_normalize(a) {
175+ var r = new MJS_FLOAT_ARRAY_TYPE(4);
176+ var im = 1.0 / V4.length(a);
177+ r[0] = a[0] * im;
178+ r[1] = a[1] * im;
179+ r[2] = a[2] * im;
180+ r[3] = a[3] * im;
181+ return r;
182+ };
183+
184+ V4.scale = function V4_scale(a, k) {
185+ var r = new MJS_FLOAT_ARRAY_TYPE(4);
186+ r[0] = a[0] * k;
187+ r[1] = a[1] * k;
188+ r[2] = a[2] * k;
189+ r[3] = a[3] * k;
190+ return r;
191+ };
192+
193+ V4.dot = function V4_dot(a, b) {
194+ return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
195+ };
196+
197+ return {
198+ v4: F4(V4.$),
199+ getX: V4.getX,
200+ getY: V4.getY,
201+ getZ: V4.getZ,
202+ getW: V4.getW,
203+ setX: F2(V4.setX),
204+ setY: F2(V4.setY),
205+ setZ: F2(V4.setZ),
206+ setW: F2(V4.setW),
207+ toTuple: V4.toTuple,
208+ toRecord: V4.toRecord,
209+ fromTuple: V4.fromTuple,
210+ fromRecord: V4.fromRecord,
211+ add: F2(V4.add),
212+ sub: F2(V4.sub),
213+ neg: V4.neg,
214+ direction: F2(V4.direction),
215+ length: V4.length,
216+ lengthSquared: V4.lengthSquared,
217+ distance: F2(V4.distance),
218+ distanceSquared: F2(V4.distanceSquared),
219+ normalize: V4.normalize,
220+ scale: F2(V4.scale),
221+ dot: F2(V4.dot),
222+ };
223+
224+}