Create a rectangle using two points

892 Views Asked by At

First of all, i'm new here, sorry if i miss anything, speak and draw badly.

I have 2 vectors and the length $n$, I want to know the formula to get corners of this equilateral rectangle.

enter image description here (Large Version)

I'm working on a C# program if you really want to know, but I think this is overall more related to maths. I hope the picture is clear enough.

1

There are 1 best solutions below

0
On BEST ANSWER

The difference vector $d$ between the red points $P=(p_x, p_y)$ and $Q =(q_x, q_y)$ is $$ d = P - Q = (p_x - q_x, p_y - q_y) $$ The two vectors $n$ fulfill $n \perp d$ thus $$ 0 = n \cdot d = n_x d_x + n_y d_y \quad (*) $$ and have the length $$ \lVert n \rVert = \sqrt{n^2} = \sqrt{n_x^2 + n_y^2} \quad (**) $$ The green points then are $P + n$ and $Q + n$.

To determine $n$ we have two unknowns $n_x$ and $n_y$ and two equations $(*)$ (a line) and $(**)$ (a circle) so there might be zero (line and circle do not intersect), one or two solutions.

Assuming $d \ne 0$ ($P$ and $Q$ are not equal), we have either $d_x$ or $d_y$ not zero. We assume $d_y \ne 0$ (otherwise $d_x \ne 0$ and the following can be performed with $x$ and $y$ coordinates swapped). Then $$ n_y = -\frac{d_x}{d_y} n_x \quad (***) $$ and we can write $$ \lVert n \rVert^2 =n_x^2 + n_y^2 = \left( 1 + \left(\frac{d_x}{d_y} \right)^2 \right) n_x^2 $$ which gives $$ n_x = \pm \frac{\lVert n \rVert}{\sqrt{1 + \left(\frac{d_x}{d_y} \right)^2 }} $$ So you can determine $n_x$ from $d$ and $\lVert n \rVert$ and use $(***)$ to get $n_y$.

Remember to check the assumptions in your program. If $d = 0$ then there is no unique solution. If $d_y = 0$ you need to do the above swapped. In case of $\lVert n \rVert = 0$ you will just get one solution, the original $P$ and $Q$.