Programmatically calculate indices of the outline of a circle and all points within in it
9 views (last 30 days)
Show older comments
I am trying to establish a programmatic means of calculating the indices of the outline of a circle and all points within in it.
Of course, this is can be done manually (very tediously) but I would grateful if anybody had any suggestions.
I can then apply logical indexing to variables of interest.
Thank you.
Daniel
r = 0.3; % radius
x = 0; % central x coordinate of domain
y = 0; % central y coordinate of domain
[xunit,yunit] = circle(x,y,r);
X = 0.375:-0.125:-0.375; % x coordinates
Y = -0.375:-0.125:-.375; % y coordinates
npoints = [99 99]; % define improved resolution for contour plots later
Xfine = linspace(X(1),X(end),npoints(1)); % y coords
Yfine = linspace(Y(1),Y(end),npoints(2)); % z coords
[Xfine Yfine] = meshgrid(Xfine,Yfine); % create mesh
function [xunit,yunit] = circle(x,y,r)
th = 0:pi/50:2*pi;
xunit = r * cos(th) + x;
yunit = r * sin(th) + y;
end
1 Comment
Matt J
on 1 Feb 2024
You appear to have code already. Were you going to tell us that something is wrong with it?
Accepted Answer
Walter Roberson
on 1 Feb 2024
X = 0.375:-0.125:-0.375; % x coordinates
Y = -0.375:-0.125:-.375; % y coordinates
npoints = [99 99]; % define improved resolution for contour plots later
Xfine = linspace(X(1),X(end),npoints(1)); % y coords
Yfine = linspace(Y(1),Y(end),npoints(2)); % z coords
[Xfine Yfine] = meshgrid(Xfine,Yfine); % create mesh
r = 0.3; % radius
x = 0; % central x coordinate of domain
y = 0; % central y coordinate of domain
mask = (Xfine - x).^2 + (Yfile - y).^2 <= r.^2;
indices = find(mask); % but typically it is better to use the mask directly
More Answers (1)
John D'Errico
on 1 Feb 2024
Wait. You want to compute the list of all points within a circle? There are infinitely many.
Ok, I guess given a MESH of points, you want to know how many lie inside? That is trivial.
For example, given a circle of radius 0.3, with center at (0,0), which points from your fine mesh lie inside? No loops needed. Just the equation of a circle.
r = 0.3; % radius
C = [0,0]; % LEARN TO USE VECTORS
[X,Y] = meshgrid(linspace(-0.375,0.375,99));
insideind = (X-C(1)).^2 + (Y - C(2)).^2 < r.^2;
XYinside = [X(insideind),Y(insideind)]
size(XYinside,1)
So 4825 nodes from that mesh lie inside. None will lie EXACTLY on the perimeter of the circle due to the use of floating point arithmetic.
sum((X-C(1)).^2 + (Y - C(2)).^2 == r.^2,'all')
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!