I have been pulling my hair out on the trigonometry on this and just can't seem to get it right.
Basically, I need to calculate the bounding box of a line going from point (x1,y1) to (x2,y2) where the line has a thickness of w (centered on the line) and an end cap -- that is to say a half circular projection centered on each endpoint, with a radius of w/2.
This is all in a flat 2 dimensional plane. I need it to generate a clip path for redrawing items under a moving line.
I am looking for the smallest rectangle that will enclose this line for all values of x1, y1, x2, y2 and w.
Any help would be greatly appreciated.
Compute the distance from the first point to the second:
$$d = \sqrt{(x_2-x_1)^2+(y_2-y_1)^2}$$
If $d$ is small then simply use a bounding box as given in my comment. Otherwise compute a vector of length $w/2$ pointing from the first point to the second
$$a = (a_1, a_2) = \left(\frac{w\,(x_2-x_1)}{2d}, \frac{w\,(y_2-y_1)}{2d} \right)$$
Compute a vector that is orthogonal to $a$ by turning it ninety degrees counter clockwise (also of length $w/2$):
$$b = (b_1, b_2) = (-a_2, a_1)$$
Now the four vertices of the bounding box are given by:
$$\begin{eqnarray} c_1 &=& (x_1 - a_1 + b_1, y_1 - a_2 + b_2)\\ c_2 &=& (x_1 - a_1 - b_1, y_1 - a_2 - b_2)\\ c_3 &=& (x_2 + a_1 - b_1, y_2 + a_2 - b_2)\\ c_4 &=& (x_2 + a_1 + b_1, y_2 + a_2 + b_2) \end{eqnarray}$$