Matrix44


public class Matrix44
extends Object

java.lang.Object
   ↳ android.graphics.Matrix44


The Matrix44 class holds a 4x4 matrix for transforming coordinates. It is similar to Matrix, and should be used when you want to manipulate the canvas in 3D. Values are kept in row-major order. The values and operations are treated as column vectors.

Summary

Public constructors

Matrix44()

The default Matrix44 constructor will instantiate an identity matrix.

Matrix44(Matrix mat)

Creates and returns a Matrix44 by taking the 3x3 Matrix and placing it on the 0 of the z-axis by setting row 2 and column 2 to the identity as seen in the following operation:

 [ a b c ]      [ a b 0 c ]
 [ d e f ]  ->  [ d e 0 f ]
 [ g h i ]      [ 0 0 1 0 ]
                [ g h 0 i ]
 

Public methods

Matrix44 concat(Matrix44 b)

Multiplies `this` matrix (A) and provided Matrix (B) in the order of A * B.

boolean equals(Object obj)

Indicates whether some other object is "equal to" this one.

float get(int row, int col)

Gets the value at the matrix's row and column.

void getValues(float[] dst)

Copies matrix values into the provided array in row-major order.

int hashCode()

Returns a hash code value for the object.

boolean invert()

Inverts the Matrix44, then return true if successful, false if unable to invert.

boolean isIdentity()

Returns true if Matrix44 is equal to identity matrix.

float[] map(float x, float y, float z, float w)

Multiplies (x, y, z, w) vector by the Matrix44, then returns the new (x, y, z, w).

void map(float x, float y, float z, float w, float[] dst)

Multiplies (x, y, z, w) vector by the Matrix44, then returns the new (x, y, z, w).

void reset()

Sets the Matrix44 to the identity matrix.

Matrix44 rotate(float deg, float xComp, float yComp, float zComp)

Applies a rotation around a given axis, then returns self.

Matrix44 scale(float x, float y, float z)

Applies scaling factors to `this` Matrix44, then returns self.

void set(int row, int col, float val)

Sets the value at the matrix's row and column to the provided value.

void setValues(float[] src)

Replaces the Matrix's values with the values in the provided array.

String toString()

Returns a string representation of the object.

Matrix44 translate(float x, float y, float z)

Applies a translation to `this` Matrix44, then returns self.

Inherited methods

Public constructors

Matrix44

Added in API level 35
public Matrix44 ()

The default Matrix44 constructor will instantiate an identity matrix.

Matrix44

Added in API level 35
public Matrix44 (Matrix mat)

Creates and returns a Matrix44 by taking the 3x3 Matrix and placing it on the 0 of the z-axis by setting row 2 and column 2 to the identity as seen in the following operation:

 [ a b c ]      [ a b 0 c ]
 [ d e f ]  ->  [ d e 0 f ]
 [ g h i ]      [ 0 0 1 0 ]
                [ g h 0 i ]
 

Parameters
mat Matrix: A 3x3 Matrix to be converted (original Matrix will not be changed) This value cannot be null.

Public methods

concat

Added in API level 35
public Matrix44 concat (Matrix44 b)

Multiplies `this` matrix (A) and provided Matrix (B) in the order of A * B. The result is saved in `this` Matrix.

Parameters
b Matrix44: The second Matrix in the concatenation operation This value cannot be null.

Returns
Matrix44 A reference to this Matrix, which can be used to chain Matrix operations This value cannot be null.

equals

Added in API level 35
public boolean equals (Object obj)

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj Object: the reference object with which to compare.

Returns
boolean true if this object is the same as the obj argument; false otherwise.

get

Added in API level 35
public float get (int row, 
                int col)

Gets the value at the matrix's row and column.

Parameters
row int: An integer from 0 to 3 indicating the row of the value to get Value is between 0 and 3 inclusive

col int: An integer from 0 to 3 indicating the column of the value to get Value is between 0 and 3 inclusive

Returns
float

getValues

Added in API level 35
public void getValues (float[] dst)

Copies matrix values into the provided array in row-major order.

Parameters
dst float: The float array where values will be copied, must be of length 16 This value cannot be null.

Throws
IllegalArgumentException if the destination float array is not of length 16

hashCode

Added in API level 35
public int hashCode ()

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

Returns
int a hash code value for this object.

invert

Added in API level 35
public boolean invert ()

Inverts the Matrix44, then return true if successful, false if unable to invert.

Returns
boolean true on success, false otherwise

isIdentity

Added in API level 35
public boolean isIdentity ()

Returns true if Matrix44 is equal to identity matrix.

Returns
boolean

map

Added in API level 35
public float[] map (float x, 
                float y, 
                float z, 
                float w)

Multiplies (x, y, z, w) vector by the Matrix44, then returns the new (x, y, z, w). Users should set w to 1 to indicate the coordinates are normalized.

Parameters
x float

y float

z float

w float

Returns
float[] An array of length 4 that represents the x, y, z, w (where w is perspective) value after multiplying x, y, z, 1 by the matrix This value cannot be null.

map

Added in API level 35
public void map (float x, 
                float y, 
                float z, 
                float w, 
                float[] dst)

Multiplies (x, y, z, w) vector by the Matrix44, then returns the new (x, y, z, w). Users should set w to 1 to indicate the coordinates are normalized.

Parameters
x float

y float

z float

w float

dst float: This value cannot be null.

reset

Added in API level 35
public void reset ()

Sets the Matrix44 to the identity matrix.

rotate

Added in API level 35
public Matrix44 rotate (float deg, 
                float xComp, 
                float yComp, 
                float zComp)

Applies a rotation around a given axis, then returns self. x, y, z represent the axis by which to rotate around. For example, pass in 1, 0, 0 to rotate around the x-axis. The axis provided will be normalized.

Parameters
deg float: Amount in degrees to rotate the matrix about the x-axis

xComp float: X component of the rotation axis

yComp float: Y component of the rotation axis

zComp float: Z component of the rotation axis

Returns
Matrix44 A reference to this Matrix, which can be used to chain Matrix operations This value cannot be null.

scale

Added in API level 35
public Matrix44 scale (float x, 
                float y, 
                float z)

Applies scaling factors to `this` Matrix44, then returns self. Pass 1s for no change.

Parameters
x float: Scaling factor for the x-axis

y float: Scaling factor for the y-axis

z float: Scaling factor for the z-axis

Returns
Matrix44 A reference to this Matrix, which can be used to chain Matrix operations This value cannot be null.

set

Added in API level 35
public void set (int row, 
                int col, 
                float val)

Sets the value at the matrix's row and column to the provided value.

Parameters
row int: An integer from 0 to 3 indicating the row of the value to change Value is between 0 and 3 inclusive

col int: An integer from 0 to 3 indicating the column of the value to change Value is between 0 and 3 inclusive

val float: The value the element at the specified index will be set to

setValues

Added in API level 35
public void setValues (float[] src)

Replaces the Matrix's values with the values in the provided array.

Parameters
src float: A float array of length 16. Floats are treated in row-major order This value cannot be null.

Throws
IllegalArgumentException if the destination float array is not of length 16

toString

Added in API level 35
public String toString ()

Returns a string representation of the object.

Returns
String a string representation of the object.

translate

Added in API level 35
public Matrix44 translate (float x, 
                float y, 
                float z)

Applies a translation to `this` Matrix44, then returns self.

Parameters
x float: Translation for the x-axis

y float: Translation for the y-axis

z float: Translation for the z-axis

Returns
Matrix44 A reference to this Matrix, which can be used to chain Matrix operations This value cannot be null.