问题
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.
I have the coordinates A and B, along with the line thickness (W).
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.
回答1:
Dx= Xb - Xa
Dy= Yb - Ya
D= sqrt(Dx * Dx + Dy * Dy)
Dx= 0.5 * W * Dx / D
Dy= 0.5 * W * Dy / D
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)
Update:
I answered for the AABB by mistake.
For the four corners of the stroke
Xa - Dy, Ya + Dx
Xa + Dy, Ya - Dx
Xb - Dy, Yb + Dx
Xb + Dy, Yb - Dx
来源:https://stackoverflow.com/questions/38807203/determine-a-bounding-rectangle-around-a-diagonal-line