# Calculate rectangle coordinates from line and height

I have a line with coordinates (a1, b1) and (b2, b2). I want to calculate the (x1, y1) and (x2, y2) as shown in image below such that the line will form a rectangle. Given, I know the height of the rectangle, how what is the easiest way to calculate (x1, y1) and (x2, y2)?

-
The slope of your given segment is $\frac{b_2-b_1}{a_2-a_1}$; take the negative reciprocal of that for the perpendicular, and then you should be able to reckon how to get the two other points needed... –  Ｊ. Ｍ. Dec 22 '11 at 11:28

As J.M. says, the line from $(a1,b1)$ to $(x1,y1)$ has slope $\sigma = -\frac{a2-a1}{b2-b1}$. So points on the line have the form $$(x,y) = (a1+t, b1+\sigma t)$$You have to choose $t$ so that $(x1,y1)$ is at a distance $h$ from $(a1,b1)$. The distance is $t\sqrt{1+\sigma^2}$, so put $$t=h/\sqrt{1+\sigma^2}$$

The same $t$ serves to calculate $(x2,y2)$.

-
Excellent and easy. Thanks @TonyK! –  Ramesh Soni Dec 22 '11 at 13:04
Let's denote Height as $h$. You should solve following system of equations :
$\begin{cases} h=\sqrt{(x_1-a_1)^2+(y_1-b_1)^2} \\ h=\sqrt{(x_2-a_2)^2+(y_2-b_2)^2} \\ \sqrt {(b_2-b_1)^2+(a_2-a_1)^2}=\sqrt{(y_2-y_1)^2+(x_2-x_1)^2} \\ \frac{y_2-y_1}{x_2-x_1}=\frac{b_2-b_1}{a_2-a_1} \end{cases}$