ECMM426-Template/Question 3.ipynb

6.2 KiB
Raw Blame History

None <html> <head> </head>

Question 3 (10 marks)

In [1]:
import numpy as np
In [2]:
points = np.load('data/points.npy').astype(np.uint8)
In [3]:
def compute_rotation_matrix(points, theta):
    """
    Write a function compute_rotation_matrix(points, theta) to compute the rotation matrix in
    homogeneous coordinate system to rotate a shape depicted with 2-dimensional (x,y) coordinates
    points with an angle 𝜃 (theta in the definition) in the anticlockwise direction about the centre of the shape.

    Parameters:
    points: a 2-dimensional numpy array of data type uint8 with shape 𝑘 × 2. Each row
    of points is a Cartesian coordinate (x, y).
    
    theta: a floating-point number denoting the angle of rotation in degree.
    
    Returns:
    The expected output is a 2-dimensional numpy array of data type float64 with shape 3 × 3.
    """

    # Convert theta from degrees to radians
    theta_rad = np.radians(theta)

    # Calculate the centre of the shape
    centre = np.mean(points, axis=0)

    # Define the translation matrices to move the centre of the shape to the origin and back
    translation_to_origin = np.array([[1, 0, -centre[0]],
                                      [0, 1, -centre[1]],
                                      [0, 0, 1]], dtype=np.float64)

    translation_back = np.array([[1, 0, centre[0]],
                                 [0, 1, centre[1]],
                                 [0, 0, 1]], dtype=np.float64)

    # Define the rotation matrix about the origin
    rotation = np.array([[np.cos(theta_rad), -np.sin(theta_rad), 0],
                         [np.sin(theta_rad),  np.cos(theta_rad), 0],
                         [0,                 0,                1]], dtype=np.float64)

    # Combine the translation and rotation into a single transformation matrix
    rotation_matrix = np.dot(np.dot(translation_back, rotation), translation_to_origin)
    
    return rotation_matrix
In [4]:
rotation_matrices = []

for t in range(0, 365, 5):
    rotation_matrices.append( compute_rotation_matrix(points, t) )

Save Output

In [5]:
np.save('data/question_3_rotation_matrices.npy', rotation_matrices)

Put students' implementations here

In [6]:
def compute_rotation_matrix(points, theta):
    # Convert points to float64
    points = points.astype(np.float64)
    # Calculate centre
    centre = np.mean(points, axis=0)
    # Compute rotation matrix
    rotation_matrix = np.array([[np.cos(np.radians(theta)), -np.sin(np.radians(theta)), 0],
                                [np.sin(np.radians(theta)), np.cos(np.radians(theta)), 0],
                                [0, 0, 1]])
    # Translation matrix to origin
    translation_to_origin = np.array([[1, 0, -centre[0]],
                                      [0, 1, -centre[1]],
                                      [0, 0, 1]])
    # Translation matrix to original position
    translation_to_centre = np.array([[1, 0, centre[0]],
                                         [0, 1, centre[1]],
                                         [0, 0, 1]])
    # Combine transformations with data type float64 
    combined_matrix = np.dot(np.dot(translation_to_centre, rotation_matrix), translation_to_origin).astype(np.float64)
    return combined_matrix

    return 0

Test (Should output ALL PASS)

Restart and Run ALL for each submission

In [7]:
n_pass = 0
for t in range(0, 365, 5):
    if np.allclose(compute_rotation_matrix(points, t), rotation_matrices[int(t / 5)]):
        n_pass = n_pass + 1

print(n_pass, "PASS")
assert n_pass == len(rotation_matrices)
print("ALL PASS")
73 PASS
ALL PASS
In [ ]:

</html>