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?
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 :)
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
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));
}
Answers
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?
@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:
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?
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 can take 6 parameters
or show your code
;-)
or this
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:
thanks I think I did it! ;)