필터 지우기
필터 지우기

finding which points in a grid are inside an irregular shape

조회 수: 9 (최근 30일)
Giovanni de amici
Giovanni de amici 2017년 11월 2일
답변: Giovanni de amici 2017년 11월 3일
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