Determine a bounding rectangle around a diagonal line

↘锁芯ラ 提交于 2020-08-23 09:46:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!