Skip to content

Commit e03e31b

Browse files
authored
Added print() method to PMatrix (Fixes #811) (#1355)
* Add print method to PMatrix interface Added a default method to print matrix data. * docs: add print() method declaration to PMatrix interface * refactor: implement toString and override print in PMatrix2D * refactor: implement toString and override print in PMatrix3D
1 parent dd3dce2 commit e03e31b

File tree

3 files changed

+20
-61
lines changed

3 files changed

+20
-61
lines changed

core/src/processing/core/PMatrix.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,4 +205,8 @@ public void preApply(float n00, float n01, float n02, float n03,
205205
* @return the determinant of the matrix
206206
*/
207207
public float determinant();
208+
/**
209+
* Print the matrix data to the console.
210+
*/
211+
public void print();
208212
}

core/src/processing/core/PMatrix2D.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -466,26 +466,15 @@ public float determinant() {
466466
//////////////////////////////////////////////////////////////
467467

468468

469+
@Override
469470
public void print() {
470-
int big = (int) abs(max(PApplet.max(abs(m00), abs(m01), abs(m02)),
471-
PApplet.max(abs(m10), abs(m11), abs(m12))));
472-
473-
int digits = 1;
474-
if (Float.isNaN(big) || Float.isInfinite(big)) { // avoid infinite loop
475-
digits = 5;
476-
} else {
477-
while ((big /= 10) != 0) digits++; // cheap log()
478-
}
479-
480-
System.out.println(PApplet.nfs(m00, digits, 4) + " " +
481-
PApplet.nfs(m01, digits, 4) + " " +
482-
PApplet.nfs(m02, digits, 4));
483-
484-
System.out.println(PApplet.nfs(m10, digits, 4) + " " +
485-
PApplet.nfs(m11, digits, 4) + " " +
486-
PApplet.nfs(m12, digits, 4));
471+
System.out.print(toString());
472+
}
487473

488-
System.out.println();
474+
@Override
475+
public String toString() {
476+
return PApplet.nfs(m00, 1, 4) + " " + PApplet.nfs(m01, 1, 4) + " " + PApplet.nfs(m02, 1, 4) + "\n" +
477+
PApplet.nfs(m10, 1, 4) + " " + PApplet.nfs(m11, 1, 4) + " " + PApplet.nfs(m12, 1, 4) + "\n";
489478
}
490479

491480

core/src/processing/core/PMatrix3D.java

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -809,52 +809,18 @@ protected boolean invApply(float n00, float n01, float n02, float n03,
809809
//////////////////////////////////////////////////////////////
810810

811811

812+
@Override
812813
public void print() {
813-
/*
814-
System.out.println(m00 + " " + m01 + " " + m02 + " " + m03 + "\n" +
815-
m10 + " " + m11 + " " + m12 + " " + m13 + "\n" +
816-
m20 + " " + m21 + " " + m22 + " " + m23 + "\n" +
817-
m30 + " " + m31 + " " + m32 + " " + m33 + "\n");
818-
*/
819-
int big = (int) Math.abs(max(max(max(max(abs(m00), abs(m01)),
820-
max(abs(m02), abs(m03))),
821-
max(max(abs(m10), abs(m11)),
822-
max(abs(m12), abs(m13)))),
823-
max(max(max(abs(m20), abs(m21)),
824-
max(abs(m22), abs(m23))),
825-
max(max(abs(m30), abs(m31)),
826-
max(abs(m32), abs(m33))))));
827-
828-
int digits = 1;
829-
if (Float.isNaN(big) || Float.isInfinite(big)) { // avoid infinite loop
830-
digits = 5;
831-
} else {
832-
while ((big /= 10) != 0) digits++; // cheap log()
833-
}
834-
835-
System.out.println(PApplet.nfs(m00, digits, 4) + " " +
836-
PApplet.nfs(m01, digits, 4) + " " +
837-
PApplet.nfs(m02, digits, 4) + " " +
838-
PApplet.nfs(m03, digits, 4));
839-
840-
System.out.println(PApplet.nfs(m10, digits, 4) + " " +
841-
PApplet.nfs(m11, digits, 4) + " " +
842-
PApplet.nfs(m12, digits, 4) + " " +
843-
PApplet.nfs(m13, digits, 4));
844-
845-
System.out.println(PApplet.nfs(m20, digits, 4) + " " +
846-
PApplet.nfs(m21, digits, 4) + " " +
847-
PApplet.nfs(m22, digits, 4) + " " +
848-
PApplet.nfs(m23, digits, 4));
849-
850-
System.out.println(PApplet.nfs(m30, digits, 4) + " " +
851-
PApplet.nfs(m31, digits, 4) + " " +
852-
PApplet.nfs(m32, digits, 4) + " " +
853-
PApplet.nfs(m33, digits, 4));
854-
855-
System.out.println();
814+
System.out.print(toString());
856815
}
857816

817+
@Override
818+
public String toString() {
819+
return PApplet.nfs(m00, 1, 4) + " " + PApplet.nfs(m01, 1, 4) + " " + PApplet.nfs(m02, 1, 4) + " " + PApplet.nfs(m03, 1, 4) + "\n" +
820+
PApplet.nfs(m10, 1, 4) + " " + PApplet.nfs(m11, 1, 4) + " " + PApplet.nfs(m12, 1, 4) + " " + PApplet.nfs(m13, 1, 4) + "\n" +
821+
PApplet.nfs(m20, 1, 4) + " " + PApplet.nfs(m21, 1, 4) + " " + PApplet.nfs(m22, 1, 4) + " " + PApplet.nfs(m23, 1, 4) + "\n" +
822+
PApplet.nfs(m30, 1, 4) + " " + PApplet.nfs(m31, 1, 4) + " " + PApplet.nfs(m32, 1, 4) + " " + PApplet.nfs(m33, 1, 4) + "\n";
823+
}
858824

859825
//////////////////////////////////////////////////////////////
860826

0 commit comments

Comments
 (0)