Class Matrix


  • public final class Matrix
    extends Object
    A Matrix object has four Vector objects.

    The four Vector objects represent the four column vectors of the 4-by-4 matrix (as in a Linear Algebra course).

    In computer graphics, the points and vectors of 3-dimensional space are represented using 4-dimensional homogeneous coordinates. So each transformation of 3-dimensional space is represented by a 4-by-4 (homogeneous) matrix.

    A 4-by-4 matrix represents a transformation of 3-dimensional space. The most common transformations are translation, rotation, and scaling. A 4-by-4 matrix can also represent a projection transformation.

    • Method Detail

      • buildFromColumns

        public static Matrix buildFromColumns​(Vector c1,
                                              Vector c2,
                                              Vector c3,
                                              Vector c4)
        This is a static facory method.

        Construct an arbitrary 4-by-4 Matrix using the given column Vectors.

        Parameters:
        c1 - 1st column Vector for the new Matrix
        c2 - 2nd column Vector for the new Matrix
        c3 - 3rd column Vector for the new Matrix
        c4 - 4th column Vector for the new Matrix
        Returns:
        a new Matrix object
      • buildFromRows

        public static Matrix buildFromRows​(Vector r1,
                                           Vector r2,
                                           Vector r3,
                                           Vector r4)
        This is a static facory method.

        Construct an arbitrary 4-by-4 Matrix using the given row Vectors.

        Parameters:
        r1 - 1st row Vector for the new Matrix
        r2 - 2nd row Vector for the new Matrix
        r3 - 3rd row Vector for the new Matrix
        r4 - 4th row Vector for the new Matrix
        Returns:
        a new Matrix object
      • identity

        public static Matrix identity()
        This is a static facory method.

        Construct an identity Matrix.

        Returns:
        a new Matrix object containing an identity Matrix
      • translate

        public static Matrix translate​(double x,
                                       double y,
                                       double z)
        This is a static facory method.

        Construct a translation Matrix that translates by the given amounts in the x, y, and z directions..

        Parameters:
        x - translation factor for the x-direction
        y - translation factor for the y-direction
        z - translation factor for the z-direction
        Returns:
        a new Matrix object containing a translation Matrix
      • scale

        public static Matrix scale​(double d)
        This is a static facory method.

        Construct a diagonal Matrix with the given number on the diagonal.

        This is also a uniform scaling matrix.

        Parameters:
        d - the diagonal value for the new Matrix
        Returns:
        a new Matrix object containing a scaling Matrix
      • scale

        public static Matrix scale​(double x,
                                   double y,
                                   double z)
        This is a static facory method.

        Construct a (diagonal) Matrix that scales in the x, y, and z directions by the given factors.

        Parameters:
        x - scale factor for the x-direction
        y - scale factor for the y-direction
        z - scale factor for the z-direction
        Returns:
        a new Matrix object containing a scaling Matrix
      • rotateX

        public static Matrix rotateX​(double theta)
        This is a static facory method.

        Construct a rotation Matrix that rotates around the x-axis by the angle theta.

        Parameters:
        theta - angle (in degrees) to rotate by around the x-axis
        Returns:
        a new Matrix object containing a rotation Matrix
      • rotateY

        public static Matrix rotateY​(double theta)
        This is a static facory method.

        Construct a rotation Matrix that rotates around the y-axis by the angle theta.

        Parameters:
        theta - angle (in degrees) to rotate by around the y-axis
        Returns:
        a new Matrix object containing a rotation Matrix
      • rotateZ

        public static Matrix rotateZ​(double theta)
        This is a static facory method.

        Construct a rotation Matrix that rotates around the z-axis by the angle theta.

        Parameters:
        theta - angle (in degrees) to rotate by around the z-axis
        Returns:
        a new Matrix object containing a rotation Matrix
      • rotate

        public static Matrix rotate​(double theta,
                                    double x,
                                    double y,
                                    double z)
        This is a static facory method.

        Construct a rotation Matrix that rotates around the axis vector (x,y,z) by the angle theta.

        See https://www.opengl.org/sdk/docs/man2/xhtml/glRotate.xml

        Parameters:
        theta - angle (in degrees) to rotate by around the axis vector
        x - x-component of the axis vector for the rotation
        y - y-component of the axis vector for the rotation
        z - z-component of the axis vector for the rotation
        Returns:
        a new Matrix object containing a rotation Matrix
      • times

        public Matrix times​(double s)
        A scalar times this Matrix returns a new Matrix.
        Parameters:
        s - scalar value to multiply this Matrix by
        Returns:
        a new Matrix object containing the scalar s times this Matrix
      • times

        public Matrix times​(Matrix m)
        This Matrix times Matrix m returns a new Matrix.
        Parameters:
        m - Matrix value to be multiplied on the right of this Matrix
        Returns:
        new Matrix object containing this Matrix times Matrix m
      • rot2euler

        public double[] rot2euler()
        Assuming that the 3-by-3 "rotation part" of this 4-by-4 Matrix represents a pure rotation, return the rotation's three Euler angles, in radians, in the order [x, y, z] for rotations in the order R_z * R_y * R_x.

        A 3-by-3 matrix is a rotation matrix if its inverse is equal to its transpose and its determinant is equal to 1.

        See http://eecs.qmul.ac.uk/~gslabaugh/publications/euler.pdf

        Returns:
        an array of 3 doubles which are this rotation's Euler angles in radians
      • eulerize

        public Matrix eulerize()
        Assuming that this Matrix represents a 3D rotation, return the rotation matrix formed by multiplying this matrix's three Euler angle rotations in the order R_z * R_y * R_x.

        This is mainly for debugging. If this matrix is really a pure rotation, then this method will return a copy of this matrix.

        Returns:
        the "eulerized" version of this Matrix