# How plot a grid of rectangles on an overlaid circle?

107 Ansichten (letzte 30 Tage)
JM am 20 Mai 2024 um 16:48
Verschoben: Matt J am 28 Mai 2024 um 12:33
How to plot a circle of radius R and an overlaid grid of rectangles (with each rectangle of dimensions LxW)?
Choice of dimensions are so that the rectangular grid is larger than the dimeter of the circle and the centre of the circle is on one of the rectangle corners within the grid. The circle fully sits within the rectangular grid.
Secondly, from the plot or otherwise can Matlab help count the full rectangles within the circle?
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Matt J am 20 Mai 2024 um 18:04
Bearbeitet: Matt J am 20 Mai 2024 um 18:14
R = 5; % circle radius
H = 1; % rect height
W = 2; % rect width
Grid=scale(nsidedpoly(4,'Side',1,'Center',[+1,+1]/2),[W,H]);
Grid=arrayfun(@(i)translate(Grid,H*[0,i]),-ceil(R/H):ceil(R/H)-1)';
Grid=arrayfun(@(i)translate(Grid,W*[i,0]),-ceil(R/W):ceil(R/W)-1,'uni',0);
Grid=vertcat(Grid{:}); %polyshape vector for grid rectangles
numContained=sum( area(intersect(Grid,circ))>=area(Grid(1))*0.9999 ) %count contained rectangles
numContained = 28
plot([circ; Grid],'FaceColor','w'); axis equal
##### 13 Kommentare11 ältere Kommentare anzeigen11 ältere Kommentare ausblenden
JM am 22 Mai 2024 um 14:18
Bearbeitet: JM am 22 Mai 2024 um 14:33
Upgrade, as much as I would want it, is beyond my control I am afraid
JM am 22 Mai 2024 um 16:03
Verschoben: Matt J am 28 Mai 2024 um 12:33
@Matt J. Thanks

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (1)

Voss am 20 Mai 2024 um 17:31
R = 5; % circle radius
L = 1; % horizontal grid spacing
W = 2; % vertical grid spacing
C = [7 8]; % center of the circle
% calculate some points on the circle
th = linspace(0,2*pi,100);
xc = C(1)+R*cos(th);
yc = C(2)+R*sin(th);
% calculate the grid's x and y coordinates
xg = C(1)+W*(floor(-R/W):ceil(R/W));
yg = C(2)+L*(floor(-R/L):ceil(R/L));
% plot the circle
plot(xc,yc)
axis equal
% create the vertical grid lines
nx = numel(xg);
xdata = [xg;xg;NaN(1,nx)];
ydata = [repmat(yg([1 end]).',1,nx); NaN(1,nx)];
line(xdata,ydata,'Color','k')
% create the horizontal grid lines
ny = numel(yg);
ydata = [yg;yg;NaN(1,ny)];
xdata = [repmat(xg([1 end]).',1,ny); NaN(1,ny)];
line(xdata,ydata,'Color','k')
% count the rectangles completely inside the circle
n_rectangles_inside = nnz(conv2((xg-C(1)).^2+(yg.'-C(2)).^2 <= R^2,ones(2)) == 4)
n_rectangles_inside = 28
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Spline Postprocessing finden Sie in Help Center und File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!

Translated by