how to get one shape out of multiple shapes

Hi,
I have multiple shapes I need to merge into a single shape, because I have sets of shapes those I have to merge and compare with each other (put into a one plot).
the set of data is attached and I can plot it like this:
plot(X(:, [1:end 1])', Y(:, [1:end 1])')
let me know, if you know how to do it, thanks.
I got this shape using patch, but it generated a Patch file that I cannot use.
Ideally, I would like my data was interpolated so the final shape will not have sharp edges but be smooth and go down to Y=0.
Help me handle that too if you can. thanks

1 Comment

"I got this shape using patch, but it generated a Patch file that I cannot use."
You can't use the patch object or you can't use the output generated?
What is the expected output? It will be helpful if you can show an illustration.

Sign in to comment.

 Accepted Answer

patch() doesn't create a file. If you created a file somehow, nobody knows how you did it.
I'm not sure where this is going, but here's a guess.
% loads X,Y
load data.mat
% get rid of NaNs
xhasnans = any(isnan(X),2);
yhasnans = any(isnan(Y),2);
goodrows = ~(xhasnans | yhasnans);
X = X(goodrows,:);
Y = Y(goodrows,:);
% find convex hull
K = convhull(double(X),double(Y));
Xh = X(K);
Yh = Y(K);
% plot the convex hull, show the curve endpoint
plot(Xh,Yh); hold on
plot(X(1),Yh(1),'o')
% get rid of the base of the curve
Xh = Xh(3:end-1);
Yh = Yh(3:end-1);
% extrapolate to Y=0 from last 10 datapoints
Np = 10; % number of points to use
% the right-hand part of the curve
Yhr = Yh(1:Np);
Xhr = Xh(1:Np);
Yexr = [0;Yhr];
Xexr = interp1(Yhr,Xhr,Yexr,'linear','extrap');
% the left-hand part of the curve
Yhl = Yh(end-Np+1:end);
Xhl = Xh(end-Np+1:end);
Yexl = [Yhl;0];
Xexl = interp1(Yhl,Xhl,Yexl,'linear','extrap');
% put them back together
Xex = [Xexr; Xh(Np+1:end-Np); Xexl];
Yex = [Yexr; Yh(Np+1:end-Np); Yexl];
% close the curve (if needed
Xex = Xex([1:end 1]);
Yex = Yex([1:end 1]);
% plot the extraploated curve, show the endpoint
plot(Xex,Yex,'--')
plot(Xex(1),Yex(1),'*')

3 Comments

thanks, I used your code but used part of the following answer for 'get rid of the base of the curve' because you just guessed it (i think).
I wouldn't call it a guess. I picked it manually based on the given hull. For a different set of polygons, I don't know that it would be consistently correct.
it didn't work korrektly for this set of data

Sign in to comment.

More Answers (2)

hello
try this
x = double(X(:));
y = double(Y(:));
% remove nan
id = isnan(x) & isnan(y);
x(id) = [];
y(id) = [];
% k = boundary(___,s) specifies shrink factor s using any of the previous syntaxes.
% s is a scalar between 0 and 1. Setting s to 0 gives the convex hull,
% and setting s to 1 gives a compact boundary that envelops the points.
% The default shrink factor is 0.5.
s = 0.1;
k = boundary(x,y,s);
x_out = x(k);
y_out = y(k);
% find lower left "corner" point to make extrapolation towards Y = 0
[mx,ix1] = min(x_out);
my = y_out(ix1);
ind = find(x_out<(mx+1));
slope = mean(diff(y_out(ind))./diff(x_out(ind)));
x_lower_left = mx - my/slope;
% find lower right "corner" point to make extrapolation towards Y = 0
[mx,ix2] = max(x_out);
my = y_out(ix2);
ind = find(x_out>(mx-1));
slope = mean(diff(y_out(ind))./diff(x_out(ind)));
x_lower_right = mx - my/slope;
% add those two new points to x_out and y_out
x_out2 = [x_out(1:ix2-1); x_lower_right; x_out(ix2:ix1); x_lower_left; x_out(ix1+1:end) ] ;
y_out2 = [y_out(1:ix2-1); 0 ; y_out(ix2:ix1); 0 ; y_out(ix1+1:end) ] ;
plot(x,y, '*', x_out, y_out, '-*r', x_out2, y_out2, '-g')
thanks, but it didn't work correctly for this set of data.

7 Comments

I got this shape if I still use your command
plot(X(:, [1:end 1])', Y(:, [1:end 1])')
  • what do you want to do - simply close the top (flat ?)
  • does your requirement "go down to Y=0" still holds ? (even though I cannot really figure out what it should be here )
Previously we got a trapezoid, in this case I need one shape that is the outline of all shapes and it will look like a big letter "V" this time (no need to go down to Y=0). thanks
You might want to unaccept the answer because some people are probably going to skip over your question because they see it's been marked solved.
It's not really clear to me what determines whether convexity and extrapolation are required.
I agree with @Image Analyst. If the question isn't answered, don't pick a solution that doesn't work.
But it fit the first data, maybe I will need different codes for different sets of data.
I also opened another question:
I would suggest to put the answer there as well, if some answered here. Sorry for the mess.
Sorry for the inconvinience, I am myself just recognizing the issues.
Extrapolation is not needed when max([y]) is at max([x]).
I think it will work if the 2 halves of the data are treated separately as follows, but I don't know how to join them together and get read of common point.
cutxr=X(1:size(X)/2,:);
cutyr=Y(1:size(Y)/2,:);
cutxl=X(size(X)/2:end,:);
cutyl=Y(size(Y)/2:end,:);
% plot(cutxl(:, [1:end 1])', cutyl(:, [1:end 1])'); hold on
% plot(cutxr(:, [1:end 1])', cutyr(:, [1:end 1])')
% find convex hull
Kr = boundary(double(cutxr(:)),double(cutyr(:)),1);
Kl = boundary(double(cutxl(:)),double(cutyl(:)),1);
Xhr = cutxr(Kr);
Yhr = cutyr(Kr);
Xhl = cutxl(Kl);
Yhl = cutyl(Kl);
% plot the convex hull, show the curve endpoint
plot(Xhr,Yhr); hold on
plot(Xhl,Yhl)

Sign in to comment.

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!