2
$\begingroup$

I'm writing a program in which I have to draw something along a line multiple time and spaced evenly. I used two points [$(x_1,y_1)$ and $(x_3, y_3)$] to create a line. Then I found out the equation $y = mx+b$ of the line going through those two points. Now I want to draw multiple time between those two points with a distance $d$ between each drawing [first drawing is at $(x_1, y_1)$].

I know the squared distance between two points in a coordinate system is $d^2 = (x_1 - x_2)^2 + (y_1 -y_2)^2$. But I am not sure how to figure out $x_2$ and $y_2$ from there. is it even possible? is there another way to know $x_2$ and $y_2$ with all the other known information?

$\endgroup$
2
$\begingroup$

$(x_2-x_1)^2+(y_2-y_1)^2=d^2$ is in fact the equation of a circle with radius $d$ and center on $(x_1, y_1)$, so to find $x_2$ and $y_2$ you have to find the intersection of line $y=mx+b$ with this circle , that is you have to solve following system of equation:

$\begin{cases}(x-x_1)^2+(y-y_1)^2=d^2\\ y=mx+b\end{cases}$

You have $x_1$, $y_1$ and $d$ so you can find x and y which in fact is $x_2$ and $y_2$

$\endgroup$
2
$\begingroup$

I think the easiest way is to write the line in vector form where $\vec{n} = \frac 1{\sqrt{1+m^2}}\begin{pmatrix}1 \\ m\end{pmatrix}$ is the normalized direction vector. You will find the $k$-th point on the line starting from $\begin{pmatrix}x_1 \\ y_1\end{pmatrix}$ by

$$\begin{pmatrix}x \\ y\end{pmatrix}= \begin{pmatrix}x_1 \\ y_1\end{pmatrix}+k\cdot d\cdot \vec{n} = \begin{pmatrix}x_1 \\ y_1\end{pmatrix}+k\frac{d}{\sqrt{1+m^2}}\begin{pmatrix}1 \\ m\end{pmatrix}$$

Here is a Desmos-graph which illustrates above formula. You may start with a horizontal line to see that the distance fits and then change the slope $m$ and animate $k$.

$\endgroup$
0
$\begingroup$

You can use a for loop.

Let's say you already drew the point $M_n(x_n, y_n)$ and wish to draw $M_{n+1}(x_{n+1}, y_{n+1})$.

First of all you know that $M_{n+1} \in \Delta: y = mx + b$.

As such $y_{n+1} = m x_{n+1} + b$.

And you know that the distance between $M_n$ and $M_{n+1}$ is $d$.

So $$ d^2 = (x_{n+1} - x_n)^2 + (y_{n+1} - y_n)^2 \\ \iff d^2 = (x_{n+1} - x_n)^2 + (m x_{n+1} + b - y_n)^2 $$ This is a quadratic equation in $x_{n+1}$. Expand it out and compute the discriminant and find the solutions. You'll get two solutions, $x_{n+1, 1}$ and $x_{n+1, 2}$.

If $x_3 > x_1$, then $x_{n+1} = \max(x_{n+1, 1}, x_{n+1, 2})$.

Else $x_{n+1} = \min(x_{n+1, 1}, x_{n+1, 2})$

And after finding $x_{n+1}$ you can just use the formula $y_{n+1} = m x_{n+1} + b$ to find the y coordinate.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.