How to find area of all possible polygons?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Manas Ranjan Pattnayak
el 14 de Oct. de 2018
Comentada: Walter Roberson
el 15 de Oct. de 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
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 comentarios
Bruno Luong
el 14 de Oct. de 2018
Editada: Bruno Luong
el 14 de Oct. de 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
Respuesta aceptada
Bruno Luong
el 14 de Oct. de 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
0 comentarios
Más respuestas (1)
Walter Roberson
el 15 de Oct. de 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 comentarios
Bruno Luong
el 15 de Oct. de 2018
Editada: Bruno Luong
el 15 de Oct. de 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
el 15 de Oct. de 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.
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!

