How to find the distance of a point to a line (knowing all the points)?

63 次查看(过去 30 天)
Hi!!
I have a line made of two points BA and a point P. I would like to find the distance from P to the perpendicular of BA and to the parallel of BA.
Do you know if there is any fuction aready doing it?
I attacht a picture to make it easier to understand.
Thanks in advance.

回答(1 个)

Image Analyst
Image Analyst 2020-3-31
See my demo. The GetPointLineDistance() function you want is in there.
% 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
errorMessage = sprintf('Error in program %s.\nError Message:\n%s',...
mfilename, ME.message);
uiwait(errordlg(errorMessage));
end
return; % from GetPointLineDistance()
end
It's down near the bottom. The rest is stuff to have you draw the three points and plot them to make a nice fancy demo. Here is the full blown demo script. Just copy and paste into a new script in MATLAB:
% Finds the point line distance from 3 points the user clicked, plus finds the intersection point on the main line.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
axis([-10, 10, -10, 10]);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Set up the axis to go from -10 to 10
xlim([-10, 10]);
ylim([-10, 10]);
axis square
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
grid on;
axis equal
% Let user click 3 points.
[x, y] = ginput(3)
hold on;
% Plot the points
plot(x, y, 'b.', 'MarkerSize', 40);
% Draw lines between them all
plot([x; x(1)], [y; y(1)], 'b-', 'LineWidth', 2);
grid on;
xlim([-10, 10]);
ylim([-10, 10]);
axis square
text(x(1), y(1), ' 1', 'FontSize', fontSize, 'Color', 'r', 'FontWeight', 'bold');
text(x(2), y(2), ' 2', 'FontSize', fontSize, 'Color', 'r', 'FontWeight', 'bold');
text(x(3), y(3), ' 3', 'FontSize', fontSize, 'Color', 'r', 'FontWeight', 'bold');
% Get theta between the line from point 1 to point 2, and the x axis
theta12 = atan2d(y(2) - y(1), x(2)-x(1))
theta13 = atan2d(y(3) - y(1), x(3)-x(1))
% Get the delta theta
deltaTheta = theta13 - theta12
distance = GetPointLineDistance(x(3),y(3),x(1),y(1),x(2),y(2))
% Get angle from a axis to point 5.
theta15 = theta12 - deltaTheta
% Get distance from point 1 to point 3
d13 = sqrt((x(1)-x(3))^2 + (y(1) - y(3))^2)
% Get (x5, y5)
x(5) = d13 * cosd(theta15) + x(1)
y(5) = d13 * sind(theta15) + y(1)
% Plot it
hold on;
plot(x(5), y(5), 'r.', 'MarkerSize', 40, 'LineWidth', 2);
line([x(3), x(5)], [y(3), y(5)], 'Color', 'r', 'LineWidth', 2);
line([x(1), x(5)], [y(1), y(5)], 'Color', 'r', 'LineWidth', 2);
xlim([-10, 10]);
ylim([-10, 10]);
axis square
text(x(5), y(5), ' 5', 'FontSize', fontSize, 'Color', 'r', 'FontWeight', 'bold');
% Get (x4, y4) which is on the line from point 1 to point 2.
x(4) = mean([x(3), x(5)])
y(4) = mean([y(3), y(5)])
% Plot it
hold on;
plot(x(4), y(4), 'r.', 'MarkerSize', 40, 'LineWidth', 2);
text(x(4), y(4), ' 4', 'FontSize', fontSize, 'Color', 'r', 'FontWeight', 'bold');
viscircles([x(1), y(1)], d13);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
% 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
errorMessage = sprintf('Error in program %s.\nError Message:\n%s',...
mfilename, ME.message);
uiwait(errordlg(errorMessage));
end
return; % from GetPointLineDistance()
end
  4 个评论
Daniel Jara
Daniel Jara 2020-3-31
I have the three points. I create BA line and I want to know the distances from C point to the BA line being the distance perpendicular to this line (as you already exposed) and the distance of the C point to the BA line, but being this distance line parallel to the X axis. I upload a picture. Thanks a lot!
Image Analyst
Image Analyst 2020-3-31
You forgot to label point C. Where is it? What's the blue circle? Anyway, the y value of the blue circle is Yp. And, if you need it you can get the x value just from the equation of the line.
coefficients = polyfit([xa,xb], [ya,yb], 1);
y = coefficients(1) * x + coefficients(2);
% Plug in yp to get xBlue
xBlue = (yp - coefficients(2)) / coefficients(1);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by