Calculating the area of overlapping polgyons
53 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Louis Helledie
el 4 de Ag. de 2023
Comentada: Louis Helledie
el 4 de Ag. de 2023
I would like to find a way to calculate the area different polygons based on their place in a hierarchy such that if a polygon overlaps with any prior defined polygons, it will obtain the overlapping area. This overlapping area should also be subtracted to the initial area of the prior defined polygon, so the total area will star correct. I have provided a simple example that tries to visualize my problem.
Here, polygon 3 is last defined (in the front) and should have an area of 1. Polygon 2 should have an initial area of 4 square units, but because polygon 3 overlaps with it, it should only have an area of 3 square units. Polygon 1 (which is farthest in the back because it is defined first) has an initial area of 9 square units, but should be 5 square units as polygon 3 obtains 1 square unit and polygon 2 obtains 3 square units (9-3-1=5)
The code I am using give me an area of 4 square units for polygon 1, and it must be because both polygon 2 and polygon 3, in the part where they both overlaps with polygon 1, are subtracted from polygon 1. The part where they both overlap is the region covered by the coordinates (1,1) (2,1), (2,2), and (1,2).
Below is the code i use provided. Hope any of you can help me with this problem.
% Clear the workspace and close all figures
clear all;
close all;
clc;
% Define the vertices of the polygons
polygon1 = [0, 0; 3, 0; 3, 3; 0, 3];
polygon2 = [0.5, 0.5; 2.5, 0.5; 2.5, 2.5; 0.5, 2.5];
polygon3 = [1, 1; 2, 1; 2, 2; 1, 2];
polygons = {polygon1, polygon2, polygon3};
% Calculate the areas of the polygons
num_polygons = numel(polygons);
areas = zeros(num_polygons, 1);
for i = num_polygons:-1:1
intersect_area = 0;
for j = (i + 1):num_polygons
[xi, yi] = polybool('intersection', polygons{i}(:, 1), polygons{i}(:, 2), polygons{j}(:, 1), polygons{j}(:, 2));
intersect_area = intersect_area + polyarea(xi, yi);
end
areas(i) = polyarea(polygons{i}(:, 1), polygons{i}(:, 2)) - intersect_area;
end
% Display the calculated areas
for i = 1:num_polygons
fprintf('Polygon %d Area: %f square units\n', i, areas(i));
end
% Plot the polygons with different colors and FaceAlpha 1
figure;
hold on;
colors = jet(num_polygons);
for i = 1:num_polygons
fill(polygons{i}(:, 1), polygons{i}(:, 2), colors(i, :), 'FaceAlpha', 1);
end
hold off;
axis equal;
title('Overlapping Squares');
xlabel('X-axis');
ylabel('Y-axis');
% Update the legend to include all polygons
legend('Polygon 1', 'Polygon 2', 'Polygon 3', 'Location', 'Best');
Thank you in advance,
Louis H
0 comentarios
Respuesta aceptada
Bruno Luong
el 4 de Ag. de 2023
Editada: Bruno Luong
el 4 de Ag. de 2023
Is it what you want? (I'm not sure about the desired output if polygons are not hierachical included to each other, see variant in comment bellow)
% Define the vertices of the polygons
polygon1 = [0, 0; 3, 0; 3, 3; 0, 3];
polygon2 = [0.5, 0.5; 2.5, 0.5; 2.5, 2.5; 0.5, 2.5];
polygon3 = [1, 1; 2, 1; 2, 2; 1, 2];
polygons = {polygon1, polygon2, polygon3};
Q = polyshape(zeros(0,2));
n = length(polygons);
A = zeros(1, n);
for k=length(polygons):-1:1
Pk = polyshape(polygons{k});
A(k) = area(subtract(Pk, Q));
Q = Pk;
end
for k=1:n
fprintf('Addition area poygonal %d = %f\n', k, A(k));
end
2 comentarios
Bruno Luong
el 4 de Ag. de 2023
% Define the vertices of the polygons
polygon1 = [0, 0; 3, 0; 3, 3; 0, 3];
polygon2 = [0.5, 0.5; 2.5, 0.5; 2.5, 2.5; 0.5, 2.5];
polygon3 = [1, 1; 3, 1; 3, 2; 1, 2]; % different examples here, non inclusion
polygons = {polygon1, polygon2, polygon3};
Q = polyshape(zeros(0,2));
n = length(polygons);
A = zeros(1, n);
P = cell(1,n);
for k=length(polygons):-1:1
Pk = polyshape(polygons{k});
A(k) = area(subtract(Pk, Q));
P{k} = Pk;
Q = union(Q, Pk); % variant
end
hold on
for k=1:n
plot(P{k},'FaceAlpha', 0.5)
end
for k=1:n
fprintf('Addition area poygonal %d = %f\n', k, A(k));
end
Más respuestas (1)
Davide Masiello
el 4 de Ag. de 2023
How about this?
% Clear the workspace and close all figures
clear,clc
% Define the vertices of the polygons
polygon1 = [0, 0; 3, 0; 3, 3; 0, 3];
polygon2 = [0.5, 0.5; 2.5, 0.5; 2.5, 2.5; 0.5, 2.5];
polygon3 = [1, 1; 2, 1; 2, 2; 1, 2];
polygons = {polygon1, polygon2, polygon3};
% Calculate the areas of the polygons
num_polygons = numel(polygons);
areas = zeros(num_polygons, 1);
for i = num_polygons:-1:1
areas(i) = polyarea(polygons{i}(:, 1), polygons{i}(:, 2));
end
areas = abs(diff([areas;0]));
% Display the calculated areas
for i = 1:num_polygons
fprintf('Polygon %d Area: %f square units\n', i, areas(i));
end
% Plot the polygons with different colors and FaceAlpha 1
figure;
hold on;
colors = jet(num_polygons);
for i = 1:num_polygons
fill(polygons{i}(:, 1), polygons{i}(:, 2), colors(i, :), 'FaceAlpha', 1);
end
hold off;
axis equal;
title('Overlapping Squares');
xlabel('X-axis');
ylabel('Y-axis');
% Update the legend to include all polygons
legend('Polygon 1', 'Polygon 2', 'Polygon 3', 'Location', 'Best');
3 comentarios
Bruno Luong
el 4 de Ag. de 2023
@Louis Helledie "Here, it should be affected by the overlapping area, and therefore obtain an area less than 5 square units."
You didn't clearly state it in your original question. See the comment under my answer.
Ver también
Categorías
Más información sobre Elementary Polygons en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!