intersection area of 3 or more rectangles
24 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Tobias Eißler
le 5 Mar 2021
Réponse apportée : Matt J
le 6 Mar 2021
hi all, I have multiple rectangles defined by rectangle()- function. I now want to find the intersection area of 3 rectangles (I know that for 2 rectangles rectint works). Alternatively it might be an option to find the intersection area of the intersection (rectangle 1, rectangle 2) with the rectangle 3.
Is there a funtion or simple solution for this problem?
Thanks!
0 commentaires
Réponse acceptée
Adam Danz
le 5 Mar 2021
Modifié(e) : Adam Danz
le 5 Mar 2021
This approach is inspired by the even-odd rule for determining if a point is inside a polygon. If the max left-edge value is smaller than the min right-edge value and if the max bottom-edge value is smaller than the min top-edge value, then all rectangles overlap. This only works when rectangle edges are parallel to the axes (ie, not rotated beyond 90 deg intervals).
Input:
r: an array of handles of rectangle objects. Add as many as you'd like.
Output:
rectDim: 1x4 vector; contains the [left, bottom, width, height] of the overlapping rectangle. If there isn't an overlapp of all rectangles, it will be all NaN values.
Variations:
If you'd rather work with [left, bottom, right, top] values directly rather than working with rectangle objects, create an nx4 matrix of those values for n rectangles, name it LBRT and remove the first two sections of code.
If you'd rather work with [left, bottom, width, height] values directly rather than working with rectangle objects, create an nx4 matrix of those values for n rectangles and convert to bounds using the LBRT conversion in the second section of code.
% Generate n rectangels with random positions and sizes and
% store their handles in vector 'r'.
cla()
hold on
r(1) = rectangle('position',rand(1,4).*[1,1,2,2],'EdgeColor','g','LineWidth',3);
r(2) = rectangle('position',rand(1,4).*[1,1,2,2],'EdgeColor','m','LineWidth',3);
r(3) = rectangle('position',rand(1,4).*[1,1,2,2],'Edgecolor','b','LineWidth',3);
% r(n) = rectangel(....) add as many as you want!
%% Find overlap of all rectangles
% Convert nx4 matrix [left,bottom,width,height] -> [left,bottom,right,top]
LBRT = vertcat(r.Position);
LBRT(:,3) = sum(LBRT(:,[1,3]),2);
LBRT(:,4) = sum(LBRT(:,[2,4]),2);
% Test Left|Right edge intersections
[~,LRidx] = sort([LBRT(:,1);LBRT(:,3)]);
LRtest = max(LRidx(1:numel(LRidx)/2)) < min(LRidx(numel(LRidx)/2+1:end));
% Test Bottom|Top edge intersections
[~,BTidx] = sort([LBRT(:,2);LBRT(:,4)]);
BTtest = max(BTidx(1:numel(BTidx)/2)) < min(BTidx(numel(BTidx)/2+1:end));
if LRtest && BTtest
% All rectangles overlap. The overlap is defined by the max left side,
% min right side, same with bottom/top. Fill in overlap.
overlapPos = [max(LBRT(:,1)), max(LBRT(:,2)), min(LBRT(:,3)), min(LBRT(:,4))];
rectDim = [overlapPos(1:2), overlapPos(3:4)-overlapPos(1:2)];
rectangle('Position', rectDim, 'FaceColor',[.0 0 0 .3])
else
rectDim = nan(1,4);
end
3 commentaires
Adam Danz
le 5 Mar 2021
I tried with 3, 4 and 5 rectangles and it works just as in the GIF image. It should work with any number of rectangles, I believe.
Voir également
Catégories
En savoir plus sur Surface and Mesh Plots dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!