Patch won't fill my polygon and inpolygon won't find points inside
Show older comments
I'm trying to do two things with the coordinates of a shape that was created using slice_stl_create_path from the FEX. First, I just want to plot one of the slices as a filled patch, but I can only get part of it filled. I think it's because it kind of appears as two polygons, but I'm not sure how to avoid that given the method for creating the coordinates. I've attached a file with the x and y coordinates of the polygon as derived from the FEX script.
Second, I have another grid of data and would like to identify points on the grid that are within this polygon. I know from looking at the numbers that some are within it, but inpolygon returns all zeros. The grid points are also in the attachment. Here's some code:
load('coords')
% this doesn't fill anything
patch(hhx,hhz,'k') % hhx and hhz are found with slice_stl_create_path.m
% this fills part of it
[shhx,idx] = sort(hhx);
shhz = hhz(idx);
patch(shhx,shhz,'k')
% to find points inside
inshape = inpolygon(xdim(31:31+length(zdim)-1),zdim,hhx,hhz); % xdim and zdim are the grid points, xdim is longer than zdim
4 Comments
Daniel
on 26 May 2021
Daniel
on 26 May 2021
Cris LaPierre
on 26 May 2021
Edited: Cris LaPierre
on 26 May 2021
I tried running your code and got the following errer:
Unrecognized function or variable 'ycoords'.
Daniel
on 26 May 2021
Accepted Answer
More Answers (2)
KSSV
on 27 May 2021
load('coords')
% Remove nans
hhx(isnan(hhx)) = [] ;
hhz(isnan(hhz)) = [] ;
% GEt the boundary
idx = boundary(hhx,hhz) ;
hhx = hhx(idx) ; hhz = hhz(idx) ;
patch(hhx,hhz,'k')
Aside from what others have said, your hhx and hhz data does not define a polygon, but rather two polygons. I supsect that this will confuse inpolygon() even if you test points that are within one of the polygons.
load coords
pgon=polyshape(hhx,hhz,'Simplify',false);
plot(pgon)
I'm assuming the shape you actually intend is closer to the following:
d=0.0003;
pgon=polybuffer( polybuffer(pgon,d), -d); %morphological close
pgon=rmslivers(pgon,3e-6);
plot(pgon)
4 Comments
Daniel
on 27 May 2021
Matt J
on 27 May 2021
You tested the code on the same data?
Daniel
on 27 May 2021
Matt J
on 27 May 2021
You can either use isinterior()
or you can get the vertices from the Vertices property of the polyshape object,
pgon.Vertices
whereupon you can use inpolygon() as normal.
Categories
Find more on Polygonal Shapes 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!



