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!