Finding intermediate points between two cartesian coordinates in 3D

6 views (last 30 days)
Poison Idea fan on 29 Apr 2024
Commented: John D'Errico on 29 Apr 2024
I have two data points p1 and p2 with coordinates (x,y,z).
How would I find all the intermediate points between them?
In 2-dimensions with coordinates (x,y), you fit the data to a line.
Would I need to fit the data to a surface?
If anyone has any resources they could point me to, that would be helpful as well.

John D'Errico on 29 Apr 2024
You cannot find ALL the points between any pair, in any number of dimensions. At best, you can describe the set of all such points, typcially as a line.
Two points define a line, in any number of dimensions. Actually, a line segment. If you extend that segment out, then it becomes a line. NO, two points cannot define a plane. There would be infinitely many planes that pass through two points.
The simplest way to define the line segment is in a parametric form. This allows the points to be vertically related, and still create a line. Thus, I would do this:
% pick two random points
xyz1 = randn(1,3)
xyz1 = 1x3
-0.2066 0.4845 1.1696
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
xyz2 = randn(1,3)
xyz2 = 1x3
-0.9677 0.1770 0.7555
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Now we can define the line segment as a linear combination of those two points. What I would call a convex combination.
lineseg = @(t) xyz1.*(1-t) + xyz2.*t;
lineseg is a function handle, that for any value of the parameter t, returns a point along the line connecting the points. If t lies in the interval [0,1], then the point returned will be between the two. When t==1.2, the point will be exactly in the middle. And when t==0 or t==1, you get the corresponding point. For example...
lineseg(0)
ans = 1x3
-0.2066 0.4845 1.1696
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
lineseg(1)
ans = 1x3
-0.9677 0.1770 0.7555
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
lineseg(0.5)
ans = 1x3
-0.5871 0.3307 0.9626
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
When t is outside of the interval [0,1], you get an extrapolated pooint along that line.
lineseg(2) % extrapolating
ans = 1x3
-1.7287 -0.1305 0.3414
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
And, if t is a (column vector) then you will get a set of points long that line.
xyz = lineseg(linspace(-1,2,100)')
xyz = 100x3
0.5544 0.7920 1.5837 0.5314 0.7827 1.5712 0.5083 0.7734 1.5586 0.4852 0.7641 1.5461 0.4622 0.7547 1.5335 0.4391 0.7454 1.5210 0.4161 0.7361 1.5084 0.3930 0.7268 1.4959 0.3699 0.7175 1.4833 0.3469 0.7081 1.4708
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
plot3(xyz(:,1),xyz(:,2),xyz(:,3),'-r')
hold on
plot3([xyz1(1),xyz2(1)],[xyz1(2),xyz2(2)],[xyz1(3),xyz2(3)],'go')
box on
grid on
As you should see, nothing special as needed. No line fit. And again, hoping to fit a surface or a plane to two points is a meaningless endeaver, since there are infinitely many planes you could form.
Poison Idea fan on 29 Apr 2024
John D'Errico on 29 Apr 2024
You are welcome. The idea of a convex combination is a hugely valuable tool that you will often trip over in mathematics. It allows you to interpolate between any two "things", and dangerously at times if you so desire, to extrapolate. You can even use it to interpolate between two functions.
syms x t
f1 = x^2;
f2 = 1 - x^2;
CC = @(t) f1.*(1-t) + f2.*t;
CC(0)
ans =
CC(1)
ans =
CC(0.25)
ans =
CC(0.5)
ans =
fplot(CC(0),'r')
hold on
fplot(CC(0.25),'g')
fplot(CC(.5),'b')
fplot(CC(0.75),'m')
fplot(CC(1),'k')
fplot(CC(-1),'c')

Categories

Find more on Surface and Mesh Plots in Help Center and File Exchange

R2023a

Community Treasure Hunt

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

Start Hunting!