8.5 KiB
8.5 KiB
None
<html>
<head>
</head>
</html>
Question 3 (10 marks)¶
In [1]:
import math
import numpy as np
In [2]:
points = np.load('data/points.npy').astype(np.uint8)
Method 1¶
In [3]:
def m1_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]:
m1_rotation_matrices = []
for t in range(0, 365, 5):
m1_rotation_matrices.append( m1_compute_rotation_matrix(points, t) )
Method 2¶
In [5]:
def m2_compute_rotation_matrix(points, theta):
# Convert angle to radians
theta_rad = np.deg2rad(theta)
cos_theta = np.cos(theta_rad)
sin_theta = np.sin(theta_rad)
# Compute center of the shape
center = np.mean(points, axis=0)
# Construct rotation matrix
rotation_matrix = np.array([
[ cos_theta, -sin_theta, -center[0] * cos_theta + center[1] * sin_theta + center[0] ],
[ sin_theta, cos_theta, -center[0] * sin_theta - center[1] * cos_theta + center[1] ],
[0, 0, 1]
])
return rotation_matrix
In [6]:
m2_rotation_matrices = []
for t in range(0, 365, 5):
m2_rotation_matrices.append( m2_compute_rotation_matrix(points, t) )
Save Output¶
In [7]:
np.save('data/question_3_rotation_matrices.npy', m1_rotation_matrices)
Put students' implementations here¶
This is one common mistake.
In [8]:
def compute_rotation_matrix(points, theta):
# Convert angle to radians
theta_rad = np.deg2rad(theta)
# Calculate sine and cosine of the angle
cos_theta = np.cos(theta_rad)
sin_theta = np.sin(theta_rad)
# Compute center of the shape
center = np.mean(points, axis=0)
# Translate points to origin
translated_points = points - center
# Wrong: Construct rotation matrix
rotation_matrix = np.array([[cos_theta, -sin_theta, center[0]],
[sin_theta, cos_theta, center[1]],
[0, 0, 1]])
return rotation_matrix
Test (Should output ALL PASS)¶
Restart and Run ALL for each submission
In [9]:
n_pass = 0
for t in range(0, 365, 5):
if np.allclose(compute_rotation_matrix(points, t), m1_rotation_matrices[int(t / 5)]):
n_pass = n_pass + 1
print(n_pass, "PASS")
try:
assert n_pass == len(m1_rotation_matrices)
print("ALL PASS")
except AssertionError:
print("Failed")
raise
In [ ]: