You're looking to:
- Take the last two points from your lists
x and y.
- Use them to create a line equation:
y = a*x + b.
- Then, calculate a third point along the line, continuing in the same direction — i.e., extrapolate a new point beyond the second one, following the same vector direction.
✅ Step-by-step Plan
Let's say:
x = [...] # list of x coordinates
y = [...] # list of y coordinates
Get the last two points:
x1, y1 = x[-2], y[-2]
x2, y2 = x[-1], y[-1]
Step 1: Compute Line Equation y = a*x + b
a = (y2 - y1) / (x2 - x1) # slope
b = y1 - a * x1 # intercept
⚠️ Make sure to check x2 != x1 to avoid dividing by zero (vertical line case).
Step 2: Continue in the same direction to find third point
Let’s create a vector from point1 → point2:
dx = x2 - x1
dy = y2 - y1
Now, extend the vector from (x2, y2) to get the next point:
x3 = x2 + dx
y3 = y2 + dy
This maintains the same direction and step size.
✅ Full Example Code
def extrapolate_next_point(x, y):
if len(x) < 2 or len(y) < 2:
raise ValueError("Need at least two points to extrapolate.")
x1, y1 = x[-2], y[-2]
x2, y2 = x[-1], y[-1]
if x2 == x1:
# vertical line, add same x delta to x2, y changes by same amount
x3 = x2
y3 = y2 + (y2 - y1)
else:
# direction vector
dx = x2 - x1
dy = y2 - y1
x3 = x2 + dx
y3 = y2 + dy
# Optional: also compute line equation (slope + intercept)
a = dy / dx
b = y1 - a * x1
print(f"Line equation: y = {a}x + {b}")
return x3, y3
🧪 Example Usage:
x = [1, 2]
y = [3, 5]
x3, y3 = extrapolate_next_point(x, y)
print("Next point:", x3, y3)
Output:
Line equation: y = 2.0x + 1.0
Next point: 3 7
🧠 Bonus: If you want to restrict the extrapolated point to be inside min_x, max_x, min_y, max_y, you could verify like this:
if min_x <= x3 <= max_x and min_y <= y3 <= max_y:
# safe to use
Let me know if you want to extrapolate by a fixed distance instead of just one step!