Skip to content

Commit a154d05

Browse files
committed
Replaced traits with abstract classes
1 parent 638a814 commit a154d05

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2448
-5850
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package vecmatlib.matrix
2+
3+
import vecmatlib.vector.Vec2d
4+
5+
/**
6+
* 2x2 double 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 Mat2d(m00: Double, m01: Double, m10: Double, m11: Double) extends MatDouble[Mat2d, Vec2d] {
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: Mat2d): Mat2d = Mat2d(
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_- : Mat2d = Mat2d(
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: Double): Mat2d = Mat2d(
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: Vec2d = Vec2d(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: Vec2d = Vec2d(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: Vec2d = Vec2d(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: Vec2d = Vec2d(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: Vec2d): Vec2d = Vec2d(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: Double, y: Double): Vec2d = this * Vec2d(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: Double, y: Double): Vec2d = this * Vec2d(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: Mat2d): Mat2d = Mat2d(
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: Mat2d = Mat2d(
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): Mat2d = {
131+
if (exp < 0) {
132+
this.transposed.power(-exp)
133+
} else if (exp == 0) {
134+
Mat2d.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: Double = this.m00 * this.m11 - this.m01 * this.m10
146+
}
147+
148+
object Mat2d {
149+
/** Shorthand for `new Mat2d(1.0, 0.0, 0.0, 1.0)` */
150+
val Identity: Mat2d = Mat2d(1.0, 0.0, 0.0, 1.0)
151+
/** Shorthand for `new Mat2d(0.0, 0.0, 0.0, 0.0)` */
152+
val Zero: Mat2d = Mat2d(0.0, 0.0, 0.0, 0.0)
153+
}
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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

Comments
 (0)