#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# drawing line that goes through 2 points (PVector)

Hi, I have 2 PVector with their coordinates and I want to draw a line between them. not just the distance but a longer line (basically the only rect that goes through them). How can I do that?

thanks

Tagged:

• Get the slope of the line by finding the x distance and y distance of the two points. Then use that slope to draw a line as long as you want.

• like this?

``````int state = 0;

final color red = color(255, 2, 2);
final color black = color(0);

PVector p1= new PVector ();
PVector p2= new PVector ();
PVector p3= new PVector ();
//
// ---------------------------------------------------------------
//
void setup()
{
// init
size(800, 600);
} // func
//
//
void draw()
{
switch (state) {
case 0:
background(255);

p1.set(200, 200);
p2.set(mouseX, mouseY);

strokeWeight(2);
linePVector (p1, p2, red);
strokeWeight(1);

p3.set(p1.x-p2.x, p1.y-p2.y);
p2.sub(p3);
linePVector (p1, p2, black);
break;

case 1:
//
break;

case 2:
//
break;

default:
//
break;
} // switch
} // func
//

// ----------------------------------------------

void linePVector (PVector p1, PVector p2, color colorLine) {
stroke(colorLine);
line (p1.x, p1.y,
p2.x, p2.y);
}

// ==============================
``````
• edited March 2015

@chrisir..honestly I have no idea what you did then I don't know if it's what I need.. :D coul you please explain what you did? what is this:

``````p3.set(p1.x-p2.x, p1.y-p2.y);
p2.sub(p3);
``````

and why you add/subtract from the vectors? also, is the line in 3 dimension?

thanks

• I just did what KevinWorkman said

I get the slope by finding the x distance and y distance of the two points

I store it in p3 (we take the distance there twice): `p3.set(p1.x-p2.x, p1.y-p2.y)`

Then I once add the slope to the one initial point and substract it from the 2nd initial point thus making the line longer using the same slope: `p1.add(p3); p2.sub(p3);`

PVector

PVector is a special data structure in processing (and a special concept in math) and when you look at the reference you'll see there are some special commands for PVectors like add and sub

It's only 2D

It's only 2D.

Otherwise size command would look different and all PVector stuff would have three components (xyz) not two (xy)

;-)

• thaks for the explanation..btw..I think I wasn't clear in my request..I need it in 3 dimension..in 2 dimension I can simply draw a normal line I guess..instead, I need to draw a line that goes through 2 pvector (each is an array x,y,z) in a 3d space.. thanks for your time :)

• Get the slope of the line by finding the x, y, and z distance of the two points. Then use that slope to draw a line as long as you want.

Can you attempt the above in your code and post an MCVE of where you get stuck?

• edited March 2015 Answer ✓

no, you wasn't clear in the request..

you wrote

• not just the distance but a longer line (it doesn't say 3D and suggest a longer line than between the 2 points...)

• basically the only rect that goes through them

Why a rectangle!?

Besides...

Besides... when the 2 PVectors are 3D then they have 3 coords xyz as said...

so then the code is just

``````line (p1.x,p1.y,p1.z,
p2.x,p2.y,p2.z);
``````

line can take 6 parameters

;-)

• or this

``````void MyBox(float x1, float y1, float z1,
float x2, float y2, float z2,
float weight,
color strokeColour)
// was called drawLine; programmed by James Carruthers
// see http://processing.org/discourse/yabb2/YaBB.pl?num=1262458611/0#9
{
PVector p1 = new PVector(x1, y1, z1);
PVector p2 = new PVector(x2, y2, z2);
PVector v1 = new PVector(x2-x1, y2-y1, z2-z1);
float rho = sqrt(pow(v1.x,2)+pow(v1.y,2)+pow(v1.z,2));
float phi = acos(v1.z/rho);
float the = atan2(v1.y,v1.x);
v1.mult(0.5);

pushMatrix();
translate(x1,y1,z1);
// normally just   translate(v1.x, v1.y, v1.z);
translate(v1.x-300, v1.y-500, v1.z-850);
rotateZ(the);
rotateY(phi);
noStroke();
fill(strokeColour);
// box(weight,weight,p1.dist(p2)*1.2);
box(weight,weight,p1.dist(p2)*1.2);
popMatrix();
}
``````
• ok thanks..I think I got it..sorry for my mispelling (rect is similar at how we call a straight line in my language)..got another problem now..as I'm using kinect..I won't be able to use P3D to draw this 3d line..as with P3D the kinect lib won't work..thanks anyway

• use OPENGL ?

• it throws error too.. :( createDepth: can't set videoMode

• when kinect related please start a new question in the kinect setcion of this forum?

• yes in fact it wasn't my intention to discuss that here :) thanks!

• sorry, I don't know the answer...

;-)

Maybe this answers your question? It draws a line from some point "a" in the direction of another point "b". Then it draws another line in the opposite direction:

``````PVector a, b;

void setup() {
size(800, 800, P3D);
noFill();

randomPoints();
}

void draw() {
background(255);

// This is just to see points a and b
displayPoints();

// Calculate the direction from a to b
PVector t = b.get();
t.sub(a);
t.normalize();

// An arbitrary length
float lineLength = 1000.0;

// Draw a line that goes from a to b with a length of lineLength
line(a.x, a.y, a.z, a.x+t.x*lineLength, a.y+t.y*lineLength, a.z+t.z*lineLength);

// Get the opposite direction
t.mult(-1.0);

// Draw another line in the opposite direction
line(a.x, a.y, a.z, a.x+t.x*lineLength, a.y+t.y*lineLength, a.z+t.z*lineLength);
}

void mousePressed() {
randomPoints();
}

void displayPoints() {
pushMatrix();
translate(a.x, a.y, a.z);
box(10);
popMatrix();

pushMatrix();
translate(b.x, b.y, b.z);
box(10);
popMatrix();
}

void randomPoints() {
a = new PVector(random(width), random(height), random(-400.0, -800.0));
b = new PVector(random(width), random(height), random(-400.0, -800.0));
}
``````
• thanks I think I did it! ;)