Borrar filtros
Borrar filtros

finding which points in a grid are inside an irregular shape

9 visualizaciones (últimos 30 días)
Giovanni de amici
Giovanni de amici el 2 de Nov. de 2017
Respondida: Giovanni de amici el 3 de Nov. de 2017
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?

Respuestas (3)

KSSV
KSSV el 3 de Nov. de 2017
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 comentario
Giovanni de amici
Giovanni de amici el 3 de Nov. de 2017
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.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 3 de Nov. de 2017
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 el 3 de Nov. de 2017
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.

Categorías

Más información sobre Elementary Polygons en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by