How to find all points that are a certain distance away from one point.

4 views (last 30 days)
I have a large set of 10,000 points defined as A. How can I find all points in A, that are an exact distance away from a point I?

Answers (3)

Image Analyst
Image Analyst on 8 Mar 2015
Just use the Pythagorean theorem then use logical indexing
allDistances = sqrt(Ax-ix).^2 + (Ay-iy).^2);
% Find points with 0.1 tolerance of 398.453 away from point (ix,iy)
tolerance = 0.1;
farAwayIndexes = abs(allDistances - 398.453) < tolerance;
Why did I use a tolerance when you said "exact"? Because there rarely is "exact" for floating point calculations. Why? See the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F

Matt J
Matt J on 8 Mar 2015
Edited: Matt J on 8 Mar 2015
I'll assume all the points are column vectors
distances = sqrt( sum( bsxfun(@minus,A,I).^2 ,1) );
find(abs(distances-givendistance)<=tolerance)

Star Strider
Star Strider on 8 Mar 2015
Or, you can use the inpolygon function:
xy = rand(1E+4, 2); % Create Random Points
r = 0.20; % Radius
xctr = 0.35; % Center Point ‘x’
yctr = 0.65; % Center Point ‘y’
th = linspace(0, 2*pi)';
circ = [xctr + r*cos(th), yctr + r*sin(th)];
[~,on] = inpolygon(xy(:,1), xy(:,2), circ(:,1), circ(:,2));
on_circ = nnz(on); % Number Of Points On Circle
figure(1)
plot(xy(:,1), xy(:,2), '.r')
hold on
plot(xctr, yctr, 'bp', 'MarkerFaceColor','b')
plot(xy(on,1), xy(on,2), 'gp', 'MarkerFaceColor','g')
hold off

Community Treasure Hunt

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

Start Hunting!