FileDocCategorySizeDatePackage
Transform.javaAPI DocphoneME MR2 API (J2ME)12937Wed May 02 18:00:34 BST 2007com.sun.perseus.j2d

Transform

public class Transform extends Object implements org.w3c.dom.svg.SVGMatrix
Class for 2D transforms.
version
$Id: Transform.java,v 1.8 2006/04/21 06:35:16 st125089 Exp $

Fields Summary
float
m0
The (0, 0) component of this 3x3 transformation matrix.
float
m1
The (1, 0) component of this 3x3 transformation matrix.
float
m2
The (0, 1) component of this 3x3 transformation matrix.
float
m3
The (1, 1) component of this 3x3 transformation matrix.
float
m4
The (0, 2) component of this 3x3 transformation matrix.
float
m5
The (1, 2) component of this 3x3 transformation matrix.
Constructors Summary
public Transform(float m0, float m1, float m2, float m3, float m4, float m5)
Constructs an Transform with a given set of components, representing the transform:
x' = m0*x + m2*y + m4
y' = m1*x + m3*y + m5
or, representing the matrix:
[ m0 m2 m4 ]
[ m1 m3 m5 ]
[ 0 0 1 ]

param
m0 (0, 0) matrix value
param
m1 (1, 0) matrix value
param
m2 (0, 1) matrix value
param
m3 (1, 1) matrix value
param
m4 (0, 2) matrix value
param
m5 (1, 2) matrix value

        setTransform(m0, m1, m2, m3, m4, m5);
    
public Transform(org.w3c.dom.svg.SVGMatrix transform)
Constructs an Transform representing the same transform as the given Transform. Future changes to the object supplied as the transform parameter will not affect the newly constructed Transform. If transform is null, the matrix is initialized to be the identity matrix.

param
transform defines the initial state for the newly constructed Transform.

        setTransform(transform);
    
Methods Summary
public booleanequals(java.lang.Object obj)

return
true if obj is a Transform and all its components are the same as this instance.

        if (obj == null || !(obj instanceof Transform)) {
            return false;
        }

        if (obj == this) {
            return true;
        }

        Transform t = (Transform) obj;
        return t.m0 == m0
            &&
            t.m1 == m1
            &&
            t.m2 == m2
            &&
            t.m3 == m3
            &&
            t.m4 == m4
            &&
            t.m5 == m5;
    
public booleanequals(float[][] m)

param
a 6x1 float array containing the transform matrix.
return
true if this transform is equal to the input matrix.

        return m[0][0] == m0
            &&
            m[1][0] == m1
            &&
            m[2][0] == m2
            &&
            m[3][0] == m3
            &&
            m[4][0] == m4
            &&
            m[5][0] == m5;
    
public floatgetComponent(int index)
The components of the matrix denoted a to f in the API are : [ a c e ] [ b d f ] [ 0 0 1 ]

param
index component index for this matrix
return
the component of the matrix corresponding to the index.
throws
DOMException - INDEX_SIZE_ERR if the index is invalid.

        switch (index) {
          case 0:
              return m0;
          case 1:
              return m1;
          case 2:
              return m2;
          case 3:
              return m3;
          case 4:
              return m4;
          case 5:
              return m5;
          default:
              throw new DOMException
                  (DOMException.INDEX_SIZE_ERR, 
                   Messages.formatMessage
                   (Messages.ERROR_OUT_OF_BOUND_PARAMETER_VALUE,
                    new String[] {"SVGMatrix",
                                  "getComponent",
                                  "index",
                                  "" + index}));
        }
    
public org.w3c.dom.svg.SVGMatrixinverse(org.w3c.dom.svg.SVGMatrix txf)
Inverses this transform and stores the resulting transform into the input transform.

param
txf the SVGMatrix into which the result should be stored.
throws
SVGException - SVG_MATRIX_NOT_INVERTABLE when determinant of this matrix is zero.

        Transform svm = (Transform) txf;

        float det = m0 * m3 - m2 * m1;
        if (MathSupport.abs(det) <= Float.MIN_VALUE) {
            throw new SVGException(SVGException.SVG_MATRIX_NOT_INVERTABLE, 
                                   this.toString());
        }

        if (svm == null) {
            svm = new Transform(1, 0, 0, 1, 0, 0);
        }
        
        if (isIdentity()) {
            svm.setTransform(1, 0, 0, 1, 0, 0);
            return svm;
        }

        svm.m0 = m3 / det;
        svm.m2 = -m2 / det;
        svm.m4 = (m2 * m5 - m3 * m4) / det;
        svm.m1 = -m1 / det;
        svm.m3 = m0 / det;
        svm.m5 = (m1 * m4 - m0 * m5) / det;

        return svm;
    
public org.w3c.dom.svg.SVGMatrixinverse()
Returns the inverse matrix.

return
the inverted matrix.
throws
SVGException - SVG_MATRIX_NOT_INVERTABLE when determinant of this matrix is zero.

        Transform svm = new Transform(1, 0, 0, 1, 0, 0);
        inverse(svm);
        return svm;
    
public booleanisIdentity()
Returns true if the current SVGMatrix is equivalent to the identity matrix, false otherwise.

return
true if this matrix is identity, false otherwise.

        return (m0 == 1f 
                &&
                m2 == 0f 
                &&
                m4 == 0f 
                &&
                m1 == 0f 
                &&
                m3 == 1f 
                &&
                m5 == 0f);
    
