|
| 1 | +package vecmatlib.matrix |
| 2 | + |
| 3 | +import vecmatlib.vector.Vec2f |
| 4 | + |
| 5 | +/** |
| 6 | + * 2x2 float matrix. |
| 7 | + * |
| 8 | + * @param m00 Element 0 0 |
| 9 | + * @param m01 Element 0 1 |
| 10 | + * @param m10 Element 1 0 |
| 11 | + * @param m11 Element 1 1 |
| 12 | + */ |
| 13 | +case class Mat2f(m00: Float, m01: Float, m10: Float, m11: Float) extends MatFloat[Mat2f, Vec2f] { |
| 14 | + |
| 15 | + /** |
| 16 | + * Returns the sum between this matrix and the given one. |
| 17 | + * |
| 18 | + * @param m The matrix to add |
| 19 | + * @return The sum between this matrix and the given one |
| 20 | + */ |
| 21 | + override def +(m: Mat2f): Mat2f = Mat2f( |
| 22 | + this.m00 + m.m00, this.m01 + m.m01, |
| 23 | + this.m10 + m.m10, this.m11 + m.m11 |
| 24 | + ) |
| 25 | + |
| 26 | + /** |
| 27 | + * Returns the additive inverse of this matrix. |
| 28 | + * |
| 29 | + * @return The additive inverse of this matrix |
| 30 | + */ |
| 31 | + override def unary_- : Mat2f = Mat2f( |
| 32 | + -this.m00, -this.m01, |
| 33 | + -this.m10, -this.m11 |
| 34 | + ) |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the product of this matrix by the given scalar. |
| 38 | + * |
| 39 | + * @param k The scalar by which this matrix is multiplied |
| 40 | + * @return The product of this matrix by the given scalar |
| 41 | + */ |
| 42 | + override def *(k: Float): Mat2f = Mat2f( |
| 43 | + this.m00 * k, this.m01 * k, |
| 44 | + this.m10 * k, this.m11 * k |
| 45 | + ) |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns the first row of this matrix as a 2d vector. |
| 49 | + * |
| 50 | + * @return The first row of this matrix as a 2d vector |
| 51 | + */ |
| 52 | + def row0: Vec2f = Vec2f(this.m00, this.m01) |
| 53 | + |
| 54 | + /** |
| 55 | + * Returns the second row of this matrix as a 2d vector. |
| 56 | + * |
| 57 | + * @return The second row of this matrix as a 2d vector |
| 58 | + */ |
| 59 | + def row1: Vec2f = Vec2f(this.m10, this.m11) |
| 60 | + |
| 61 | + /** |
| 62 | + * Returns the first column of this matrix as a 2d vector. |
| 63 | + * |
| 64 | + * @return The first column of this matrix as a 2d vector |
| 65 | + */ |
| 66 | + def col0: Vec2f = Vec2f(this.m00, this.m10) |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns the second column of this matrix as a 2d vector. |
| 70 | + * |
| 71 | + * @return The second column of this matrix as a 2d vector |
| 72 | + */ |
| 73 | + def col1: Vec2f = Vec2f(this.m01, this.m11) |
| 74 | + |
| 75 | + /** |
| 76 | + * Returns the product of this matrix by the given vector. |
| 77 | + * |
| 78 | + * @param v The vector by which this matrix is multiplied |
| 79 | + * @return The product of this matrix by the given vector |
| 80 | + */ |
| 81 | + override def *(v: Vec2f): Vec2f = Vec2f(this.row0 dot v, this.row1 dot v) |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns the product of this matrix by the vector with the given components. |
| 85 | + * |
| 86 | + * @param x X component of the vector by which this matrix is multiplied |
| 87 | + * @param y Y component of the vector by which this matrix is multiplied |
| 88 | + * @return The product between this matrix and the vector with the given components |
| 89 | + */ |
| 90 | + def *(x: Float, y: Float): Vec2f = this * Vec2f(x, y) |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns the product of this matrix by the vector with the given components. |
| 94 | + * |
| 95 | + * This method can be used in place of the '*' operator for better interoperability with Java. |
| 96 | + * |
| 97 | + * @param x X component of the vector by which this matrix is multiplied |
| 98 | + * @param y Y component of the vector by which this matrix is multiplied |
| 99 | + * @return The product between this matrix and the vector with the given components |
| 100 | + */ |
| 101 | + def multiply(x: Float, y: Float): Vec2f = this * Vec2f(x, y) |
| 102 | + |
| 103 | + /** |
| 104 | + * Returns the product between this matrix and the given one. |
| 105 | + * |
| 106 | + * @param m The matrix by which this one is multiplied |
| 107 | + * @return The product between this matrix and the given one |
| 108 | + */ |
| 109 | + override def *(m: Mat2f): Mat2f = Mat2f( |
| 110 | + this.row0 dot m.col0, this.row0 dot m.col1, |
| 111 | + this.row1 dot m.col0, this.row1 dot m.col1 |
| 112 | + ) |
| 113 | + |
| 114 | + /** |
| 115 | + * Returns the transposed of this matrix. |
| 116 | + * |
| 117 | + * @return The transposed of this matrix |
| 118 | + */ |
| 119 | + override def transposed: Mat2f = Mat2f( |
| 120 | + this.m00, this.m10, |
| 121 | + this.m01, this.m11 |
| 122 | + ) |
| 123 | + |
| 124 | + /** |
| 125 | + * Returns this matrix to the power of the given exponent. |
| 126 | + * |
| 127 | + * @param exp The exponent |
| 128 | + * @return This matrix raised to the power of the given exponent |
| 129 | + */ |
| 130 | + override def power(exp: Int): Mat2f = { |
| 131 | + if (exp < 0) { |
| 132 | + this.transposed.power(-exp) |
| 133 | + } else if (exp == 0) { |
| 134 | + Mat2f.Identity |
| 135 | + } else { |
| 136 | + this * this.power(exp - 1) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Returns the determinant of this matrix. |
| 142 | + * |
| 143 | + * @return The determinant of this matrix |
| 144 | + */ |
| 145 | + override def determinant: Float = this.m00 * this.m11 - this.m01 * this.m10 |
| 146 | +} |
| 147 | + |
| 148 | +object Mat2f { |
| 149 | + /** Shorthand for `new Mat2f(1.0f, 0.0f, 0.0f, 1.0f)` */ |
| 150 | + val Identity: Mat2f = Mat2f(1.0f, 0.0f, 0.0f, 1.0f) |
| 151 | + /** Shorthand for `new Mat2f(0.0f, 0.0f, 0.0f, 0.0f)` */ |
| 152 | + val Zero: Mat2f = Mat2f(0.0f, 0.0f, 0.0f, 0.0f) |
| 153 | +} |
0 commit comments