python
interpolation
line geometry
programming tutorial
data visualization

How to interpolate a line between two other lines in python

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Interpolating a line between two other lines means creating a new line that lies partway between them according to some interpolation parameter. The simplest case is when both lines are represented in slope-intercept form, because then you can interpolate the slope and intercept directly.

Interpolate in slope-intercept form

Suppose the two lines are:

  • 'y = m1 * x + b1'
  • 'y = m2 * x + b2'

If t is between 0 and 1, then the interpolated line is:

  • 'm = (1 - t) * m1 + t * m2'
  • 'b = (1 - t) * b1 + t * b2'

In Python:

python
1def interpolate_line(m1, b1, m2, b2, t):
2    m = (1 - t) * m1 + t * m2
3    b = (1 - t) * b1 + t * b2
4    return m, b
5
6
7m, b = interpolate_line(1.0, 0.0, 3.0, 4.0, 0.5)
8print(m, b)

When t = 0, you get the first line. When t = 1, you get the second line. A value like 0.5 gives the midpoint line in coefficient space.

Evaluate the interpolated line on x values

Once you have the interpolated slope and intercept, generate y values normally.

python
1import numpy as np
2
3def line_y(m, b, x):
4    return m * x + b
5
6x = np.linspace(0, 10, 5)
7m, b = interpolate_line(1.0, 0.0, 3.0, 4.0, 0.5)
8y = line_y(m, b, x)
9
10print(x)
11print(y)

This is useful for plotting, animation, or constructing an intermediate trend line.

Plot all three lines

Here is a full example with Matplotlib:

python
1import numpy as np
2import matplotlib.pyplot as plt
3
4def interpolate_line(m1, b1, m2, b2, t):
5    m = (1 - t) * m1 + t * m2
6    b = (1 - t) * b1 + t * b2
7    return m, b
8
9x = np.linspace(0, 10, 200)
10
11m1, b1 = 1.0, 0.0
12m2, b2 = 3.0, 4.0
13mi, bi = interpolate_line(m1, b1, m2, b2, 0.5)
14
15plt.plot(x, m1 * x + b1, label="line 1")
16plt.plot(x, m2 * x + b2, label="line 2")
17plt.plot(x, mi * x + bi, label="interpolated")
18plt.legend()
19plt.show()

This visualizes the intermediate line clearly.

Interpolate from endpoints instead of coefficients

Sometimes the two lines are not given as equations, but as pairs of points. In that case, interpolate the corresponding endpoints first, then build the intermediate line from those points.

python
1def lerp(a, b, t):
2    return (1 - t) * a + t * b
3
4def interpolate_points(p1, p2, q1, q2, t):
5    start = (lerp(p1[0], q1[0], t), lerp(p1[1], q1[1], t))
6    end = (lerp(p2[0], q2[0], t), lerp(p2[1], q2[1], t))
7    return start, end
8
9
10line_a_start = (0, 0)
11line_a_end = (10, 10)
12line_b_start = (0, 4)
13line_b_end = (10, 20)
14
15print(interpolate_points(line_a_start, line_a_end, line_b_start, line_b_end, 0.5))

This interpretation is often better when the lines come from geometry or drawing contexts rather than algebraic formulas.

Be clear about what "between" means

There is no single universal definition of a line "between" two other lines. The correct interpretation depends on the data model:

  • interpolate slope and intercept when lines are equations
  • interpolate endpoints when lines are geometric segments
  • interpolate y values pointwise when both lines are sampled over the same x grid

For example, if you already have arrays of y values on the same x coordinates, pointwise interpolation may be simplest:

python
1import numpy as np
2
3y1 = np.array([0, 1, 2, 3])
4y2 = np.array([4, 5, 6, 7])
5t = 0.25
6
7y_mid = (1 - t) * y1 + t * y2
8print(y_mid)

That produces an intermediate curve, which is a line only if the inputs themselves are linear and aligned.

Common Pitfalls

The biggest mistake is interpolating line coefficients when the real problem is about line segments in a drawing or plotting context. In those cases, interpolating endpoints is often the correct model.

Another issue is assuming the interpolated line must be parallel to both inputs. That is only true when the original lines share the same slope.

Developers also forget to define the interpolation parameter. A value of t = 0.5 is the midpoint, but values outside the range from 0 to 1 are extrapolation, not interpolation.

Finally, if the lines are represented by sampled data rather than equations, make sure they share the same x domain before interpolating pointwise.

Summary

  • If lines are in slope-intercept form, interpolate slope and intercept directly.
  • If lines are defined by endpoints, interpolate the endpoints and rebuild the line.
  • Use a parameter t from 0 to 1 to move between the two inputs.
  • Be explicit about whether you want coefficient interpolation, geometric interpolation, or pointwise interpolation.
  • The best method depends on how the original lines are represented in your program.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.