# Calculate the next point along a line

**URL:** <https://discourse.processing.org/t/calculate-the-next-point-along-a-line/45425>\
**Category:** Coding Questions\
**Created:** [December 3, 2024, 4:21am UTC](https://discourse.processing.org/t/calculate-the-next-point-along-a-line/45425 "2024-12-03T04:21:14Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 3, 2024, 10:09am UTC](https://discourse.processing.org/t/calculate-the-next-point-along-a-line/45425/4 "2024-12-03T10:09:04Z")

</div>

This is a common type of problem that can be easily solved using parametric equations. The code below provides a simple function that will calculate the extension point for any line and any extension amount. This is the output produced by the sketch.

 ![lineex](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/1/f1d35326e980ec4619b6624c28add41ea61afb8f.png)

```auto
PVector a, b, c;

void setup() {
  size(300, 300);
  a = new PVector(90, 50);
  b = new PVector(200, 150);
  c = extendedLinePoint(a, b, 60);
}

void draw() {
  background(255, 255, 200);
  stroke(192, 0, 0);
  fill(192, 0, 0);
  strokeWeight(2);
  line(b.x, b.y, c.x, c.y);
  ellipse(c.x, c.y, 6, 6);
  text("C", c.x, c.y - 10);
  stroke(0, 128, 0);
  fill(0, 128, 0);
  line(a.x, a.y, b.x, b.y);
  ellipse(a.x, a.y, 6, 6);
  ellipse(b.x, b.y, 6, 6);
  text("A", a.x, a.y - 10);
  text("B", b.x, b.y - 10);
}

/*
Given the start and end points of a line caluculate the coordinates
 for the point that extends the line by a predetermined distance.
 */
PVector extendedLinePoint(PVector v0, PVector v1, float dist) {
  float len = dist(v0.x, v0.y, v1.x, v1.y);
  if (len == 0) {
    println("ERROR cannot extend a zero length line");
    return null;
  }
  float t = 1 + dist / len;
  float x = v0.x + t * (v1.x - v0.x);
  float y = v0.y + t * (v1.y - v0.y);
  return new PVector(x, y);
}

```

---

_[View the full topic](https://discourse.processing.org/t/calculate-the-next-point-along-a-line/45425)._
