确定围绕对角线的边界矩形 [英] Determine a bounding rectangle around a diagonal line
问题描述
用户将在屏幕上定义一条线,该线在绘制时将具有给定的厚度(或宽度).
A user will define a line on screen which will have, when drawn, a given thickness (or width).
我现在需要能够确定围绕它的边界矩形的坐标.
I now need to be able to determine the coordinates of a bounding rectangle around this.
我有坐标A和B,以及线宽(W).
I have the coordinates A and B, along with the line thickness (W).
如何计算坐标A1,A2,B1和B2.
How can I calculate the coordinates A1, A2, B1 and B2.
我搜索了但找不到与已经问过的问题相对应的问题.
I searched but was unable to find a question corresponding to this already asked.
推荐答案
Dx= Xb - Xa
Dy= Yb - Ya
D= sqrt(Dx * Dx + Dy * Dy)
Dx= 0.5 * W * Dx / D
Dy= 0.5 * W * Dy / D
这将计算(Dx, Dy)
在AB
方向上长度为W/2
的向量.那么(-Dy, Dx)
是垂直向量.
This computes (Dx, Dy)
a vector of length W/2
in the direction of AB
. Then (-Dy, Dx)
is the perpendicular vector.
Xmin = min(Xa, Xb) - abs(Dy)
Xmax = max(Xa, Xb) + abs(Dy)
Ymin = min(Ya, Yb) - abs(Dx)
Ymax = max(Ya, Yb) + abs(Dx)
更新:
我错误地回答了AABB.
I answered for the AABB by mistake.
对于笔触的四个角
Xa - Dy, Ya + Dx
Xa + Dy, Ya - Dx
Xb - Dy, Yb + Dx
Xb + Dy, Yb - Dx
这篇关于确定围绕对角线的边界矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!