-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Give PMatrix a print() method #811
Comments
And since I'm here, usually you would override the |
Hey @benfry @PianoMastR64 do you mean something like this? |
@vaishnavi192 I was thinking something maybe like this: public interface PMatrix {
default void print(){
System.out.println(this);
}
}
public class PMatrix2D implements PMatrix {
@Override
public String toString() {
int big = (int) abs(max(PApplet.max(abs(m00), abs(m01), abs(m02)),
PApplet.max(abs(m10), abs(m11), abs(m12))));
int digits = 1;
if(Float.isNaN(big) || Float.isInfinite(big)) { // avoid infinite loop
digits = 5;
} else {
while((big /= 10) != 0) digits++; // cheap log()
}
StringBuilder sb = new StringBuilder();
sb
.append(PApplet.nfs(m00, digits, 4)).append(" ")
.append(PApplet.nfs(m01, digits, 4)).append(" ")
.append(PApplet.nfs(m02, digits, 4)).append("\n")
.append(PApplet.nfs(m10, digits, 4)).append(" ")
.append(PApplet.nfs(m11, digits, 4)).append(" ")
.append(PApplet.nfs(m12, digits, 4)).append("\n");
return sb.toString();
}
}
public class PMatrix3D implements PMatrix {
@Override
public String toString() {
int big = (int) Math.abs(max(max(max(max(Math.abs(m00), Math.abs(m01)),
max(Math.abs(m02), Math.abs(m03))),
max(max(Math.abs(m10), Math.abs(m11)),
max(Math.abs(m12), Math.abs(m13)))),
max(max(max(Math.abs(m20), Math.abs(m21)),
max(Math.abs(m22), Math.abs(m23))),
max(max(Math.abs(m30), Math.abs(m31)),
max(Math.abs(m32), Math.abs(m33))))));
int digits = 1;
if(Float.isNaN(big) || Float.isInfinite(big)) { // avoid infinite loop
digits = 5;
} else {
while((big /= 10) != 0) digits++; // cheap log()
}
StringBuilder sb = new StringBuilder();
sb
.append(PApplet.nfs(m00, digits, 4)).append(" ")
.append(PApplet.nfs(m01, digits, 4)).append(" ")
.append(PApplet.nfs(m02, digits, 4)).append(" ")
.append(PApplet.nfs(m03, digits, 4)).append("\n")
.append(PApplet.nfs(m10, digits, 4)).append(" ")
.append(PApplet.nfs(m11, digits, 4)).append(" ")
.append(PApplet.nfs(m12, digits, 4)).append(" ")
.append(PApplet.nfs(m13, digits, 4)).append("\n")
.append(PApplet.nfs(m20, digits, 4)).append(" ")
.append(PApplet.nfs(m21, digits, 4)).append(" ")
.append(PApplet.nfs(m22, digits, 4)).append(" ")
.append(PApplet.nfs(m23, digits, 4)).append("\n")
.append(PApplet.nfs(m30, digits, 4)).append(" ")
.append(PApplet.nfs(m31, digits, 4)).append(" ")
.append(PApplet.nfs(m32, digits, 4)).append(" ")
.append(PApplet.nfs(m33, digits, 4)).append("\n");
return sb.toString();
}
} |
And since I'm here, I have a suggestion that doesn't have to be taken seriously. I'm just having fun learning about streams and functional programming in Java recently. This: int big = (int) Math.abs(max(max(max(max(Math.abs(m00), Math.abs(m01)),
max(Math.abs(m02), Math.abs(m03))),
max(max(Math.abs(m10), Math.abs(m11)),
max(Math.abs(m12), Math.abs(m13)))),
max(max(max(Math.abs(m20), Math.abs(m21)),
max(Math.abs(m22), Math.abs(m23))),
max(max(Math.abs(m30), Math.abs(m31)),
max(Math.abs(m32), Math.abs(m33)))))); Could be turned into this: int big = (int) (float) Stream.of(
m00, m01, m02, m03,
m10, m11, m12, m13,
m20, m21, m22, m23,
m30, m31, m32, m33)
.map(Math::abs)
.max(Float::compare)
.orElse(Float.NaN); It's a little more readable. Also, I didn't test it. |
Oh, also |
fixed issue benfry#811 printing a PMatrix now possible
the contribution guidelines say no ;) also this is my first pull request so let's see how this goes |
Since
PMatrix2D
andPMatrix3D
both have aprint()
method with matching signatures, it would make sense for their common interfacePMatrix
to have this method. Right now I have to do this:The text was updated successfully, but these errors were encountered: