How to get the non-overlapping area ??
4 views (last 30 days)
Show older comments
Hi all,
My question is; How to compute [(sum of circles' areas)-(area of overlaps of the circles)]?
Any suggestions for code to do that??
5 Comments
Image Analyst
on 1 Feb 2020
Original question by Erman (in case he deletes it):
Hi all, I have the following code to draw 10 circles;
%%center of circles
X = [0.02 0.44; 0.08 0.58;...
0.22 0.57; 0.3 0.9;...
0.44 0.79; 0.39 0.24;...
0.65 0.19; 0.95 0.11;...
0.93 0.7; 0.82 0.93];
theta = linspace(0,2*pi) ;
C_M=[X(:,1),X(:,2)];
r=[0.18,0.18,0.18,0.18,0.18,0.18,0.13,0.13,0.13,0.13];
for index = 1:length(C_M)
if ~isinf(C_M(index))
x(:,index) =r(index)*cos(theta) ;
y(:,index) =r(index)*sin(theta) ;
xx(:,index)= x(:,index)+C_M(index,1);
yy(:,index)= y(:,index)+C_M(index,2);
%%plot circles
plot(xx,yy,'-b','linewidth',1.5)
end
end
%%x and y axis limits
xlim([-0.2,1.2])
ylim([-0.2,1.2])
grid on
grid minor
Six of these circles have radius of r=0.18 and the rest have radius of r=0.13. So, there are overlapping areas as the following image;
My question is; How to compute [(sum of circles' areas)-(area of overlaps of the circles)], i.e.
Any suggestions for code to do that??
Accepted Answer
Walter Roberson
on 14 Sep 2018
You can calculate lower and upper bounds of the data based upon the starting positions and the radii. Then you can create x and y grids like
N = 500;
xv = linspace(min_x, max_x, N);
dx = xv(2) - xv(1);
yv = linspace(min_y, max_y, N);
dy = yv(2) - yv(1);
[X, Y] = ndgrid(xv, yv);
counts = zeros(N, N, 'uint8');
unscaled_areas = zeros(number_of_circles, 1);
After that you can loop,
mask = (X-x_center(K)).^2 + (Y-y_center(K)).^2 <= radii(K).^2;
unscaled_areas(K) = nnz(mask);
counts(mask) = counts(mask) + 1;
and do that for all of the circles.
Then
scaled_areas = unscaled_areas * (dx * dy);
total_scaled_area = sum(scaled_areas); %which is the first summation in your equation
Now examine the counts matrix. Anywhere location that is 0 had no circles present. Anywhere that is 1 had exactly 1 circle present, but you do not need to count those because size of the circles without overlap was already calculated in scaled_areas. Anywhere that is more than 1 had more than 1 circle present. Count how many locations have value greater than 1, multiply by dx * dy to get the total area occupied by overlaps, which is the second summation from your equations. Subtract appropriate variables to get the overall result of your calculations.
The memory requirements of this go up as you go for finer resolution of boxes, so the more decimal places you need, the more memory it uses, corresponding to increasing N.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!