How to find the equation of a straight line in 3D?

1 view (last 30 days)
If I have data points (x1,x2), (y1,y2) and (z1,z2) how can I find the equation between these points(it is a straight line)? Thank you!

Accepted Answer

David Goodmanson
David Goodmanson on 20 Sep 2018
Edited: David Goodmanson on 20 Sep 2018
Hi Federico,
Are you looking for a parametric description? It's constructive.
p1 = rand(1,3)
p2 = rand(1,3)
u = (p2-p1)/norm(p2-p1); % unit vector, p1 to p2
d = (-1:.1:1)'; % displacement from p1, along u
xyz = p1 + d*u;
plot3(xyz(:,1),xyz(:,2),xyz(:,3),'o-')
  5 Comments
David Goodmanson
David Goodmanson on 22 Sep 2018
Hi Federico,
It's certainly best not to accept an answer unless you know what it is doing. Just because the code pops out an answer is not justification enough.
As far as d is concerned, draw two points p1 and p2, separated by a distance of, say, 3. Draw a vector of length 1 starting at p1 and pointing toward p2. That's u*. Then the vector p1 + d*u is a point d/3 of the way from p1 to p2. Varying d traces out the line. Values d>3 go past point p2 and negative values of d create points heading away from p2.
*u is the unit vector pointing in the direction (p2-p1) and is its own entity. To create u, the base of u is at p1, but later for other purposes u could be moved to any location.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 20 Sep 2018
Edited: Matt J on 20 Sep 2018
If
p1=[x1,y1,z1];
p2=[x2,y2,z2];
then equations for the line are
(x-p1)*null(p2-p1)=0 %equations in x
  9 Comments
AFHG
AFHG on 20 Sep 2018
Edited: AFHG on 20 Sep 2018
Suppose there are particles moving. There is a loop which changes the x,y,z position values each time it runs (as time passes). i need to form the equation of the line from the precedent and current coordinate which changes when the direction of the particle changes. I need this equation to perform further calculations within the loop.
James Tursa
James Tursa on 20 Sep 2018
Edited: James Tursa on 20 Sep 2018
Please show us your code, or a small sample of it, so we can see how you intend to use these linear equations downstream in your code. E.g., are you going to use them to determine if a particle has "turned"? To interpolate or predict particle positions or collisions? Or ...?

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!