// FIXME
package org.ibex.graphics;
import java.util.*;
+import org.ibex.util.*;
/** an affine transform; all operations are destructive */
public final class Affine {
- // [ a b e ]
- // [ c d f ]
+ // [ a c e ]
+ // [ b d f ]
// [ 0 0 1 ]
public float a, b, c, d, e, f;
Affine(float _a, float _b, float _c, float _d, float _e, float _f) { a = _a; b = _b; c = _c; d = _d; e = _e; f = _f; }
public String toString() { return "[ " + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f + " ]"; }
public Affine copy() { return new Affine(a, b, c, d, e, f); }
+ public boolean doesNotRotate() { return a==0 && b==0 && c==0 && d==0; }
public static Affine identity() { return new Affine(1, 0, 0, 1, 0, 0); }
public static Affine scale(float sx, float sy) { return new Affine(sx, 0, 0, sy, 0, 0); }
public static Affine shear(float degrees) {
public boolean equalsIgnoringTranslation(Affine x) { return a == x.a && b == x.b && c == x.c && d == x.d; }
public Affine clearTranslation() { e = (float)0.0; f = (float)0.0; return this; }
+ public long rotateBox(long box) { return rotateBox(Encode.longToFloat1(box), Encode.longToFloat2(box)); }
+ public long rotateBox(float x, float y) {
+ float width =
+ max(max(multiply_px(0, 0), multiply_px(x, y)), max(multiply_px(x, 0), multiply_px(0, y))) -
+ min(min(multiply_px(0, 0), multiply_px(x, y)), min(multiply_px(x, 0), multiply_px(0, y)));
+ float height =
+ max(max(multiply_py(0, 0), multiply_py(x, y)), max(multiply_py(x, 0), multiply_py(0, y))) -
+ min(min(multiply_py(0, 0), multiply_py(x, y)), min(multiply_py(x, 0), multiply_py(0, y)));
+ return Encode.twoFloatsToLong(width, height);
+ }
+
+ static float min(float a, float b) { if (a<b) return a; else return b; }
+ static float max(float a, float b) { if (a>b) return a; else return b; }
+
public boolean equals(Object o) {
if (!(o instanceof Affine)) return false;
Affine x = (Affine)o;
return new Affine(c, s, -s, c, 0, 0);
}
- /** this = this * a */
- public Affine multiply(Affine A) {
+ /** this = a * this */
+ public Affine premultiply(Affine A) {
float _a = this.a * A.a + this.b * A.c;
float _b = this.a * A.b + this.b * A.d;
float _c = this.c * A.a + this.d * A.c;
return this;
}
- /** this = a * this */
- public Affine premultiply(Affine A) {
+ /** this = this * a */
+ public Affine multiply(Affine A) {
float _a = A.a * this.a + A.b * this.c;
float _b = A.a * this.b + A.b * this.d;
float _c = A.c * this.a + A.d * this.c;
return this;
}
+ public Affine inverse() { return copy().invert(); }
public Affine invert() {
- float det = 1 / (a * d - b * c);
- float _a = d * det;
- float _b = -1 * b * det;
- float _c = -1 * c * det;
- float _d = a * det;
- float _e = -1 * e * a - f * c;
- float _f = -1 * e * b - f * d;
+ float det = (a * d - b * c);
+ float _a = d / det;
+ float _b = -1 * b / det;
+ float _c = -1 * c / det;
+ float _d = a / det;
+ float _e = (f*c-e*d)/det;
+ float _f = (b*e-a*f)/det;
a = _a; b = _b; c = _c; d = _d; e = _e; f = _f;
return this;
}