How to find area of all possible polygons?

2 views (last 30 days)
n = 5; % No. of control points
array = 1:1:n;
p=arrayfun(@(k) num2cell(nchoosek(array,k),2), 3:length(array), 'unif', 0);
p=cat(1,p{:});
p=cellfun(@(array) num2cell(perms(array),2),p,'unif',0);
p=cat(1,p{:});
x = [1 ; 2 ; 3 ; 4 ; 2 ]; % coordinate of control points
y = [8 ; 5 ; 8 ; 7 ; 6 ]; % coordinate of control points
for i = 1:length(p)
x_p{i,1} = x(p{i,1});
y_p{i,1} = y(p{i,1});
end
From the above code, I got two cells x_p & y_p which provides coordinates of all possible polygons. I want to find the array of areas of all polygons. The size of area array will be (300 * 1). Thanks in advance.
  8 Comments
Manas Ranjan Pattnayak
Manas Ranjan Pattnayak on 14 Oct 2018
Thank you so much. Found the answer. However just asking - Is there any way to find the "unsigned area" of the polygon??
Bruno Luong
Bruno Luong on 14 Oct 2018
Edited: Bruno Luong on 14 Oct 2018
Yes but this method is still strongly NOT recommended, since that math behind is not consistent, as stated by the warning. Just manage to eliminate those invalid polygonal.
>> x = [0;4;0;4;0]; y = [0;0;2;2;0];
>> p=polyshape(x,y)
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce
inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
> In polyshape/checkAndSimplify (line 481)
In polyshape (line 175)
p =
polyshape with properties:
Vertices: [7×2 double]
NumRegions: 2
NumHoles: 0
>> p.area
ans =
4

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 14 Oct 2018
n = 5; % No. of control points
array = 1:1:n;
p=arrayfun(@(k) num2cell(nchoosek(array,k),2), 3:length(array), 'unif', 0);
p=cat(1,p{:});
p=cellfun(@(array) num2cell(perms(array),2),p,'unif',0);
p=cat(1,p{:});
x = [1 ; 2 ; 3 ; 4 ; 2 ]; % coordinate of control points
y = [8 ; 5 ; 8 ; 7 ; 6 ]; % coordinate of control points
% Vectorize the computing of areas (in S(:))
% by group of polygonals having the same length
np = cellfun('length',p);
[npu,~,J] = unique(np); % npu is logically [3,4,5]
s = zeros(size(p)); % allocate array of area result
for i = 1:length(npu) % == 3
b = J==i;
Pi = cat(1,p{b});
s(b) = polyarea(x(Pi),y(Pi),2);
end
% Many of them represents the same geometrical polygon
% The entire procedure seems redundant thus wasteful
allimax = find(s==max(s));
for j=1:length(allimax)
imax = allimax(j);
pmax = p{imax};
xpmax = x(pmax);
ypmax = y(pmax);
figure;
plot(xpmax([1:end 1]),ypmax([1:end 1]));
end

More Answers (1)

Walter Roberson
Walter Roberson on 15 Oct 2018
The official way to do this since R2017b is to use polyshape() to define a polyshape object from x and y coordinates, and then to use the area() method. You can see from https://www.mathworks.com/help/matlab/ref/polyshape.html#mw_fc5e8ce5-418f-4935-bd8b-6dd205170891 that "bowtie" area is supported.
  2 Comments
Bruno Luong
Bruno Luong on 15 Oct 2018
Edited: Bruno Luong on 15 Oct 2018
"The official way ..."
just wonder: polyarea is not official?
Unless I'm mistaken, polyarea can compute area of multiple polygonal with the same number of vertices in a single call (vectorized then), something polyshape can't.
TMW seems to go lately father and father to OOP, and neglects the essential: speed.
Walter Roberson
Walter Roberson on 15 Oct 2018
polyshape decomposes to non selfintersecting polygons and calculates the total area. polyarea uses a much faster formula that is valid when there is no selfintersecting and which ends up subtracting areas if there is, which is useful in some situations. Different purposes.

Sign in to comment.

Categories

Find more on Elementary Polygons in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!