Matrix

import introcs

We assume that all matrices at 4x4 matrices, allowing us to represent affine transforms on homogeneous coordinates. This class is primarily used for the graphics applications in class. For more general matrices, you should use numpy.

Note that Matrix has no attributes. This is what we call an opaque class. In fact you cannot even pass attributes to the standard constructor. You are expected to make an identity matrix and then modify it via methods. If you want a special type of matrix quickly, use one of the class methods.

This is a very advanced class for an introductory course and is almost always delegated to behind-the-scenes parts of assignments.

Constructor

class introcs.Matrix

An instance is a homongenous matrices for graphics transforms.

This class is backed by numpy for fast computation. There are no publicly accessible attributes, as it is not safe to access the internals.

The constructor creates a new 4x4 identify matrix

Class Methods

Class methods are methods that are called with the class name before the period, instead of an object. They provide alternate constructors.

classmethod Matrix.CreateTranslation(x=0, y=0, z=0)

Creates a translation matrix for the given offset.

Parameters
  • x (int or float) – x-coordinate of translation (default 0)

  • y (int or float) – y-coordinate of translation (default 0)

  • z (int or float) – z-coordinate of translation (default 0)

classmethod Matrix.CreateRotation(ang=0, x=0, y=0, z=1)

Creates a rotation about the given axis.

The rotation angle is given in degrees, not radians. Rotation is counterclockwise around the angle of rotation. The z-axis is the default axis of rotation.

Parameters
  • angle (int or float) – angle of rotation in degrees (default 0)

  • x (int or float) – x-coordinate of rotation axis (default 0)

  • y (int or float) – y-coordinate of rotation axis (default 0)

  • z (int or float) – z-coordinate of rotation axis (default 1)

classmethod Matrix.CreateScale(x=1, y=1, z=1)

Scales this matrix (in-place) by the given amount

Parameters
  • x (int or float) – x-coordinate of the scale (default 1)

  • y – y-coordinate of the scale (default 1)

  • z – z-coordinate of the scale (default 1)

Immutable Methods

Immutable methods return a new object and do not modify the original.

Matrix.invert()

Inverts this matrix in place.

This method returns this object for chaining.

Returns

This object, newly modified

Return type

Matrix`

Matrix.transpose()
Returns

the transpose of this matrix

Return type

Matrix

Matrix.transform(value)

Transforms the given point or vector by this matrix.

Value can be a point or vector of any dimenion. This includes Point2, Point3, Vector2, and Vector3. The value returned will have the same type as value.

Parameters

value (point or vector) – the object to transform

Returns

The value transformed by this matrix

Return type

type(value)

Matrix.copy()
Returns

a copy of this matrix

Return type

Matrix

Mutable Methods

Mutable methods modify the underlying object.

Matrix.inverse()
Returns

the inverse of this matrix

Return type

Matrix`

Matrix.transpost()

Transposes this matrix in place.

This method returns this object for chaining.

Returns

This object, newly modified

Return type

Matrix`

Matrix.translate(x=0, y=0, z=0)

Translates this matrix (in-place) by the given amount.

This method will modify the attributes of this oject. This method returns this object for chaining.

Parameters
  • x (int or float) – x-coordinate of translation (default 0)

  • y (int or float) – y-coordinate of translation (default 0)

  • z (int or float) – z-coordinate of translation (default 0)

Returns

This object, newly modified

Matrix.rotate(ang=0, x=0, y=0, z=1)

Rotates this matrix (in place) about the given axis

The rotation angle is given in degrees, not radians. Rotation is counterclockwise around the angle of rotation. The z-axis is the default axis of rotation.

This method will modify the attributes of this oject. This method returns this object for chaining.

Parameters
  • angle (int or float) – angle of rotation in degrees (default 0)

  • x (int or float) – x-coordinate of rotation axis (default 0)

  • y (int or float) – y-coordinate of rotation axis (default 0)

  • z (int or float) – z-coordinate of rotation axis (default 1)

Returns

This object, newly modified

Matrix.scale(x=1, y=1, z=1)

Scales this matrix (in-place) by the given amount

This method will modify the attributes of this oject. This method returns this object for chaining.

Parameters
  • x (int or float) – x-coordinate of the scale (default 1)

  • y – y-coordinate of the scale (default 1)

  • z – z-coordinate of the scale (default 1)

Returns

This object, newly modified

Operators

Operators redefine the meaning of the basic operations. For example:: p * q is the same as p.__mul__(q).

Matrix.__mul__(other)

Premultiplies this matrix by other.

This operation pre-multiplies the matrix on the right. As a result, this allows us to read graphics operations left to right (which is more natural). This method does not modify this matrix.

For example, suppose p is a rotation and q is a translation. Then:

p * q

produces a rotation followed by a translation.

Parameters

other (Matrix) – the matrix to pre-multiply

Returns

The result of premultiplying this matrix by other

Return type

Matrix

Matrix.__imul__(other)

Premultiplies this matrix by other in place.

This operation pre-multiplies the matrix on the right. As a result, this allows us to read graphics operations left to right (which is more natural).

This method will modify the attributes of this oject. This method returns this object for chaining.

For example, suppose p is a rotation and q is a translation. Then:

p *= q

changes p into a rotation followed by a translation.

Returns

This object, newly modified

Return type

Matrix