Skip to content
This repository
Browse code

mat2.multiply, mat3.multiplyVec2

  • Loading branch information...
commit f783ba2a0df52790d9b34e67edb123008a464c5d 1 parent 99a6ced
Colin MacKenzie IV authored
39  gl-matrix.js
@@ -529,6 +529,23 @@
529 529
     };
530 530
 
531 531
     /**
  532
+     * Transforms the vec2 according to the given 3x3 matrix.
  533
+     *
  534
+     * @param {mat3} matrix the 3x3 matrix to multiply against
  535
+     * @param {vec2} vec    the vector to multiply
  536
+     * @param {vec2} [dest] an optional receiving vector. If not given, vec is used.
  537
+     *
  538
+     * @returns {vec2} The multiplication result
  539
+     **/
  540
+    mat3.multiplyVec2 = function(matrix, vec, dest) {
  541
+      if (!dest) dest = vec;
  542
+      var x = vec[0], y = vec[1];
  543
+      dest[0] = x * matrix[0] + y * matrix[3] + matrix[6];
  544
+      dest[1] = x * matrix[1] + y * matrix[4] + matrix[7];
  545
+      return dest;
  546
+    };
  547
+
  548
+    /**
532 549
      * Transforms the vec3 according to this rotation matrix.
533 550
      *
534 551
      * @param {mat3} matrix the 3x3 matrix to multiply against
@@ -2643,6 +2660,28 @@
2643 2660
         return dest;
2644 2661
     }
2645 2662
     
  2663
+    /**
  2664
+     * Performs a matrix multiplication
  2665
+     *
  2666
+     * @param {mat2} matA First operand
  2667
+     * @param {mat2} matB Second operand
  2668
+     * @param {mat2} [dest] mat2 receiving operation result. If not specified result is written to matA
  2669
+     *
  2670
+     * @returns {mat2} dest if specified, matA otherwise
  2671
+     */
  2672
+    mat2.multiply = function (matA, matB, dest) {
  2673
+        if (!dest) { dest = matA; }
  2674
+        var a11 = matA[0],
  2675
+            a12 = matA[1],
  2676
+            a21 = matA[2],
  2677
+            a22 = matA[3];
  2678
+        dest[0] = a11 * matB[0] + a12 * matB[2];
  2679
+        dest[1] = a11 * matB[1] + a12 * matB[3];
  2680
+        dest[2] = a21 * matB[0] + a22 * matB[2];
  2681
+        dest[3] = a21 * matB[1] + a22 * matB[3];
  2682
+        return dest;
  2683
+    }
  2684
+
2646 2685
     /*
2647 2686
      * Exports
2648 2687
      */
17  spec/javascripts/mat2_spec.js
@@ -85,4 +85,21 @@ describe("mat2", function() {
85 85
       it("should return a", function() { expect(result).toBe(a); });
86 86
     });
87 87
   });
  88
+  
  89
+  describe("multiply", function() {
  90
+    describe("with dest", function() {
  91
+      beforeEach(function() { result = mat2.multiply(a, b, dest); });
  92
+      it("should set dest", function() { expect(dest).toBeEqualish([19, 22, 43, 50]); });
  93
+      it("should return dest", function() { expect(result).toBe(dest); });
  94
+      it("should not modify a", function() { expect(a).toBeEqualish([1, 2, 3, 4]); });
  95
+      it("should not modify b", function() { expect(b).toBeEqualish([5, 6, 7, 8]); });
  96
+    });
  97
+    
  98
+    describe("without dest", function() {
  99
+      beforeEach(function() { result = mat2.multiply(a, b); });
  100
+      it("should set a", function() { expect(a).toBeEqualish([19, 22, 43, 50]); });
  101
+      it("should not change b", function() { expect(b).toBeEqualish([5, 6, 7, 8]); });
  102
+      it("should return a", function() { expect(result).toBe(a); });
  103
+    });
  104
+  });
88 105
 });
22  spec/javascripts/mat3_spec.js
... ...
@@ -1,11 +1,29 @@
1 1
 describe("mat3", function() {
2  
-  var mat, vec, dest;
  2
+  var mat, vec, dest, result;
3 3
   
4 4
   beforeEach(function() {
  5
+    mat = mat3.create([-1, 0, 1, 0, -1, 0, 1, 0, -1]);
5 6
     vec = vec3.create([1, 2, 3]);
6 7
     dest = vec3.create();
7 8
   });
8 9
   
  10
+  describe("multiplyVec2", function() {
  11
+    beforeEach(function() { vec = [1, 2]; dest = vec2.create() });
  12
+    
  13
+    describe("with dest", function() {
  14
+      beforeEach(function() { result = mat3.multiplyVec2(mat, vec, dest); });
  15
+      it("should return dest", function() { expect(result).toBe(dest); });
  16
+      it("should not alter vec", function() { expect(vec).toBeEqualish([1, 2]); });
  17
+      it("should set dest", function() { expect(dest).toBeEqualish([0, -2]); });
  18
+    });
  19
+
  20
+    describe("without dest", function() {
  21
+      beforeEach(function() { result = mat3.multiplyVec2(mat, vec); });
  22
+      it("should return vec", function() { expect(result).toBe(vec); });
  23
+      it("should set vec", function() { expect(vec).toBeEqualish([0, -2]); });
  24
+    });
  25
+  });
  26
+  
9 27
   describe("multiplyVec3", function() {
10 28
     describe("when set to identity", function() {
11 29
       beforeEach(function() { mat = mat3.identity(mat3.create()); });
@@ -16,8 +34,6 @@ describe("mat3", function() {
16 34
     });
17 35
     
18 36
     describe("with an arbitrary mat3", function() {
19  
-      beforeEach(function() { mat = mat3.create([-1, 0, 1, 0, -1, 0, 1, 0, -1]); });
20  
-      
21 37
       describe("given a dest vec3", function() {
22 38
         it("should not modify incoming vec3", function() {
23 39
           mat3.multiplyVec3(mat, vec, vec3.create());

0 notes on commit f783ba2

Please sign in to comment.
Something went wrong with that request. Please try again.