Plotting circles with some constraints from random points

3 views (last 30 days)
Hey guys,
i have randomly generated n-by-2 matrix which have (x,y) coordinates in it.
after sorting them like sort(coordinates,'rows')
i want to plot them starting from the first to last one if the distance between them is less than some distance d.
To make it more clear
the program will plot (or put the coordinates in another matrix or do something else) the first coordinates because there will be notting to compare with , than it will look the distance beetween coordinates(2,:) and coordiante(1,:) and plot it if the distance is more than d. then for the third one first it will look the first and second.(Plotted ones)
so a nth coordinates will be compared with all coordinates from 1 to n IF THEY ARE PLOTTED.
I know it become a long text but i hope i explained it clear enough.
And thank you everyone for your help.

Accepted Answer

Image Analyst
Image Analyst on 29 Jul 2012
Edited: Image Analyst on 29 Jul 2012
By the way, somewhere MATLAB has a demo to optimize placement of radio towers to optimize coverage, though I couldn't find it. You might call them and ask them for it, or post a question in this forum about it - maybe someone remembers it.
Well anyway, at least you gave it a shot. Now try it this way:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Generate some sample data.
possibleSites = rand(150, 2);
commissionedSites(1, 1) = possibleSites(1, 1); % First good X
commissionedSites(1, 2) = possibleSites(1, 2); % First good Y.
radius = 0.1;
% Commissioning Phase
goodPoints =1;
% Calculating interference
for k1 = 2 : length(possibleSites)
nextX = possibleSites(k1, 1);
nextY = possibleSites(k1, 2);
% Check the distance of this possible next point
% to ALL of the other previous good points.
for k2 = 1 : goodPoints
% Get the coordinates of the earlier good point.
thisGoodX = commissionedSites(k2, 1);
thisGoodY = commissionedSites(k2, 2);
% Calculate distance from the possible next point
% to this prior good point.
distance = sqrt(((nextX - thisGoodX)^2) + ((nextY - thisGoodY)^2))
% Set a flag about whether it's too close to prior points.
itsTooClose = false;
if distance < 2 * radius
itsTooClose = true;
% At least one prior point is too close.
% So this point is already no good. Bail out.
break;
end
end
% If it's not too close to any prior points, then accept it and plot it.
if ~itsTooClose
goodPoints = goodPoints + 1;
% It's far enough away. Accept it as a good point.
commissionedSites(goodPoints, 1) = nextX;
commissionedSites(goodPoints, 2) = nextY;
% Plot it
plot(commissionedSites(:, 1), commissionedSites(:, 2), ...
'bo-', 'LineWidth', 2);
grid on;
xlim([0 1]);
ylim([0 1]);
if goodPoints <= 2
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
end
% Let user see it and prompt him to continue.
promptMessage = sprintf('That was point #%d.\nDo you want to Continue processing,\nor Cancel to abort processing?', goodPoints);
button = questdlg(promptMessage, 'Continue', 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
return;
end
end
end

More Answers (1)

Image Analyst
Image Analyst on 29 Jul 2012
Seems very easy and straightforward. Were you unable to do it with a pair of nested for loops? What happened when you tried the obvious brute force method:
for k1 = 1 : n
for k2 = 1 : k1
% Compare to others that went before it.
% Calc distance, etc.
end
end
Whas there some error or something?
  2 Comments
E K
E K on 29 Jul 2012
Edited: E K on 29 Jul 2012
I tried it but when i run the code below it does not eleminate ones with the radius smaller than 2*radius
but i think i am missing a point
%%Commissioning Phase
kk=1;
% Calculating interference
for k1 = 1 : length(possiblebasesite)
for k2 = 1 : k1
xtest1=possiblebasesite(k1,1);
ytest1=possiblebasesite(k1,2);
xtest2=possiblebasesite(k2,1);
ytest2=possiblebasesite(k2,2);
if sqrt(((xtest1-xtest2)^2)+((ytest1-ytest2)^2)) > radius*2
commissionedbasesite(kk,1)=xtest1;
commissionedbasesite(kk,2)=ytest1;
kk=kk+1;
end
% Calc distance, etc.
end
end
end
Image Analyst
Image Analyst on 29 Jul 2012
Edited: Image Analyst on 29 Jul 2012
You're not comparing the next possible site to all prior commissioned sites. You just haven't thought it through clearly enough. See the Answer below.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!