finding which points in a grid are inside an irregular shape
5 views (last 30 days)
Show older comments
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?
0 Comments
Answers (3)
KSSV
on 3 Nov 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.
Image Analyst
on 3 Nov 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?
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!