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
1 个评论
Matt J
2024-2-1
You appear to have code already. Were you going to tell us that something is wrong with it?
采纳的回答
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
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)]
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')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!