Get two specific coordinates on a linear function.

40 Views Asked by At

I've been trying to build an algorithm for my Unity3D project to get some specific coordinates, but I got stuck with some math problem.

I have two coordinates of type $ \begin{pmatrix} x \\ y \\ z \end{pmatrix} $ and since it's a 2D project we can ignore $z$ here.

Let's call both coordinates $p_1$ and $p_2$.

First I use the function to calculate $m$: $m = \frac{y_2 - y_1}{x_2 - x_1} $

So now I've got the first part for the linear function: $f(x) = m \cdot x + n$

Actually we don't to calculate $f(x)$ here so there is no need to find $n$.

Then I calculate $m_{orthogonal}$ with the help of: $m \cdot m_{orthogonal} = -1$ or $m_{orthogonal} = \frac{-1}{m}$

Assume $g(x)$ is our orthogonal function here: $g(x) = m_{orthogonal} \cdot x + c$

We could find two different functions if we use $p_1$ or $p_2$, which is totally correct and in the end of my algorithm I will need both, but lets keep it simple for now and only calculate it for $p_1$ if needed: $c_1 = \frac{g(x)}{m_{orthogonal} \cdot x_1}$ (where $x_1$ is from $p_1$)

For the next step lets say I have a distance from point $p_1$ which is: $d = 2$

I need to find two coordinates in both direction from $p_1$, which lies on the orthogonal function (think of a circle, which is cut in half trough its middle point), where $\overline{Ap_1} = d = \overline{p_1B}$ and $\overline{Ap_1B} = 2 \cdot d$

I couldn't find any better explanation and I have problems searching for specific terms on my own.

enter image description here

1

There are 1 best solutions below

0
On

For what you’re trying to do, it might be easier to work with parametric equations of the lines.

The line through $p_1$ and $p_2$ can be expressed by the vector formula $\vec p_1 + (\vec p_2-\vec p_1)t$. The vector $\vec p_2-\vec p_1 = \langle x_2-x_1,y_2-y_1\rangle$ represents the direction of the line. Rotating it 90° counterclockwise gives the vector $\langle y_1-y_2,x_2-x_1 \rangle$. This is the same as what you did when you went from $m$ to $-1/m$ for the slope of the line.

Now you have a formula for the orthogonal line through $p_1$: $\vec p_1+\langle y_1-y_2,x_2-x_1 \rangle t$. Unfortunately, the parameter $t$ doesn’t measure distance along this line. That’s easily remedied, though. Any vector that’s in the same direction will give the same line, so if you use one that has unit length, $t$ will then be distance. You can get such a vector by dividing the one you have by its length: $$ \vec u = {\langle y_1-y_2,x_2-x_1 \rangle \over \|\langle y_1-y_2,x_2-x_1 \rangle\|} = {\langle y_1-y_2,x_2-x_1 \rangle \over \sqrt{(y_1-y_2)^2+(x_2-x_1)^2}}. $$ Note that the denominator is just $\|\vec p_2-\vec p_1\|$, the distance between the two given points. Now, $\vec p_1+\vec ut$ will give you a point along the orthogonal line at a distance $t$ from $p_1$. The sign of $t$ determines the direction in which you move along that line.