[Math] How to interpolate points between 2 points

interpolation

I have 2 points X,Y (for example [5,10] and [20,30]) and I need to interpolate points between these 2 points in order that all this points are spaced by 1 measurement unit.

Let's pretend I am using cm (as my measurement unit) and I have a point at [5,10] and another at [20,30]. How can I know the first point in this interpolation so it's spaced only 1cm from [5,10]? Walking 1cm each step, I would like to know every coordinate of points till I reach the last point [20,30].

Best Answer

Given two points $A$ and $B$, this answer will put points on the line segment between $A$ and $B$ so that the first point is $1$ unit from $A$, the second point is $2$ units from $A$, and so forth until the last point, which is a whole number of units from $A$ and one unit or less from $B$.

Suppose the Cartesian $(x,y)$ coordinates of the points are $A = (x_A, y_A)$ and $B = (x_B, y_B)$. Let $d$ be the distance between these two points; by the Pythagorean Theorem, $$d = \sqrt{(x_A - x_B)^2 + (y_A - y_B)^2}.$$

Since $B$ is at a distance $d$ from $A$, to move $d$ units from $A$ toward $B$ we add $x_B - x_A$ to $x_A$ and $y_B - y_A$ to $y_A$ to get the new $(x,y)$ coordinates. To move just $1$ unit we want to move $\frac1d$ times as far, that is, the point $1$ unit from $A$ is $(x_1,y_1)$ where \begin{align} x_1 = x_A + \frac1d(x_B - x_A),\\ y_1 = y_A + \frac1d(y_B - y_A). \end{align} The next point is at $\frac2d$ of the distance from $A$ to $B$, the next at $\frac3d$ the distance, and so forth. In general the $n$th point that we place along the segment from $A$ to $B$ should be at coordinates $(x_n,y_n)$ where \begin{align} x_n = x_A + \frac nd(x_B - x_A),\\ y_n = y_A + \frac nd(y_B - y_A). \end{align} We do this for each integer $n$ such that $1 \leq n < d$.

Related Question