Skip to main content

5.) Writing Mat2

info
  1. Read through comments of src/A0/math/Mat2.ts
  2. Search for TODO keyword in the src/A0/math/Mat2.ts file to get started.
Goal

This assignment is designed to help you get comfortable working with Typescript. In this assignment, we will implement some functions to make 2x2 matrix related computations much easier with a custom Mat2 class.

1. Facilitate Mat2 property accessing and setting

Task Checklist
  • Write the remaining two getters and setters for m10 and m11.
  • Write the remaining two getters and setters for r1 and c1.
  • Implement getElement(row: number, col: number): number.
note

Getter and setter functions are special member functions, serving as syntax sugers for accessing or setting a certain property of the class (like mat2.m00)

  • set m00() will be called with syntax like mat1.m00 = 1
  • get m00() will be called with syntax like let a = mat1.m00

More on getter and setter: Introduction to JavaScript getters and setters in ES6

Tips for implementing getters and setters for `m10`, `m11`, `r1`, and `c1`
  • Mimic the examples for m00 and m01.
  • Recall that elements are structured as [m00, m01, m10, m11].
Tips for implementing `getElement(row: number, col: number): number`
  • Index starts with 0.
  • Recall that elements are stored in row major order (e.g. getElement(0,1) = m01 = element[1])

2. Ease the construction of a Mat2 object

Task Checklist
  • Implement static FromRows(r0:Vec2, r1:Vec2): Mat2
  • Implement static FromColumns(c0:Vec2, c1:Vec2): Mat2
note
  • static functions are like memeber functions of a class, accessed like Mat2.FromRows(r2,r2)
  • Here, we will implement some static functions, serving as convenient construction functions (e.g. conveniently create a Mat2 object by calling FromRows(r0:Vec2, r1:Vec2))
Tips for implementing `FromRows` and `FromColumns`
  • Recall that you need to use a new keyword to create a new class instance.
  • e.g. new Mat2(1,2,3,4)

3. Facilitate 2x2 matrix-vector and matrix-matrix computation

Task Checklist
  • Implement _timesMatrix(rhs: Mat2): Mat2
  • Implement protected _timesVector(v: Vec2): Vec2
  • Implement plus(other: Mat2): Mat2
  • Implement minus(other: Mat2): Mat2
  • Implement determinant(): number
  • Implement getInverse(): Mat2 | null
Matrix Refresh Materials

4. Ease common matrix creation

Task Checklist
  • Implement static Scale(scale: number): Mat2
  • Implement static Rotation(radians: number): Mat2 (rotate counter-clockwise)
note

Recall that matrices are essentially a way to transform a plane.

In this section, we will implement some common matrices that can scale or rotate a whole plane.

Recall that static functions are like memeber functions of a class, so we would call Mat2.Scale(2) to create a matrix that will scale x and y by 2 for all points on the plane.