题解|二维平移矩阵实现
二维平移矩阵实现
https://www.nowcoder.com/practice/bfd675dba98a4df69d7d4b7156720ca3?tpId=377&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj
二维平移矩阵是一种将二维空间中的点进行平移的矩阵,其计算公式为:
其中,和
分别是平移的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()