Meshgrid orthogonal to a line in 3D Space
4 views (last 30 days)
Show older comments
In 3D space, how do I generate a set of points, like a meshgrid, orthogonal to a specific straight line passing through its center? This is definitely not a difficult problem, but I'm overcomplicating it somewhere in my brain and related questions didn't help me with the gridding part.
For a line orientated along one axis, what I'm trying to do is this:
X = [-5:5]; Y = ones(size(X)); Z = ones(size(X)); %Define Line
No_points = 5;
plane_x = linspace(-1,1,5); plane_y = linspace(0.9,1.1,5); plane_z = linspace(0.9,1.1,5); %Specify Points
[plane_x,plane_y,plane_z]=meshgrid(plane_x,plane_y,plane_z);
figure;plot3(X,Y,Z,'r-'); hold on; plot3(plane_x(:),plane_y(:),plane_z(:),'k.');
How do I generate the same orthogonal meshgrid of points passing through the origin for a "3D line", e.g:
X = [-5:5]; Y = X; Z = X;
0 Comments
Answers (3)
Matt J
on 20 May 2022
Edited: Matt J
on 20 May 2022
Pick a 3D direction vector for the straight line, e.g.
d=[1,1,1];
Then,
d=d(:)./norm(d);
B=null(d.'); %basis
[x,y,z]=meshgrid(linspace(-5,5,10));
[m,n]=size(x);
res=@(q)reshape(q,1,m,n);
XYZ=B(:,1).*res(x) + B(:,2).*res(y) +d(:).*res(z);
scatter3(XYZ(1,:), XYZ(2,:), XYZ(3,:) );
axis equal
xlabel X, ylabel Y, zlabel Z, view(30,40)
2 Comments
Matt J
on 20 May 2022
Edited: Matt J
on 20 May 2022
There are also ready-made File Exchange tools you can use, like this one
d=[1;1;1]; %direction of line
d=d(:)./norm(d);
gtPlane=planarFit.groundtruth([],d,0); %ground truth plane
b0=[1,0,0];
b1=cross([0,1,0],gtPlane.normal); %Make one sampling direction parallel to x-z plane
b2=[]; %Make the other direction orthogonal to b1
t=linspace(-5,5,10);
XYZ=gtPlane.sample(b0,b1,b2,t,t); %Post-sample the plane
XYZ=num2cell( cell2mat(XYZ)+d(:).*reshape(t,1,1,[]) ,[2,3]); %expand along d
hPost=scatter3(XYZ{1}(:), XYZ{2}(:), XYZ{3}(:));
xlabel X, ylabel Y, zlabel Z; view(30,40); axis equal
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1005765/image.png)
0 Comments
See Also
Categories
Find more on Surface and Mesh Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!