Implement a 2D translation matrix. Given translation amounts dx and dy, construct a 3x3 homogeneous transformation matrix that translates 2D points and apply it to a set of points.
import numpy as np
def translate_2d(points, dx, dy):
points = np.array(points, dtype=np.float64)
translation_matrix = np.array([
[1, 0, dx],
[0, 1, dy],
[0, 0, 1]
], dtype=np.float64)
n = points.shape[0]
ones = np.ones((n, 1))
homogeneous = np.hstack([points, ones])
translated = (translation_matrix @ homogeneous.T).T
return translated[:, :2].tolist()dx and dy in the last column.