Generate a circular array of gridpoints inside a given contour.
Mostrar comentarios más antiguos
Hello Everyone,
Say I have a contour that needs to be filled with discrete individual gridpoints, as illustrated in the figure. However, the gridpoints are of circular orientation, i.e, the gridpoints, when joined together forms a circle and this progressively gets bigger as we move radially outwards, untill the contour is reached.
I understand an 'inpolygon' test is to be done somewhere, but I cannot create the grid.
The size of the points have nothing to do here. Points are nothine but XY cordinates. The red circle marks the contour.
Thank you.
Respuesta aceptada
Más respuestas (1)
Here's a function that fills an aribitrary contour (i.e., not necessarily a single circle) with random points (i.e., not necessarily in a circular grid) within each pair of contour levels.
x = linspace(-1,1,100);
y = linspace(-1,1,100).';
figure
[~,h] = contour(x,y,x.^2+y.^2, ...
'LevelList',1, ...
'EdgeColor','r', ...
'LineWidth',1.5);
axis padded
axis square
axis off
fill_contour_with_points(h);
figure
[~,h] = contour(x,y,x.^2+y.^2, ...
'EdgeColor','r', ...
'LineWidth',1.5);
axis padded
axis square
axis off
fill_contour_with_points(h,0.24);
figure
[~,h] = contour(x,y,abs(x)+0.3*abs(y), ...
'LevelList',[0.7 1 1.4], ...
'EdgeColor','r', ...
'LineWidth',1.5);
axis padded
axis square
axis off
fill_contour_with_points(h,0.12,true);
figure
[~,h] = contour(x,y,cos(x*10)+cos(y*4), ...
'EdgeColor','k', ...
'LineWidth',1);
axis padded
axis square
axis off
fill_contour_with_points(h,0.65,true);
function fill_contour_with_points(h,density,do_include_last)
if nargin < 2 || isempty(density)
density = 0.1;
end
if nargin < 3
do_include_last = false;
end
zd = get(h,'ZData');
xd = get(h,'XData');
yd = get(h,'YData');
if isvector(xd)
xd = xd(:).';
xd = xd(ones(size(zd,1),1),:);
end
if isvector(yd)
yd = yd(:);
yd = yd(:,ones(size(zd,2),1));
end
hold on
level_list = get(h,'LevelList');
level_list = [-Inf; level_list(:); Inf];
if ~do_include_last
level_list(end) = [];
end
for ii = 1:numel(level_list)-1
idx = find(zd >= level_list(ii) & zd < level_list(ii+1));
n_idx = numel(idx);
idx = idx(randperm(n_idx,ceil(density*n_idx)));
plot(xd(idx),yd(idx),'.');
end
end
Categorías
Más información sobre Sparse Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




