Package snap.geom

Class Transform

java.lang.Object
snap.geom.Transform
All Implemented Interfaces:
Cloneable

public class Transform extends Object implements Cloneable
A class to represent a mathematical linear transforms. Transforms are represented by matrices in row-major form (vs column-major - google it). This works fine with Java2D transforms, which are column-major, since when all six values are given/returned from this class are transposed from Java2D transform (as is everything else).
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected double
     
    protected double
     
    protected double
     
    protected double
     
    protected double
     
    protected double
     
    static final Transform
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new Transform.
    Transform(double[] anArray)
    Creates a new Transform with given double array.
    Transform(double tx, double ty)
    Creates a new Transform with given translation.
    Transform(double a, double b, double c, double d, double tx, double ty)
    Creates a new Transform with given components.
    Transform(float[] anArray)
    Creates a new Transform with given float array.
    Transform(float a, float b, float c, float d, float tx, float ty)
    Creates a new Transform with given components.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the transform to identity.
    Standard clone implementation.
    void
    concat(double a, double b, double c, double d, double tx, double ty)
    Concatentates (pre-multiplies) this transform by given transform: T' = Tn x T.
    void
    Concatentates (pre-multiplies) this transform by given transform: T' = Tn x T.
    boolean
    equals(Object anObj)
    Standard equals implementation.
    Returns the inverse.
    final double[]
    Returns the matrix.
    final void
    getMatrix(double[] m)
    Loads the given matrix.
    static Transform
    getRotate(double theta)
    Returns a rotation transform for given angle in degrees.
    static Transform
    getRotateAround(double anAngle, double aX, double aY)
    Returns a rotation transform for given angle in degrees around given x/y point.
    static Transform
    getScale(double sx, double sy)
    Returns a scale transform.
    static Transform
    getTransformBetweenRects(Rect fromRect, Rect toRect)
    Returns a transform from one rect to another.
    double
    Returns the translation x component.
    double
    Returns the translation y component.
    void
    Inverts this transform.
    final boolean
    Returns whether this transform is identity.
    final boolean
    Returns whether this transform has a rotation component.
    final boolean
    Returns whether this transform is translation only.
    void
    multiply(double a, double b, double c, double d, double tx, double ty)
    Multiplies this transform by the given transform components.
    void
    Multiplies this transform by the given transform.
    void
    preTranslate(double dx, double dy)
    Translates this transform by given x & y in global space (pre-multiply).
    void
    rotate(double anAngle)
    Rotates this transform by given angle in degrees.
    void
    rotateAround(double anAngle, double aX, double aY)
    Returns a rotation transform.
    void
    scale(double sx, double sy)
    Scales this transform by given scale x and scale y.
    final void
    setMatrix(double[] m)
    Sets transform values to given matrix values.
    final void
    setMatrix(double a, double b, double c, double d, double tx, double ty)
    Sets transform values to given matrix values.
    final void
    setMatrix(float[] m)
    Sets transform values to given matrix values.
    final void
    Sets transform values to given transform values.
    void
    skew(double aSkewX, double aSkewY)
    Skews this transform by given skew x and skew y angles in degrees.
    Standard toString implementation.
    final void
    Transforms the given point.
    final void
    Transforms the given rect.
    final void
    Transforms the given size.
    void
    Transforms the given size as a vector (preserves negative values).
    final double
    transformX(double aX, double aY)
    Transforms the given X value.
    final Point
    transformXY(double aX, double aY)
    Transforms the given XY values and return as point.
    final void
    transformXYArray(double[] anAry)
    Transforms the given values.
    final void
    transformXYArray(double[] anAry, int aPntCnt)
    Transforms the given values.
    final double
    transformY(double aX, double aY)
    Transforms the given Y value.
    void
    translate(double dx, double dy)
    Translates this transform by given x & y.

    Methods inherited from class java.lang.Object

    finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • _a

      protected double _a
    • _b

      protected double _b
    • _c

      protected double _c
    • _d

      protected double _d
    • _tx

      protected double _tx
    • _ty

      protected double _ty
    • IDENTITY

      public static final Transform IDENTITY
  • Constructor Details

    • Transform

      public Transform()
      Creates a new Transform.
    • Transform

      public Transform(double tx, double ty)
      Creates a new Transform with given translation.
    • Transform

      public Transform(float[] anArray)
      Creates a new Transform with given float array.
    • Transform

      public Transform(double[] anArray)
      Creates a new Transform with given double array.
    • Transform

      public Transform(float a, float b, float c, float d, float tx, float ty)
      Creates a new Transform with given components.
    • Transform

      public Transform(double a, double b, double c, double d, double tx, double ty)
      Creates a new Transform with given components.
  • Method Details

    • getX

      public double getX()
      Returns the translation x component.
    • getY

      public double getY()
      Returns the translation y component.
    • isIdentity

      public final boolean isIdentity()
      Returns whether this transform is identity.
    • isSimple

      public final boolean isSimple()
      Returns whether this transform is translation only.
    • isRotated

      public final boolean isRotated()
      Returns whether this transform has a rotation component.
    • translate

      public void translate(double dx, double dy)
      Translates this transform by given x & y.
    • preTranslate

      public void preTranslate(double dx, double dy)
      Translates this transform by given x & y in global space (pre-multiply).
    • rotate

      public void rotate(double anAngle)
      Rotates this transform by given angle in degrees.
    • rotateAround

      public void rotateAround(double anAngle, double aX, double aY)
      Returns a rotation transform.
    • scale

      public void scale(double sx, double sy)
      Scales this transform by given scale x and scale y.
    • skew

      public void skew(double aSkewX, double aSkewY)
      Skews this transform by given skew x and skew y angles in degrees.
    • concat

      public void concat(Transform aTr)
      Concatentates (pre-multiplies) this transform by given transform: T' = Tn x T.
    • concat

      public void concat(double a, double b, double c, double d, double tx, double ty)
      Concatentates (pre-multiplies) this transform by given transform: T' = Tn x T. [ a b 0 ] [ _a _b 0 ] [ a*_a+b*_c a*_b+b*_d 0 ] T' = [ c d 0 ] x [ _c _d 0 ] = [ c*_a+d*_c c*_b+d*_d 0 ] [ tx ty 1 ] [ _tx _ty 1 ] [ tx*_a+ty*_c+_tx tx*_b+ty*_d+ty 1 ]
    • multiply

      public void multiply(Transform aTr)
      Multiplies this transform by the given transform.
    • multiply

      public void multiply(double a, double b, double c, double d, double tx, double ty)
      Multiplies this transform by the given transform components. [ _a _b 0 ] [ a b 0 ] [ _a*a+_b*c _a*b+_b*d 0 ] T' = [ _c _d 0 ] x [ c d 0 ] = [ _c*a+_d*c _c*b+_d*d 0 ] [ _tx _ty 1 ] [ tx ty 1 ] [ _tx*a+_ty*c+tx _tx*b+_ty*d+ty 1 ]
    • invert

      public void invert()
      Inverts this transform.
    • getInverse

      public Transform getInverse()
      Returns the inverse.
    • clear

      public void clear()
      Clears the transform to identity.
    • getMatrix

      public final double[] getMatrix()
      Returns the matrix.
    • getMatrix

      public final void getMatrix(double[] m)
      Loads the given matrix.
    • setMatrix

      public final void setMatrix(float[] m)
      Sets transform values to given matrix values.
    • setMatrix

      public final void setMatrix(double[] m)
      Sets transform values to given matrix values.
    • setMatrix

      public final void setMatrix(Transform aTrans)
      Sets transform values to given transform values.
    • setMatrix

      public final void setMatrix(double a, double b, double c, double d, double tx, double ty)
      Sets transform values to given matrix values.
    • transformXYArray

      public final void transformXYArray(double[] anAry)
      Transforms the given values.
    • transformXYArray

      public final void transformXYArray(double[] anAry, int aPntCnt)
      Transforms the given values.
    • transformXY

      public final Point transformXY(double aX, double aY)
      Transforms the given XY values and return as point.
    • transformPoint

      public final void transformPoint(Point aPoint)
      Transforms the given point. [ _a _b 0 ] P' = [ tx ty 1 ] x [ _c _d 0 ] = [ tx * _a + ty * _c + _tx tx * _b + ty * _d + ty 1 ] [ _tx _ty 1 ]
    • transformX

      public final double transformX(double aX, double aY)
      Transforms the given X value.
    • transformY

      public final double transformY(double aX, double aY)
      Transforms the given Y value.
    • transformSize

      public final void transformSize(Size aSize)
      Transforms the given size.
    • transformRect

      public final void transformRect(Rect aRect)
      Transforms the given rect.
    • transformVector

      public void transformVector(Size aSize)
      Transforms the given size as a vector (preserves negative values).
    • clone

      public Transform clone()
      Standard clone implementation.
      Overrides:
      clone in class Object
    • equals

      public boolean equals(Object anObj)
      Standard equals implementation.
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Standard toString implementation.
      Overrides:
      toString in class Object
    • getRotate

      public static Transform getRotate(double theta)
      Returns a rotation transform for given angle in degrees.
    • getRotateAround

      public static Transform getRotateAround(double anAngle, double aX, double aY)
      Returns a rotation transform for given angle in degrees around given x/y point.
    • getScale

      public static Transform getScale(double sx, double sy)
      Returns a scale transform.
    • getTransformBetweenRects

      public static Transform getTransformBetweenRects(Rect fromRect, Rect toRect)
      Returns a transform from one rect to another.