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.
Do you know the length of B…C?
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.
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))
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);
}
Story of my life.
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.
dang. you make it look so easy. thanks for this.