MATLAB: How to calculate the area percentage of the polygons

areapolygons

I have x_limit and y_limit as [69.5,70.5] and [21.2,21.8] inside which their i have plotted many polygons (the coordinates of those polygons are attached with this text). Now i want to calculate the percentage of area occupied by the polygons inside the particular x_limit and y_limit.
Thank you

Best Answer

Try this, using polyarea(). Note though that I am not deducting area for any overlapping portions. In other words, I assume no polygons overlap when computing area fraction:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
s = load('polygons_coordinates.mat')
output = s.output;
x1 = 69.5
x2 = 70.5;
y1 = 21.2;
y2 = 21.8;
boxArea = (x2-x1) * (y2-y1)
numPolygons = length(output);
colors = hsv(numPolygons);
totalArea = 0;
for k = 1 : length(output)
thisPolygon = output{k};
x = thisPolygon(:, 1);
y = thisPolygon(:, 2);
thisArea = polyarea(x, y);
totalArea = totalArea + thisArea;
patch(x, y, colors(k, :));
hold on;
end
hold off;
grid on;
areaFraction = totalArea / boxArea
caption = sprintf('%d total polygons. Area Fraction = %f', ...
numPolygons, areaFraction);
title(caption, 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);