How to compute a weighted mean between two polygons?
1 view (last 30 days)
Show older comments
I am going to compute a weighted mean for two polygons with X Y Z (mat file attached). My purpose: I plan to have a polygon resulting from both polygons. These are the weights for each polygon.
W1 = 63.799;
W2 = 60.959;
4 Comments
John D'Errico
on 24 Jun 2023
Edited: John D'Errico
on 25 Jun 2023
I'm sorry, but this makes no sense that I can see, as you are asking it.
Suppose we have two polygons.
P1 = polyshape([0 0 1 1],[0 1 1 0]); % A simple unit square
P2 = polyshape([-1 2 -1],[-2 -2 1]); % a triangle
Utterly boring things as they are. I can even plot them.
plot(P1)
hold on
plot(P2)
Now, what would you expect as the result of a weighted mean of the two? How would a weighted mean of P1 and P2 be different with weights of [1,1], versus weights of [2,1]?
What does a mean of two polygons mean anyway? Then you can explain how a weighted mean is a different thing.
Anyway, in this simple case, what would you expect to see? I see your comment, that you don't know how to write the code, but before you write ANY code at all, you need ot know what that code should do. And you have not explained anything about that. So what would you expect, even in the trivially simple case I have shown, as a result?
(I suppose, one could decide to form a linear mapping, where we form a linear combination of any point in polygon 1, and in some way, a corresponding point in polygon 2. But that fails to answer the question of how you know which points correspond in that mapping. Without that information, the process seems meaningless. Another vague possibility is to compute a linear combination of any point in polygon1 plus EVERY point in polygon 2. That is, take all possible combinations thereof between two points in each of the two polygons. So, are you effectively asking to compute a (weighted) Minkowski sum of two polyhedra?)
For example, the Minkowski sum of the two polygons I show above is:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1418758/image.jpeg)
Computed using my utility minkowskisum. It can be found on the file exchange. However, it is not of any use for a weighted mean. Nor would it apply to a 3-d polyhedron. Could you write a Minkowski sum in 3-d? Well, yes. I'm still not at all sure how the weights would apply there, nor what they even mean. But if you can compute the sum, then a weighted sum must still apply.
Answers (1)
Mathieu NOE
on 27 Jun 2023
Edited: Mathieu NOE
on 27 Jun 2023
hello
maybe this ? (nothing fancy)
as W1 and W2 are almost the same , the resulting average curve (black dashed ) is half way between P1 and P2
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1420503/image.png)
load('P1.mat');
x1 = polygon1(:,1);
y1 = polygon1(:,2);
load('P2.mat');
x2 = polygon2(:,1);
y2 = polygon2(:,2);
% average polygon
W1 = 63.799;
W2 = 60.959;
[x_aw,y_aw] = average_curve(x1,y1,W1,x2,y2,W2);
plot(x1,y1,'b',x2,y2,'r',x_aw,y_aw,'k--')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [xn,yn] = average_curve(x1,y1,w1,x2,y2,w2)
theta_new = linspace(-pi,pi,360);
[r1_new,centroid_x1,centroid_y1] = convert(x1,y1,theta_new);
[r2_new,centroid_x2,centroid_y2] = convert(x2,y2,theta_new);
% averaging (according to weigths) the radii
r_awg = (r1_new*w1 + r2_new*w2)/(w1 + w2);
% averaging (according to weigths) the centroids
centroid_x_awg = (centroid_x1*w1 + centroid_x2*w2)/(w1 + w2);
centroid_y_awg = (centroid_y1*w1 + centroid_y2*w2)/(w1 + w2);
% convert back to cartesian
[xn,yn] = pol2cart(theta_new,r_awg);
% add back centroid info
xn = xn + centroid_x_awg;
yn = yn + centroid_y_awg;
end
function [r_new,centroid_x,centroid_y] = convert(x,y,theta_new)
%% method 1
centroid_x = mean(x);
centroid_y = mean(y);
[theta,r] = cart2pol(x-centroid_x,y-centroid_y);
% sort theta in ascending order
[theta,ind] = sort(theta);
r = r(ind);
% remove duplicates
[theta,IA,IC] = unique(theta);
r = r(IA);
r_new = interp1(theta,r,theta_new);
end
2 Comments
Mathieu NOE
on 6 Jul 2023
ok
I tried to provide a solution acording to your initial data (P1 and P2.mat)
this is a different scenario
glad you coud find a viable solution
See Also
Categories
Find more on Elementary Polygons in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!