📄 fscoordtransform.java
字号:
m[1][0] = (float)Math.sin(Math.toRadians(angle));
m[1][1] = (float)Math.cos(Math.toRadians(angle));
composite(m);
}
/** Sets the shearing factor for the transform.
*
* @param x value to shear the object in the x direction.
* @param y value to shear the object in the y direction.
*/
public void shear(double x, double y)
{
float[][] m = new float[][] {
{1.0f, (float)y, 0.0f},
{(float)x, 1.0f, 0.0f},
{0.0f, 0.0f, 1.0f}
};
composite(m);
}
/**
* Applies the transformation to the coordinates of a point.
*
* @param x x-coordinate of a point.
* @param y x-coordinate of a point.
* @return an array containing the transformed point.
*/
public int[] transformPoint(int x, int y)
{
float[] point = new float[] { (float)x, (float)y, 1.0f };
int[] result = new int[2];
result[0] = (int)(matrix[0][0] * point[0] + matrix[0][1] * point[1] + matrix[0][2] * point[2]);
result[1] = (int)(matrix[1][0] * point[0] + matrix[1][1] * point[1] + matrix[1][2] * point[2]);
return result;
}
/** Gets the 3 X 3 array that is used to store the transformation values.
@return an array, float[3][3], containing the values for the transformation matrix.
*/
public float[][] getMatrix()
{
return matrix;
}
/** Sets the values in the 3 X 3 array that is used to store the transformation values.
@param aMatrix a 3x3 array of floats containing the values defining the transform.
*/
public void setMatrix(float[][] aMatrix)
{
for(int i=0; i<3; i++)
for (int j = 0; j<3; j++)
matrix[i][j] = aMatrix[i][j];
}
/** Composite the 3 X 3 matrix with the FSCoordTransform argument. This method is used to create multiple transformation effects that can be applied to an object in a single step. Using the instance method fixes the order in which the transforms are composited. Since matrix multiplication is not commutative this limits the number of complex transforms that can be generated when compared to the class method.
@param transform an FSCoordTransform object to composite with this instance.
*/
public void composite(FSCoordTransform transform)
{
composite(transform.getMatrix());
}
/**
* Returns true if anObject is equal to this one. Objects are considered
* equal if they would generate identical binary data when they are encoded
* to a Flash file.
*
* @return true if this object would be identical to anObject when encoded.
*/
public boolean equals(Object anObject)
{
boolean result = false;
if (super.equals(anObject))
{
FSCoordTransform typedObject = (FSCoordTransform)anObject;
float m[][] = typedObject.matrix;
result = true;
for(int i=0; i<3; i++)
for (int j=0; j<3; j++)
if (matrix[i][j] != m[i][j])
result = false;
}
return result;
}
public void appendDescription(StringBuffer buffer, int depth)
{
buffer.append(name());
if (depth > 0)
{
buffer.append(": { ");
buffer.append("[ ");
buffer.append("[" + matrix[0][0] + ", " + matrix[0][1] + ", " + matrix[0][2] + "], ");
buffer.append("[" + matrix[1][0] + ", " + matrix[1][1] + ", " + matrix[1][2] + "], ");
buffer.append("[" + matrix[2][0] + ", " + matrix[2][1] + ", " + matrix[2][2] + "] ");
buffer.append("]; ");
buffer.append("}");
}
}
public int length(FSCoder coder)
{
int numberOfBits = 7 + translateFieldSize()*2;
if (containsScaling())
numberOfBits += 5 + scaleFieldSize()*2;
if (containsRotation())
numberOfBits += 5 + rotateFieldSize()*2;
numberOfBits += (numberOfBits % 8 > 0) ? 8 - (numberOfBits % 8) : 0;
return numberOfBits>>3;
}
public void encode(FSCoder coder)
{
int translateBits = translateFieldSize();
coder.alignToByte();
coder.writeBits(containsScaling() ? 1 : 0, 1);
if (containsScaling())
{
int scaleBits = scaleFieldSize();
coder.writeBits(scaleBits, 5);
coder.writeFixedBits(matrix[0][0], scaleBits, 16);
coder.writeFixedBits(matrix[1][1], scaleBits, 16);
}
coder.writeBits(containsRotation() ? 1 : 0, 1);
if (containsRotation())
{
int rotateBits = rotateFieldSize();
coder.writeBits(rotateBits, 5);
coder.writeFixedBits(matrix[1][0], rotateBits, 16);
coder.writeFixedBits(matrix[0][1], rotateBits, 16);
}
coder.writeBits(translateBits, 5);
coder.writeBits((int)matrix[0][2], translateBits);
coder.writeBits((int)matrix[1][2], translateBits);
coder.alignToByte();
}
public void decode(FSCoder coder)
{
int scaleFieldSize = 0;
int rotateFieldSize = 0;
int translateFieldSize = 0;
coder.alignToByte();
boolean _containsScaling = coder.readBits(1, false) != 0 ? true : false;
if (_containsScaling)
{
scaleFieldSize = coder.readBits(5, false);
matrix[0][0] = coder.readFixedBits(scaleFieldSize, 16);
matrix[1][1] = coder.readFixedBits(scaleFieldSize, 16);
}
boolean _containsRotation = coder.readBits(1, false) != 0 ? true : false;
if (_containsRotation)
{
rotateFieldSize = coder.readBits(5, false);
matrix[1][0] = coder.readFixedBits(rotateFieldSize, 16);
matrix[0][1] = coder.readFixedBits(rotateFieldSize, 16);
}
translateFieldSize = coder.readBits(5, false);
matrix[0][2] = (float)coder.readBits(translateFieldSize, true);
matrix[1][2] = (float)coder.readBits(translateFieldSize, true);
coder.alignToByte();
}
/** Returns true if the values in the transformation matrix represent a unity transform - one which will not change the physical appearance or location of a shape.
@return true if the object represents a unity transform, false otherwise.
*/
public boolean isUnityTransform()
{
return ! (containsScaling() || containsRotation() || containsTranslation());
}
private boolean containsScaling()
{
return matrix[0][0] != 1.0f || matrix[1][1] != 1.0f;
}
private boolean containsRotation()
{
return matrix[1][0] != 0.0f || matrix[0][1] != 0.0f;
}
private boolean containsTranslation()
{
return matrix[0][2] != 0.0f || matrix[1][2] != 0.0f;
}
private int scaleFieldSize()
{
int size = 0;
if (isUnityTransform() == false)
size = FSCoder.fixedSize(new float[] {matrix[0][0], matrix[1][1]});
return size;
}
private int rotateFieldSize()
{
int size = FSCoder.fixedSize(new float[] {matrix[1][0], matrix[0][1]});
return size;
}
private int translateFieldSize()
{
int size = 0;
if (containsTranslation())
size = FSCoder.size(new int[] {(int)matrix[0][2], (int)matrix[1][2]}, true);
return size;
}
private void composite(float[][] m)
{
float result[][] = new float[3][3];
result[0][0] = matrix[0][0] * m[0][0] + matrix[0][1] * m[1][0] + matrix[0][2] * m[2][0];
result[0][1] = matrix[0][0] * m[0][1] + matrix[0][1] * m[1][1] + matrix[0][2] * m[2][1];
result[0][2] = matrix[0][0] * m[0][2] + matrix[0][1] * m[1][2] + matrix[0][2] * m[2][2];
result[1][0] = matrix[1][0] * m[0][0] + matrix[1][1] * m[1][0] + matrix[1][2] * m[2][0];
result[1][1] = matrix[1][0] * m[0][1] + matrix[1][1] * m[1][1] + matrix[1][2] * m[2][1];
result[1][2] = matrix[1][0] * m[0][2] + matrix[1][1] * m[1][2] + matrix[1][2] * m[2][2];
result[2][0] = matrix[2][0] * m[0][0] + matrix[2][1] * m[1][0] + matrix[2][2] * m[2][0];
result[2][1] = matrix[2][0] * m[0][1] + matrix[2][1] * m[1][1] + matrix[2][2] * m[2][1];
result[2][2] = matrix[2][0] * m[0][2] + matrix[2][1] * m[1][2] + matrix[2][2] * m[2][2];
matrix[0][0] = result[0][0];
matrix[0][1] = result[0][1];
matrix[0][2] = result[0][2];
matrix[1][0] = result[1][0];
matrix[1][1] = result[1][1];
matrix[1][2] = result[1][2];
matrix[2][0] = result[2][0];
matrix[2][1] = result[2][1];
matrix[2][2] = result[2][2];
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -