牛客题解 | 二维平移矩阵实现

题目

题目链接

二维平移矩阵是一种将二维空间中的点进行平移的矩阵,其计算公式为:

\[T = \begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \\ 0 & 0 & 1 \end{bmatrix} \]

其中,\(t_x\)\(t_y\)分别是平移的x和y方向的距离。
然后将二维点与平移矩阵相乘,得到平移后的点。

标准代码如下

def translate_object(points, tx, ty):
    translation_matrix = np.array([
        [1, 0, tx],
        [0, 1, ty],
        [0, 0, 1]
    ])
    
    homogeneous_points = np.hstack([np.array(points), np.ones((len(points), 1))])
    
    translated_points = np.dot(homogeneous_points, translation_matrix.T)
    
    return translated_points[:, :2].tolist()
posted @ 2025-03-12 12:40  wangxiaoxiao  阅读(5)  评论(0)    收藏  举报