how to find the distance from a point to a line
27 views (last 30 days)
Show older comments
I am ask to find the distance from a point to a line
with this given
(0,0,12); x=4t, y=-2t, z=2t
but I don't know to to encode it to MATLAB
6 Comments
Image Analyst
on 26 Apr 2022
I use a Firefox plug-in/add-on called WebArchives that searched Google plus several other archives. It's a icon on the Firefox toolbar (once installed).
Rik
on 26 Apr 2022
I wonder why a normal search turns up a cached page, but this plug-in doesn't. Otherwise this would make it a lot easier.
(for the record: it is also available for Chromium-based browsers)
Answers (1)
Image Analyst
on 27 Jul 2020
See attached demo
% Get the distance from a point (x3, y3) to
% a line defined by two points (x1, y1) and (x2, y2);
% Reference: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
function distance = GetPointLineDistance(x3,y3,x1,y1,x2,y2)
try
% Find the numerator for our point-to-line distance formula.
numerator = abs((x2 - x1) * (y1 - y3) - (x1 - x3) * (y2 - y1));
% Find the denominator for our point-to-line distance formula.
denominator = sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2);
% Compute the distance.
distance = numerator ./ denominator;
catch ME
callStackString = GetCallStack(ME);
errorMessage = sprintf('Error in program %s.\nTraceback (most recent at top):\n%s\nError Message:\n%s',...
mfilename, callStackString, ME.message);
uiwait(warndlg(errorMessage))
end
return; % from GetPointLineDistance()
end
4 Comments
Rik
on 25 May 2022
@Maite Osaba, latitude and longitude can be very non-linear. If you want accuracy, I would suggest not taking shortcuts.
Image Analyst
on 25 May 2022
@Maite Osaba it is probably good for short distances where the earth is fairly flat. Of course it's the straight line distance and if the points are very widely spaced, you're getting the straight line distances, which would mean boring through the earth, if the earth is significantly curved over that distance. If you want to do this in general, then I'm sure there are probably functions in the Mapping Toolbox to help you though I'm not sure what they are since I don't have that toolbox. (Expand comments to see @Rik's comments just before mine.)
See Also
Categories
Find more on Mapping Toolbox 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!