nearest tangent point from Ginput point to line ?
Show older comments
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);
1 Comment
Ramesh Bala
on 22 Oct 2018
Accepted Answer
More Answers (1)
Image Analyst
on 22 Oct 2018
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
Ramesh Bala
on 23 Oct 2018
Ramesh Bala
on 23 Oct 2018
Image Analyst
on 23 Oct 2018
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.
Categories
Find more on Data Exploration in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


