finding which points in a grid are inside an irregular shape

11 次查看(过去 30 天)
Good day; I have an irregular closed shape (think of a sketch of a Christmas tree) where for any X there can be multiple Ys and viceversa. I can plot the perimeter of this shape, and with "fill" I can color all the points inside the shape. I would like to add a third dimension Z, defined to be 1 inside the shape (where 'fill' place a dot of color) and 0 elsewhere. Is there any (relatively simple) way to do that?

回答(3 个)

KSSV
KSSV 2017-11-3
You can happily achieve this. Check the below example code to get what you want.
%%some closed polygon
th = linspace(0,2*pi)' ;
x = cos(th) ;
y = sin(th) ;
plot(x,y) ;
%%Adding third diemsnion
% make a grid first
N = 100 ;
xi = linspace(min(x),max(x),N) ;
yi = linspace(min(y),max(y),N) ;
[X,Y] = meshgrid(xi,yi) ;
%%get points lying inside polygon
idx = inpolygon(X(:),Y(:),x,y) ;
Z = zeros(size(X)) ;
Z(idx) = 1 ; % assign one which lie inside
%%plot to check
surf(X,Y,Z)
In the above code, I have used circle as a closed region. You can replace that with your coordinates.
  1 个评论
Giovanni de amici
Giovanni de amici 2017-11-3
Thanks. the call to 'inpolygon' works very well for relatively small grids, but in my case I need the N of your code to be at least 2500. Checking a grid of 2500by2500 slows my matlab processor to a going-nowhere crawl. Was hoping to find something like 'fill' which responds instantly no matter how small/large the polygon to fill might be. Oh, well, it looks like I might have to look for a faster/bigger machine.

请先登录,再进行评论。


Image Analyst
Image Analyst 2017-11-3
Simply use poly2mask():
binaryImageZ = poly2mask(x, y, rows, columns);
What is your colored image that you want to add binaryImageZ to in the z dimensions? Is it an RGB image, or an indexed image with a pseudocolor map?

Giovanni de amici
Giovanni de amici 2017-11-3
thanks. unfortunately my edition of matlab does not recognize the call 'poly2mask'. it is specific to the 'image processing' toolbox, which I do not have. To answer your question: not trying to add images (at least not now). I draw a shape from the union of several analytical functions, then need to 'fill in' the perimeter and export the filled-in shape in a format that a 3D printer will use to produce an object with that form. So I need to recognize which points in the XY plane are inside the shape and set the height of those points to a non-zero value.

类别

Help CenterFile Exchange 中查找有关 Elementary Polygons 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by