python——获取矩形四个角点的坐标
程序员文章站
2022-04-02 18:56:45
...
任务描述
已知矩形中点[row, col],矩形主方向(自定义)与水平轴的夹角angle,长height,宽bottom。
获取矩形四个角点的坐标。
代码
def rect_loc(row, col, angle, height, bottom):
assert angle <= 35, 'angle out of index'
angle *= (10 / 180) * math.pi # 弧度
xo = np.cos(angle)
yo = np.sin(angle)
y1 = row + height / 2 * yo
x1 = col - height / 2 * xo
y2 = row - height / 2 * yo
x2 = col + height / 2 * xo
return np.array(
[
[y1 - bottom/2 * xo, x1 - bottom/2 * yo],
[y2 - bottom/2 * xo, x2 - bottom/2 * yo],
[y2 + bottom/2 * xo, x2 + bottom/2 * yo],
[y1 + bottom/2 * xo, x1 + bottom/2 * yo],
]
).astype(np.int)
if __name__ == '__main__':
loc = rect_loc(200, 200, 0, 100, 40)
print(loc)
# 结果
>>> [[180 150]
[180 250]
[220 250]
[220 150]]