Prepend a vector with 90 degree angle to an existing one

64 Views Asked by At

First of all: my knowledge in mathematics is a bit rusty, so no matter how simple my question is, I afraid I in every case need a somewhat detailed answer:

I have a line from coordinates (x1,y1) to (x2,y2). Now I want to prepend a second line (x0,y0) to (x1,y1) before this one, but it should have a 90 degree angle to the first one.

Any idea how I can calculate my coordinates (x0,y0) so that the two lines form a right angle?

Finally all should be done in C programming language but I think this does not matter for this specific question.

Thanks for your patience!

2

There are 2 best solutions below

2
On

When two lines are perpendicular, their slopes (denoted by $m$) are negative reciprocals. In general, you can calculate $m$ by using the following formula.

$$m = \frac{\Delta y}{\Delta x} = \frac{y_2-y_1}{x_2-x_1}$$

Call the slope of the first line $m_1$ and the slope of the second line $m_2$.

$$m_1 = \frac{y_2-y_1}{x_2-x_1}$$

$$m_2 = \frac{y_1-y_0}{x_1-x_0}$$

Use

$$m_1 = -\frac{1}{m_2}$$

which gives

$$\frac{y_2-y_1}{x_2-x_1} = -\frac{1}{\frac{y_1-y_0}{x_1-x_0}} \implies \frac{y_2-y_1}{x_2-x_1} = \frac{x_1-x_0}{y_1-y_0}$$

and you have $(x_1, y_1)$ and $(x_2, y_2)$, so you can reach an equation for $y_0$ and $x_0$ followed by calculating for $(x_0, y_0)$, for which there are infinite possibilities.

0
On

There's no need to deal with slopes at all. Let $(a, b)$ be a vector, then $J(a, b):=(-b,a)$ is the rotated vector $(a, b)$ by $90^\circ$. Now let $p_i=(x_i,y_i)$. Then $$p_0=p_1+J(p_2-p_1),$$ that is explicitly written $$\begin{pmatrix}x_0\\ y_0 \end{pmatrix}=\begin{pmatrix}x_1\\ y_1 \end{pmatrix}+J\begin{pmatrix}x_2-x_1\\y_2-y_1 \end{pmatrix}=\begin{pmatrix}x_1\\ y_1 \end{pmatrix}+\begin{pmatrix}y_1-y_2\\x_2-x_1 \end{pmatrix}= \begin{pmatrix}x_1+y_1-y_2\\ y_1+x_2-x_1 \end{pmatrix}. $$