
How to discover intersecting circles
11 views (last 30 days)
Show older comments
I have two types of intersecting circles in images, created by the Matlab imfindcircles function, which returns a matrix of centroid (x, y) and a vector with the radii of the circles (r):
1- Concentric circles;
2 - Circles intersecting.
I need to identify all of these cases and apply two functions to treat these intersections, one for case 1 (function1) and one for case 2 (function2). These functions will have as parameters a matrix containing the centroides and the radii of the circles in each case. I just need a program that reports the parameters of the two functions and when to call one or the other.

0 Comments
Accepted Answer
Image Analyst
on 19 Aug 2020
Try this:
% Create sample data.
numCircles = 10;
xy = rand(numCircles, 2);
radii = 0.1 * rand(numCircles, 1) + 0.1;
viscircles(xy, radii);
% Label them so we know what circle is what.
for k = 1 : numCircles
text(xy(k, 1), xy(k, 2), num2str(k));
end
axis square;
% See if any intersect
for k = 1 : numCircles
% Compute the distance between centers.
distances = sqrt((xy(k, 1) - xy(:, 1)).^2 + (xy(k, 2) - xy(:, 2)) .^2);
sumOfRadii = radii(k) + radii(:);
intersectingIndexes = distances < sumOfRadii;
% Don't compare circle to itself. Of course that one intersects!
intersectingIndexes(k) = false;
% Convert to linear indexes
intersectingIndexes = find(intersectingIndexes);
for k2 = 1 : length(intersectingIndexes)
fprintf('Circle %d intersects with circle #%d.\n', k, intersectingIndexes(k2));
end
end

You see
Circle 1 intersects with circle #2.
Circle 1 intersects with circle #5.
Circle 1 intersects with circle #7.
Circle 2 intersects with circle #1.
Circle 2 intersects with circle #5.
Circle 3 intersects with circle #7.
Circle 3 intersects with circle #9.
Circle 5 intersects with circle #1.
Circle 5 intersects with circle #2.
Circle 6 intersects with circle #10.
Circle 7 intersects with circle #1.
Circle 7 intersects with circle #3.
Circle 8 intersects with circle #9.
Circle 9 intersects with circle #3.
Circle 9 intersects with circle #8.
Circle 10 intersects with circle #6.
and it's trivial to find out if one circle is completely contained within another. See if you can do it.
0 Comments
More Answers (1)
KSSV
on 19 Aug 2020
- You have center C and Radius R of circles. You can form the coordinates i.e (x,y) points of circle using:
m = 500 ;
th = linspace(0,2*pi,m) ;
x = C(1)+R*cos(th) ;
y = C(2)+R*sin(th) ;
2. You can get the point of intersections using Interx: https://www.mathworks.com/matlabcentral/fileexchange/22441-curve-intersections
3. You can find whether a circle is lying inside other circle using inpolygon.
4. You can get the nearest points to a given point using knnsearch.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!