# How to find a rectangle which is formed from the line

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.

-
Could you please try to explain more clearly what information you are given, and what you need to find? –  Ragib Zaman Aug 28 '11 at 14:58
little has changed post –  Mediator Aug 28 '11 at 15:01
What sort of coordinates do you have? Cartesian coordinates? What sort of coordinates are you looking for? The Cartesian coordinates of the corners of the rectangle? Also, in the image the rectangle looks axis-parallel. Is that intentional? Or do you need the answer for a rectangle with arbitrary orientation with respect to the coordinate axes? Also, there seems to be an implication that A and B lie on the centres of the vertical sides. Is that true? If you want to make such assumptions, you should explicate them; without making them, the problem is underspecified. –  joriki Aug 28 '11 at 15:04
Ok. Do you know if A and B cuts the side into half? –  Ragib Zaman Aug 28 '11 at 15:04
If the coordinates of A and B are $(x_A,y_A), (x_B,y_B)$ and $H$ is the width, then your figure suggests that the coordinates of the four vertices are $(x_A,y_A+H/2),(x_A,y_A−H/2),(x_B,y_B+H/2)$ and $(x_B,y_B−H/2)$. –  Américo Tavares Aug 28 '11 at 15:16

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_y).$$

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$.

-