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 behindthescenes 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, [y, [z]]])¶ Creates a translation matrix for the given offset.
 Parameters
x (
int
orfloat
) – xcoordinate of translation (default 0)y (
int
orfloat
) – ycoordinate of translation (default 0)z (
int
orfloat
) – zcoordinate of translation (default 0)

classmethod
Matrix.
CreateRotation
([ang, [x, [y, [z]]]])¶ 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 zaxis is the default axis of rotation.
 Parameters
angle (
int
orfloat
) – angle of rotation in degrees (default 0)x (
int
orfloat
) – xcoordinate of rotation axis (default 0)y (
int
orfloat
) – ycoordinate of rotation axis (default 0)z (
int
orfloat
) – zcoordinate of rotation axis (default 1)

classmethod
Matrix.
CreateScale
([x, [y, [z]]])¶ Scales this matrix (inplace) by the given amount
 Parameters
x (
int
orfloat
) – xcoordinate of the scale (default 1)y – ycoordinate of the scale (default 1)
z – zcoordinate 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
, andVector3
. The value returned will have the same type asvalue
. 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, [y, [z]]])¶ Translates this matrix (inplace) by the given amount.
This method will modify the attributes of this oject. This method returns this object for chaining.
 Parameters
x (
int
orfloat
) – xcoordinate of translation (default 0)y (
int
orfloat
) – ycoordinate of translation (default 0)z (
int
orfloat
) – zcoordinate of translation (default 0)
 Returns
This object, newly modified

Matrix.
rotate
([ang, [x, [y, [z]]]])¶ 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 zaxis 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
orfloat
) – angle of rotation in degrees (default 0)x (
int
orfloat
) – xcoordinate of rotation axis (default 0)y (
int
orfloat
) – ycoordinate of rotation axis (default 0)z (
int
orfloat
) – zcoordinate of rotation axis (default 1)
 Returns
This object, newly modified

Matrix.
scale
([x, [y, [z]]])¶ Scales this matrix (inplace) by the given amount
This method will modify the attributes of this oject. This method returns this object for chaining.
 Parameters
x (
int
orfloat
) – xcoordinate of the scale (default 1)y – ycoordinate of the scale (default 1)
z – zcoordinate 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 premultiplies 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 andq
is a translation. Then:p * q
produces a rotation followed by a translation.
 Parameters
other (
Matrix
) – the matrix to premultiply Returns
The result of premultiplying this matrix by
other
 Return type
Matrix

Matrix.
__imul__
(other)¶ Premultiplies this matrix by
other
in place.This operation premultiplies 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 andq
is a translation. Then:p *= q
changes
p
into a rotation followed by a translation. Returns
This object, newly modified
 Return type
Matrix