# Check the overlap between circles

11 views (last 30 days)
shdotcom shdotcom on 26 Sep 2018
Edited: Andrei Bobrov on 8 Oct 2018
This code is to check the overlap between circles, is it possible to do this without for loop?
%circles : circles matrix [x, y]; each row represents a circle;
ncircles = size(circles,1);
for i=1:ncircles -1
% distances between circles:
dx = bsxfun(@minus, circles(i,1), circles(i+1,1));
dy = bsxfun(@minus, circles(i,2), circles(i+1,2));
% distance between centers:
dist(i) = sqrt(dx.^2 + dy.^2);
end
% pairs that have overlap:
pairs = find(dist < sumr);
end
Run:
circles = rand(5, 2); % 5 circles

Andrei Bobrov on 26 Sep 2018
Edited: Andrei Bobrov on 8 Oct 2018
EDIT
dxy = sqrt( (xy(:,1) - xy(:,1)').^2 + (xy(:,2) - xy(:,2)').^2 );
lo(1:size(xy,1)+1:end) = false;
intersecting_circles = find(any(lo,2));

#### 1 Comment

shdotcom shdotcom on 26 Sep 2018
Thank you very much for your answer. However, I could not understand the content of out. The output should be the indices of overlapped circles.

R2017a

### Community Treasure Hunt

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

Start Hunting!

Translated by