How to cover an irregular closed area with square pixels
1 次查看(过去 30 天)
显示 更早的评论
I have a set of closed curves described by the (x,y) coordinates of the approximating polygon.
My task is to cover the area enclosed by each curve with square pixel whose side is input.
No piece of the area must be left uncovered. That implies the squares covering the curve rim will lie partially inside and partially outside the curve.
In the following I describe what I have done.
- Generate a square grid of points inside or on the edge of the curve
- Generate new points lying outside the closed curve by subtracting the pixel side in the x or in the y direction
- Use Matlab function "rectangle" to build the square pixel.
- Eventually I will store to a file the coordinates of each pixel in the form X_min X_max Y_min Y_max
My problem is that some points, generated outside the closed curve, give rise to pixels that are completely outside the closed area. They do not intersect it.
I am attaching my script "Pixelize_Collimator_Aperture.m", my function "CollimatorGrid.m", and a zipped file containing the pictures of the resulting pixelized closed areas
I am pretty sure there are smarter ways to address this task. I would appreciate any suggestion and/or comment.
Thank you in advance.
Maura E.
0 个评论
回答(1 个)
KSSV
2019-6-4
Check the code:
R = 1. ;
C = [0. 0.] ;
th = linspace(0,2*pi) ;
xc = C(1)+R*cos(th) ;
yc = C(1)+R*sin(th) ;
% Make grid
m = 50 ; n = 50 ;
xi = linspace(min(xc),max(xc),m) ;
yi = linspace(min(yc),max(yc),n) ;
[X,Y] = meshgrid(xi,yi) ;
idx = inpolygon(X(:),Y(:),xc,yc) ;
X(~idx) = NaN ;
Y(~idx) = NaN ;
figure
hold on
plot(xc,yc,'b')
plot(X,Y,'r')
plot(X',Y','r')
3 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Bounding Regions 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!