public Vector InverseTransform(Vector v) { float x = v.x, y = v.y; float det = m00 * m11 - m01 * m10; if (Math.Abs(det) == 0f) { // determinant is zero; matrix is not invertible throw new NoninvertibleTransformException(this.ToString()); } float rdet = 1 / det; return new Vector((x * m11 - y * m10) * rdet, (y * m00 - x * m01) * rdet); }
public Vector Transform(Vector v) { float x = v.x, y = v.y; return new Vector(m00*x + m10*y, m01*x + m11*y); }
public float DistanceSq(Vector other) { float dx = x - other.x, dy = y - other.y; return dx*dx + dy*dy; }
public float Dot(Vector other) { return x*other.x + y*other.y; }
public float AngleBetween(Vector other) { float cos = Dot(other) / (this.Length * other.Length); return cos >= 1f ? 0f : FloatMath.Acos(cos); }
public float Distance(Vector other) { return FloatMath.Sqrt(DistanceSq(other)); }
public Vector Add(Vector other) { return Add(other.x, other.y); }
public Vector Transform(Vector v) { return Vectors.Transform(v.x, v.y, ScaleX, ScaleY, Rotation); }
public Vector Scale(Vector other) { return new Vector(x*other.x, y*other.y); }
public Vector Subtract(Vector other) { return Add(-other.x, -other.y); }
public Vector RotateScaleAndAdd(float angle, float scale, Vector add) { float sina = FloatMath.Sin(angle), cosa = FloatMath.Cos(angle); return new Vector((x*cosa - y*sina)*scale + add.x, (x*sina + y*cosa)*scale + add.y); }
public Vector RotateAndAdd(float angle, Vector add) { float sina = FloatMath.Sin(angle), cosa = FloatMath.Cos(angle); return new Vector(x*cosa - y*sina + add.x, x*sina + y*cosa + add.y); }
public Vector Lerp(Vector other, float t, Vector result) { float dx = other.x - x, dy = other.y - y; return new Vector(x + t*dx, y + t*dy); }
/** * Returns true if the supplied vectors' x and y components are equal to one another within * {@link MathUtil#EPSILON}. */ public static bool EpsilonEquals(Vector v1, Vector v2) { return EpsilonEquals(v1, v2, MathUtil.EPSILON); }
public Vector AddScaled(Vector other, float v) { return new Vector(x + other.x*v, y + other.y*v); }
/** * Returns true if the supplied vectors' x and y components are equal to one another within * {@code epsilon}. */ public static bool EpsilonEquals(Vector v1, Vector v2, float epsilon) { return Math.Abs(v1.x - v2.x) <= epsilon && Math.Abs(v1.y - v2.y) <= epsilon; }
public Transform Invert() { Vector iscale = new Vector(1f / ScaleX, 1f / ScaleY); Vector t = new Vector(Tx, Ty).Negate().Rotate(-Rotation).Scale(iscale); return new NonUniformTransform(iscale.x, iscale.y, -Rotation, t.x, t.y); }