nearest tangent point from Ginput point to line ?

I'm trying to get the nearest by creating a ginput and then ginput point finds the nearest point to the line.
x = [0,20]
y= [20,50]
plot(x,y)
[x,y] = ginput(1);
h1 = text(x,y,'o', ...
'HorizontalAlignment','center', ...
'Color', [1 0 0], ...
'FontSize',8);

 Accepted Answer

My FEX submission should help here. Unlike what the documented behavior should be, pt is actually not extended to 3D automatically, so you'll have to do that yourself until I update the file. The code below should work as intended.
x = [0,20];
y= [20,50];
v1=[x(1) y(1) 0];
v2=[x(2) y(2) 0];
plot(x,y)
[x,y] = ginput(1);
pt=[x(:),y(:),zeros(numel(y),1)];
h1 = text(x,y,'o', ...
'HorizontalAlignment','center', ...
'Color', [1 0 0], ...
'FontSize',8);
distance=point_to_line_distance(pt, v1, v2)

11 Comments

Thanks Rik,I saw your code but it's going to lower axis instead of projection.
When I run this, it runs exactly as expected: it calculates the distance to the plotted line. What code are you using to plot those extra dots? I suspect they might be the problem.
%%this is the code ; %% just added a plot to get the distance values
x = [0,20];
y= [20,50];
v1=[x(1) y(1) 0];
v2=[x(2) y(2) 0];
plot(x,y)
hold on
[x,y] = ginput(1);
pt=[x(:),y(:),zeros(numel(y),1)];
h1 = text(x,y,'o', ... 'HorizontalAlignment','center', ... 'Color', [1 0 0], ... 'FontSize',8);
distance = point_to_line_distance(pt, v1, v2)
plot (distance,'*')
Thanks Rik for the reply.
I'm expecting something like this ,like a shorter distance point near to line or projected to line.
My function calculates the distance, not the projected point on the line.
Ah! but it should be somewhere near to the ginput point right, not to the axis?
plot(distance,'*') will take the distance as the y-value and take the element number (i.e. 1:numel(distance)) as the x-value. So I actually don't expect it to be close to either the line or the ginput point.
Thanks Rik , So your's is like
d1 = sqrt ((x3 - x).^2 + (y3-y).^2);
Also from your code by using a random ginput, I got random distance values as 5 10 15 is that x,y,z or ?
Actually my function is equivalent to the line below (as the description states as well):
distance=norm(cross(v1-v2,pt-v2))/norm(v1-v2)
The distance you get will depend on which point you randomly select. Look at your axes, do you believe the distance output? The few I tested I believe the output to be correct.
The code below should project your point to the line
x = [0,20];
y= [20,50];
figure(1),clf(1)
plot(x,y),hold on
P=ginput(1);%'random' point
%assign shorthand
AP=P-A;AB=A-B;A=[x(1) y(1)];B=[x(2) y(2)];
%calculate projection
Pp=A+(AB/norm(AB))*dot(AP,AB)/norm(AB);
%plot P and P'
plot(P(1),P(2),'o')
plot(Pp(1),Pp(2),'*')
Did this suggestion solve your problem? If so, please consider marking it as accepted answer. It will make it easier for other people with the same question to find an answer. If this didn't solve your question, please comment with what problems you are still having.
Perhaps have your code draw a line from the point perpendicularly to the closest point on the line, then run your code, and save a screenshot of the figure and upload it. Maybe if he sees that he will accept it.

Sign in to comment.

More Answers (1)

Use sqrt():
uiwait(helpdlg('Click one point.'));
[xUser, yUser] = ginput(1);
distances = sqrt((xUser - xLine).^2 + (yUser - yLine) .^ 2);
[minDistance, indexOfMin] = min(distances);
hold on;
% Put a marker on the line.
plot(xLine(indexOfMin), yLine(indexOfMin), 'r*');
% Draw a line from the user-clicked point to the point on the line.
line([xUser, xLine(indexOfMin)], [yUser, yLine(indexOfMin)], 'LineWIdth', 2, 'Color', 'r');

3 Comments

Thanks Image analyst I tried to add it to the code ,but it provides different result as it's taking to 0,0 or even lower axis value,I'm bascially trying to project the point to the line @ shorter distance location
x = [0,20]
y= [20,50]
plot(x,y)
uiwait(helpdlg('Click one point.'));
[xUser, yUser] = ginput(1);
distances = sqrt((xUser - x).^2 + (yUser - y) .^ 2); [minDistance, indexOfMin] = min(distances);
hold on;
% Put a marker on the line.
plot(x(indexOfMin), y(indexOfMin), 'r*');
% Draw a line from the user-clicked point to the point on the line.
line([xUser, x(indexOfMin)], [yUser, y(indexOfMin)], 'LineWIdth', 2, 'Color', 'r');
It's like the point getting projected to the line @ shortest travel distance.
OK, I thought you had a bunch of points along a line, not just two. In that case you'll have to use the point-to-line distance formula, which are readily avalable all over the web.

Sign in to comment.

Categories

Products

Asked:

on 22 Oct 2018

Commented:

on 30 Oct 2018

Community Treasure Hunt

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

Start Hunting!