Viron Gil A. Estrada
Viron Gil A. Estrada

Reputation: 89

Generate concentric circles in a meshgrid in MATLAB

I am trying to generate concentric circles in a meshgrid but the code I used only plots the borders of the circles.

theta = linspace(0, 2*pi, 100);
[X, Y] = meshgrid(1:1:4, theta);
a = 0;
b = 0;
plot(a+cos(Y).*X, b+sin(Y).*X);
axis equal

What I intend to do is to generate concentric circles that are filled and that the points inside the circles are valued as either 1 (white) or 0 (black). Below is a picture for visualization. How do you code this using MATLAB?

Thanks,

enter image description here

Upvotes: 1

Views: 878

Answers (1)

bla
bla

Reputation: 26069

you need to use meshgrid a bit differently:

N=200;  % some size of grid
if mod(N,2) % odd vs even matrix sizes
        [x,y] = meshgrid(-(N-1)/2:(N-1)/2);
else
        [x,y] = meshgrid(-N/2+1:N/2);
end

x0=0; y0=0; % assuming the circle are always in the center, but we can modify this if needed 

% say we want a ring between `r1` and `r2`
f = @(r1,r2) (x-x0).^2+(y-y0).^2<=r2^2 & ...
             (x-x0).^2+(y-y0).^2>=r1^2;

now try:

imagesc(f(30,40)+f(50,60))

enter image description here

f is a logical matrix that has value 1 for the conditions given by an area between two circles of radius r1 and r2 .... you only care about the radius because the theta coordinate is always satisfied (or between 0 and 2pi) so it's not relevant...

Upvotes: 1

Related Questions