Patch won't fill my polygon and inpolygon won't find points inside

조회 수: 13 (최근 30일)
Daniel
Daniel 2021년 5월 26일
댓글: Matt J 2021년 5월 27일
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
Cris LaPierre
Cris LaPierre 2021년 5월 26일
편집: Cris LaPierre 2021년 5월 26일
I tried running your code and got the following errer:
Unrecognized function or variable 'ycoords'.
Daniel
Daniel 2021년 5월 26일
Sorry, had an error. Fixed now.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 5월 27일
For question 2, it doesn't work for a couple reasons.
  1. xdim(31:length(zdim)-1) must be the same length as zdim. They are not, so you get an error.
  2. Modifying the data so they are the same length, none of the points fall within the polygon, hence, inpolygon is not returning anything.
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')
hold on
plot(xdim(31:length(zdim)+30),zdim,'r.')
hold off
Perhaps you have not created the grid of data you intended. Can you describe what you expected the gridded points to be?
  댓글 수: 2
Daniel
Daniel 2021년 5월 27일
Apologies. I had a typo that I have now fixed. The lengths were not the issue. Perhaps I need a meshgrid? xdim and zdim are meant to be the x and z coordinates of a grid on which data exist. So all pairs of x and z coordinates should be considered, but I thought that inpolygon did that. Does it not?
Cris LaPierre
Cris LaPierre 2021년 5월 27일
No, it will not create the combinations for you. Look to the documentation.
  • The size of xq must match the size of yq. (and vice versa)
You must pass in the data with all the pairings already made. If you want to create a grid using the points you already have,then yes, use meshgrid.
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')
[xq,yq] = meshgrid(xdim,zdim);
inshape = inpolygon(xq,yq,hhx,hhz);
hold on
plot(xq,yq,'b.')
plot(xq(inshape),yq(inshape),'r.')
hold off

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

KSSV
KSSV 2021년 5월 27일
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')
  댓글 수: 1
Daniel
Daniel 2021년 5월 27일
Thanks for the answer. I had already figured out this part of the question, but I'm leaving it "unanswered" until I get an answer on the second part regarding inpolygon. Can you tell me why that is not working even though some of the grid points (xdim and zdim) in the attachment are within the polygon?

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2021년 5월 27일
편집: Matt J 2021년 5월 27일
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
Daniel
Daniel 2021년 5월 27일
Honestly, I'm not totally sure, but that's the less important part. How would you extend this to use inpolygon?
Matt J
Matt J 2021년 5월 27일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by