Using a tolerance as KSSV mentioned does the job.
x=[0 1]; y=[0 1];
[xp,yp]=ginput;
epsilon = .1;
dx = x - xp; dy = y - yp;
if any(hypot(dx, dy) < epsilon) disp('Close') else disp('Far') end
Using a tolerance as KSSV mentioned does the job.
x=[0 1]; y=[0 1];
[xp,yp]=ginput;
epsilon = .1;
dx = x - xp; dy = y - yp;
if any(hypot(dx, dy) < epsilon) disp('Close') else disp('Far') end
This example should have the answers to your problems:
% Values that can be changed in the code: % d, Xu, Yu
% original points X = [0 1]; Y = [0 1];
% error as an absolute value d = 0.1;
% data for P(3) and P(4) below % indexing vectors for X idx1 = false(1,8); idx1(1,[1,4,5,end]) = true; idx2 = ~idx1; % error area X values Xd = repelem(X,4); Xd(idx1) = Xd(idx1)-d; Xd(idx2) = Xd(idx2)+d;
% indexing vectors for Y idx3 = false(1,8); idx3(1,[1,2,5,6]) = true; idx4 = ~idx3; % error area X values Yd = repelem(Y,4); Yd(idx3) = Yd(idx3)+d; Yd(idx4) = Yd(idx4)-d;
% user defined vector Xu = [0.05 1.09]; Yu = [0.1 1.05];
% deciding whether baseline vector and user defined vector % are within error 'd' from each other Xb = Xd([1:2;5:6]); Yb = Yd([3,2;7,6]);
Xcompare = (Xb(:,1) <= Xu') & (Xb(:,2) >= Xu'); Ycompare = (Yb(:,1) <= Yu') & (Yb(:,2) >= Xu'); result = all([Xcompare; Ycompare]);
f(1) = figure; p(1) = plot(X,Y,'ok--'); hold on p(2) = fill(Xd(1:4),Yd(1:4),[0 1 0]); p(3) = fill(Xd(5:end),Yd(5:end),[0 1 0]); p(4) = plot(Xu,Yu,'rx--'); grid on
if result title('In'); else title('Out'); end
legend([p(1), p(2), p(4)],... {'baseline', 'error area', 'user input'},... 'Location','best');
set([p(2) p(3)],... 'facealpha', 0.2,... 'LineStyle', 'none');
The output:
Read about ismembertol.
Or you can add some tolerance (may be 10^-3) to (x,y) and then check with (xp,yp).
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!