Check the overlap between circles

3 ビュー (過去 30 日間)
shdotcom shdotcom
shdotcom shdotcom 2018 年 9 月 26 日
編集済み: Andrei Bobrov 2018 年 10 月 8 日
This code is to check the overlap between circles, is it possible to do this without for loop?
function pairs = checkOverlap(circles, rads)
%circles : circles matrix [x, y]; each row represents a circle;
rads : radius of each 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));
sumr(i) = bsxfun(@plus, rads(i,1), rads(i+1,1));
% 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
rads = rand(5,1); % 5 radius
Isoverlap = checkOverlap(circles, rads);

採用された回答

Andrei Bobrov
Andrei Bobrov 2018 年 9 月 26 日
編集済み: Andrei Bobrov 2018 年 10 月 8 日
EDIT
rad2 = rads(:) + rads(:)';
dxy = sqrt( (xy(:,1) - xy(:,1)').^2 + (xy(:,2) - xy(:,2)').^2 );
lo = dxy <= rad2;
lo(1:size(xy,1)+1:end) = false;
intersecting_circles = find(any(lo,2));
  1 件のコメント
shdotcom shdotcom
shdotcom shdotcom 2018 年 9 月 26 日
編集済み: shdotcom shdotcom 2018 年 9 月 26 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by