Programmatically calculate indices of the outline of a circle and all points within in it

9 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Walter Roberson
Walter Roberson el 1 de Feb. de 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

Más respuestas (1)

John D'Errico
John D'Errico el 1 de Feb. de 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)]
XYinside = 4825×2
-0.2985 -0.0230 -0.2985 -0.0153 -0.2985 -0.0077 -0.2985 0 -0.2985 0.0077 -0.2985 0.0153 -0.2985 0.0230 -0.2908 -0.0689 -0.2908 -0.0612 -0.2908 -0.0536
size(XYinside,1)
ans = 4825
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')
ans = 0

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by