public booleanisInvertible()
Returns true if the matrix is invertible.

return
true if the matrix is invertible.

        return (m0 * m3 - m1 * m2) != 0;
    
public org.w3c.dom.svg.SVGMatrixmMultiply(org.w3c.dom.svg.SVGMatrix secondMatrix)

        if (secondMatrix == null) {
            throw new NullPointerException();
        }

        Transform sm = (Transform) secondMatrix;

        float t0 = sm.m0;
        float t1 = sm.m1;
        float t2 = sm.m2;
        float t3 = sm.m3;
        float t4 = sm.m4;
        float t5 = sm.m5;

        float mM0 = m0;
        float mM1 = m2;
        m0  = t0 * mM0 + t1 * mM1;
        m2  = t2 * mM0 + t3 * mM1;
        m4 += t4 * mM0 + t5 * mM1;

        mM0 = m1;
        mM1 = m3;
        m1  = t0 * mM0 + t1 * mM1;
        m3  = t2 * mM0 + t3 * mM1;
        m5 += t4 * mM0 + t5 * mM1;

        return this;
    
public org.w3c.dom.svg.SVGMatrixmRotate(float angle)

        final float angl = MathSupport.toRadians(angle);
        final float trigTOLERANCE = 1E-7f;
        final float sin = MathSupport.sin(angl);
        final float cos = MathSupport.cos(angl);

        if (MathSupport.abs(sin) < trigTOLERANCE) {  // angle = 0 or 180 deg.
            if (cos < 0f) {         // angle = 180 deg.
                m0 = -m0;
                m3 = -m3;
                m2 = -m2;
                m1 = -m1;
            }
            return this;
        }

        if (MathSupport.abs(cos) < trigTOLERANCE) {  // angle = 90 or 270 deg.
            if (sin < 0f) {         // angle = 270 deg
                float mM0 = m0;
                m0  = -m2;
                m2  = mM0;
                mM0 = m1;
                m1  = -m3;
                m3  = mM0;
            } else {                 // angle = 90 deg
                float mM0 = m0;
                m0  = m2;
                m2  = -mM0;
                mM0 = m1;
                m1  = m3;
                m3  = -mM0;
           }
           return this;
        }

        float mM0, mM1;
        mM0 = m0;
        mM1 = m2;
        m0 =  cos * mM0 + sin * mM1;
        m2 = -sin * mM0 + cos * mM1;
        mM0 = m1;
        mM1 = m3;
        m1 =  cos * mM0 + sin * mM1;
        m3 = -sin * mM0 + cos * mM1;

        return this;
    
public org.w3c.dom.svg.SVGMatrixmScale(float scaleFactor)

        m0 *= scaleFactor;
        m2 *= scaleFactor;
        m1 *= scaleFactor;
        m3 *= scaleFactor;
        return this;
    
public org.w3c.dom.svg.SVGMatrixmScale(float scaleFactorX, float scaleFactorY)
Post-multiplies a scale transformation on the current matrix and returns the resulting matrix.

[ scaleFactorX 0 0 ]
[ 0 scaleFactorY 0 ]
[ 0 0 1 ]

param
scaleFactorX the factor by which coordinates are scaled along the X axis direction.
param
scaleFactorY the factor by which coordinates are scaled along the Y axis direction.
return
this matrix scaled by the scaleFactor.

        m0 *= scaleFactorX;
        m2 *= scaleFactorY;
        m1 *= scaleFactorX;
        m3 *= scaleFactorY;
        return this;
    
public org.w3c.dom.svg.SVGMatrixmTranslate(float x, float y)

        m4 = x * m0 + y * m2 + m4;
        m5 = x * m1 + y * m3 + m5;

        return this;
    
public voidsetTransform(float m0, float m1, float m2, float m3, float m4, float m5)
Sets the transform to the matrix defined by the input parameters:
[ m0 m2 m4 ]
[ m1 m3 m5 ]
[ 0 0 1 ]

param
m0 (0, 0) matrix value
param
m1 (1, 0) matrix value
param
m2 (0, 1) matrix value
param
m3 (1, 1) matrix value
param
m4 (0, 2) matrix value
param
m5 (1, 2) matrix value

        this.m0 = m0;
        this.m1 = m1;
        this.m2 = m2;
        this.m3 = m3;
        this.m4 = m4;
        this.m5 = m5;
    
public voidsetTransform(org.w3c.dom.svg.SVGMatrix transform)
Sets the transform to the value defined by the input matrix.

param
transform the transform whose value should be copied. If null, this transform is set to identity.

        Transform txf = (Transform) transform;

        if (txf == null) {
            m0 = 1;  // create IDENTITY transformation
            m1 = 0;
            m2 = 0;
            m3 = 1;
            m4 = 0;
            m5 = 0;
            return;
        }

        this.m0 = txf.m0;
        this.m1 = txf.m1;
        this.m2 = txf.m2;
        this.m3 = txf.m3;
        this.m4 = txf.m4;
        this.m5 = txf.m5;
    
public voidtransformPoint(float[] pt, float[] opt)
Transforms a point's coordinates.

param
pt the point to transform
param
opt the transformed point coordinates.

        opt[0] = pt[0] * m0 + pt[1] * m2 + m4;
        opt[1] = pt[0] * m1 + pt[1] * m3 + m5;