Calculate the next point along a line

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.

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);
}
3 Likes