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

9 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2024-2-1
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

更多回答(1 个)

John D'Errico
John D'Errico 2024-2-1
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

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by