Skip to content

Commit 06d69f7

Browse files
author
Samuel Huylebroeck
committed
Add addition method to matrix class
Add method, test, new constructor and improve existing javadocs
1 parent 4193350 commit 06d69f7

File tree

2 files changed

+70
-6
lines changed

2 files changed

+70
-6
lines changed

kernel/src/main/java/com/itextpdf/kernel/geom/Matrix.java

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,32 @@ public Matrix(float tx, float ty) {
100100
vals[I32] = ty;
101101
}
102102

103+
/**
104+
* Creates a Matrix with 9 specified entries
105+
* @param e11 element at position (1,1)
106+
*/
107+
public Matrix(float e11, float e12, float e13, float e21, float e22, float e23, float e31, float e32, float e33){
108+
vals[I11] = e11;
109+
vals[I12] = e12;
110+
vals[I13] = e13;
111+
vals[I21] = e21;
112+
vals[I22] = e22;
113+
vals[I23] = e23;
114+
vals[I31] = e31;
115+
vals[I32] = e32;
116+
vals[I33] = e33;
117+
}
118+
103119
/**
104120
* Creates a Matrix with 6 specified entries
105-
* @param a
106-
* @param b
107-
* @param c
108-
* @param d
109-
* @param e
110-
* @param f
121+
* The third column will always be [0 0 1]
122+
* (row, column)
123+
* @param a element at (1,1)
124+
* @param b element at (1,2)
125+
* @param c element at (2,1)
126+
* @param d element at (2,2)
127+
* @param e element at (3,1)
128+
* @param f element at (3,2)
111129
*/
112130
public Matrix(float a, float b, float c, float d, float e, float f){
113131
vals[I11] = a;
@@ -162,6 +180,32 @@ public Matrix multiply(Matrix by){
162180
return rslt;
163181
}
164182

183+
/**
184+
* adds a matrix from this matrix and returns the results
185+
* @param arg the matrix to subtract from this matrix
186+
* @return a Matrix object
187+
*/
188+
public Matrix add(Matrix arg){
189+
Matrix rslt = new Matrix();
190+
191+
float[] a = vals;
192+
float[] b = arg.vals;
193+
float[] c = rslt.vals;
194+
195+
c[I11] = a[I11]+b[I11];
196+
c[I12] = a[I12]+b[I12];
197+
c[I13] = a[I13]+b[I13];
198+
c[I21] = a[I21]+b[I21];
199+
c[I22] = a[I22]+b[I22];
200+
c[I23] = a[I23]+b[I23];
201+
c[I31] = a[I31]+b[I31];
202+
c[I32] = a[I32]+b[I32];
203+
c[I33] = a[I33]+b[I33];
204+
205+
206+
return rslt;
207+
}
208+
165209
/**
166210
* Subtracts a matrix from this matrix and returns the results
167211
* @param arg the matrix to subtract from this matrix

kernel/src/test/java/com/itextpdf/kernel/geom/MatrixTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,24 @@ public void testDeterminant(){
6969
Assert.assertEquals(-2f, m.getDeterminant(), .001f);
7070
}
7171

72+
@Test
73+
public void testSubtract() throws Exception{
74+
Matrix m1 = new Matrix(1, 2, 3, 4, 5, 6);
75+
Matrix m2 = new Matrix(6, 5, 4, 3, 2, 1);
76+
Matrix shouldBe = new Matrix(-5, -3,0, -1, 1,0, 3, 5,0);
77+
78+
Matrix rslt = m1.subtract(m2);
79+
Assert.assertEquals(shouldBe, rslt);
80+
}
81+
82+
@Test
83+
public void testAdd() throws Exception{
84+
Matrix m1 = new Matrix(1, 2, 3, 4, 5, 6);
85+
Matrix m2 = new Matrix(6, 5, 4, 3, 2, 1);
86+
Matrix shouldBe = new Matrix(7, 7,0, 7, 7,0, 7, 7,2);
87+
88+
Matrix rslt = m1.add(m2);
89+
Assert.assertEquals(shouldBe, rslt);
90+
}
91+
7292
}

0 commit comments

Comments
 (0)