Reputation: 31
I have 100 data points scattered within a region. Now I divided the region with 6x6 equisized grids and now it looks like according the figure below:
figure http://s2.postimg.org/c6zk68myf/IGN2.png
Now I need to to draw a circle within a box of a grid so that I could group points in a box if there is more than 1 point. If this is the case, there shall be no circle within a box if there are none or one points in a box.
Any idea on what I should do?
Here is my MATLAB code for the plot I created above:
xm=100;
ym=100;
%x and y Coordinates of the Sink
sink.x=0.5*xm;
sink.y=0.5*ym;
%Number of Nodes in the field
n=100
figure(1);
for i=1:1:n
S(i).xd=rand(1,1)*xm;
XR(i)=S(i).xd;
S(i).yd=rand(1,1)*ym;
YR(i)=S(i).yd;
S(i).G=0;
%initially there are no cluster heads only nodes
S(i).type='N';
plot(S(i).xd,S(i).yd,'o');
hold on;
end
NrGrid = 6;
% Number Of Grids
x = linspace(0, 100, NrGrid+1);
[X,Y] = meshgrid(x);
S(n+1).xd=sink.x;
S(n+1).yd=sink.y;
plot(S(n+1).xd,S(n+1).yd,'x',X,Y,'k');
hold on
plot(Y,X,'k')
set(gca, 'Box','off', 'XTick',[], 'YTick',[])
axis square
%First Iteration
figure(1);
Expected result:
http://i.share.pho.to/27ae061b_o.jpeg
Upvotes: 3
Views: 1087
Reputation: 35525
So the proccess you need to go to get there is the following:
So here there is a piece of code that does 1-2
%% Where are the points?
% I am not going to modify your `S` variable, but you could do this inside
% it
points=[S(1:end-1).xd; S(1:end-1).yd];
gridind=zeros(size(points,2),1);
Grid=NrGrid^2;
for ii=1:NrGrid
for jj=1:NrGrid
% get points in the current square
ind=points(1,:)>X(1,ii)& points(1,:)<X(1,ii+1)...
& points(2,:)>Y(jj,1)& points(2,:)<Y(jj+1,1);
% save index
gridind(ind)=(ii-1)*NrGrid+(jj-1)+1;
end
end
% now that we know in wich grid each point is lets find out wich ones are
% not
c=zeros(NrGrid^2,2);
r=zeros(NrGrid^2,1);
for ii=1:NrGrid^2
if sum(gridind==ii)>1
[c(ii,:), r(ii)] = minboundcircle(points(1,gridind==ii),points(2,gridind==ii));
else
c(ii,:)=nan;
r(ii)=nan;
end
end
and here a piece of code that plots them
theta=linspace(0,2*pi,20);
offs=1; % For beauty purposes
for ii=1:NrGrid^2
if ~isnan(c(ii))
xc=(r(ii)+offs).*cos(theta)+c(ii,1);
yc=(r(ii)+offs).*sin(theta)+c(ii,2);
plot(xc,yc,'r');
end
end
axis([min(X(:)) max(X(:)) min(Y(:)) max(Y(:)) ])
Result:
You could easily draw ellipses changing a bit the code, using for example this FEX submission.
Ask if you don't understand something, but should be straightforward.
Upvotes: 4