I have Cartesian coordinates $A$ and $B$.
Line $AB$ is the axis (center) of the rectangle.
And I have $H$ (height).
I need Cartesian coordinates blue rectangle.
I have Cartesian coordinates $A$ and $B$.
Line $AB$ is the axis (center) of the rectangle.
And I have $H$ (height).
I need Cartesian coordinates blue rectangle.
To get the coordinates of the corner points, we need to displace $A$ and $B$ by $\pm h$ along the normal of the line connecting them (i.e. along a vector pointing 90° away from the line).
Let $A = (A_x, A_y)$ and $B = (B_x, B_y)$. Then the vector from $A$ to $B$ is
$v = B-A = (v_x, v_y),$
where
$v_x = B_x-A_x \text{ and } v_y = B_y-A_y.$
Rotating this vector by 90° is equivalent to swapping its coordinates and negating one of them, giving the normal vector
$n = (v_y, -v_x) = (B_y-A_y,\ A_x-B_x).$
However, the length of $n$ is the same as the length of $v$; we need to scale it down to $1$ before multiplying it with $\pm h$. To do this, we can divide $n$ by its length $|n| = |v| = \sqrt{v_x^2+v_y^2}$ to get the unit normal vector
$\hat n = \frac{n}{|n|} = \left({\frac{v_y}{\scriptstyle \sqrt{v_x^2+v_y^2}},\ \frac{-v_x}{\scriptstyle \sqrt{v_x^2+v_y^2}}}\right).$
We can then calculate the coordinates of the four corners as $A + h \hat n$, $A - h \hat n$, $B + h \hat n$ and $B - h \hat n$.