# 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:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![travis.stdenis](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@travis.stdenis](https://discourse.processing.org/u/travis.stdenis)\
**Post date:** [December 3, 2024, 4:21am UTC](https://discourse.processing.org/t/calculate-the-next-point-along-a-line/45425/1 "2024-12-03T04:21:14Z")

</div>

![20241202_230423](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/b/b/bb159a0918009e11ce78a6373db2ed8f1846adc8.jpeg)  
Per the diagram, I have points A & B and want to know how to get C which is a straight continuation of the —\>AB. This is part of a larger shape I’m drawing so I can’t change the origin, which is what is throwing me off.

---

<div class="post-metadata">

**Author:** ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)\
**Post date:** [December 3, 2024, 5:10am UTC](https://discourse.processing.org/t/calculate-the-next-point-along-a-line/45425/2 "2024-12-03T05:10:04Z")

</div>

Do you know the length of B…C?

---

<div class="post-metadata">

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

</div>

> [@travis.stdenis](#):
>
> This is part of a larger shape I’m drawing so I can’t change the origin,

Is it correct to assume that this origin may move and therefore the other points should move as well?

If that is the case, then you need to set up the points _relative_ to the origin.

```auto
PVector origin = new PVector(width / 2, height / 2);
PVector A_rel = new PVector(-150, 50); // pointing left(ish)
PVector B_rel = new PVector(-50, 120); // pointing down(ish)
PVector A = PVector.add(origin, A_rel);
PVector B = PVector.add(origin, B_rel);

```

_[There’s a whole different approach here using matrix transformations, but let’s skip that for the time being]_

To get to your point C we start off with AB, which is the direction vector from A to B:

`PVector AB = PVector.sub(B, A);`

If we normalize AB (giving it a magnitude of 1) we have a handy vector pointing into the direction of C, i.e. it continues the line with A and B:

`AB.normalize()`

Now you can scale AB to how long you need it to be:

`AB.mult(0.9)`.

Add this AB to B and then you should get C:

`PVector C = PVector.add(B, AB.mult(0.9))`

---

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

```

---

<div class="post-metadata">

**Author:** ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)\
**Post date:** [December 3, 2024, 12:12pm UTC](https://discourse.processing.org/t/calculate-the-next-point-along-a-line/45425/5 "2024-12-03T12:12:38Z")

</div>

> [@quark](#):
>
> This is a common type of problem that can be easily solved using ~~parametric equations~~ this thing I’m not aware of.

Story of my life. 🙃

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 3, 2024, 11:24pm UTC](https://discourse.processing.org/t/calculate-the-next-point-along-a-line/45425/6 "2024-12-03T23:24:58Z")

</div>

Interesting problem!

I’m using C as my camera position in 3D space  
when B is a player (or ball) and A is the center of the scene (center of the marble track / roller coaster or whatever).

Thus you have a nice camera that follows the player on a circle line; you see the player and at the same time most of the scene.

---

<div class="post-metadata">

**Author:** ![travis.stdenis](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@travis.stdenis](https://discourse.processing.org/u/travis.stdenis)\
**Post date:** [December 4, 2024, 2:16am UTC](https://discourse.processing.org/t/calculate-the-next-point-along-a-line/45425/7 "2024-12-04T02:16:54Z")

</div>

dang. you make it look so easy. thanks for this.
