how to locate/extract users in circle from randomly distributed many points..?

Hi everyone. I have randomly deployed users in a hexagon and circle. The problem i am facing is to locate the users which are in circle, and to find the distance of those users/points from the center of circle. Is there any way that only the users which are in circle can be extracted as shown in figure? Kindly help me with this.

 Accepted Answer

You must know the circle you're talking about. So let's say it is at location (xCenter, yCenter) and has radius R. I'm going to assume that when you said you "randomly deployed users in hexagon and circle" that you actually did NOT do that since you said some may not be inside the circle and you need to know which are not in the circle. So you can use sqrt() to find the distance of each user location from the center. I'll assume the users' locations are in vectors xUser and yUser. So, to find the distance of all users from the known center of the circle, you do:
distances = sqrt((xUser - xCenter) .^ 2 + (yUser - yCenter) .^ 2);
Now to find out which indexes are inside the circle, look for which distances are less than R
usersInsideCircle = distances < R;
That is a logical vector. If you want to extract the x and y of users in the circle from the entire list, use that logical index:
xInCircle = xUsers(usersInsideCircle);
yInCircle = yUsers(usersInsideCircle);
For the hexagon you might find it easier to use the inpolygon() function:
for k = 1 : length(xUsers)
usersInsideCircle(k) = inpolygon(xUsers(k), yUsers(k), xVertices, yVertices);
end
where xVertices and yVertices are the 6 vertices of your hexagon. You could use that for the circle also if you wanted to supply all the edge coordinates of the circle perimeter though there is a slight chance that a point could be inside the theoretical circle but outside the "circle" approximated by a bunch of vertex points.

8 Comments

I saw your comment and added the picture to your original post. It looks like my first two lines of code should work, assuming you know where the centers of the 4 circles are.
thank you. is there any way to count the number of points within these circles. i have randomly distributed points in polygon. now i want to count the points every time coming inside circle kindly help me out.
Yes, just sum usersInsideCircle:
numPointsInside = sum(usersInsideCircle);
this not working, just giving sum of the distances. i just want to count the number of points inside circle. help plz
How can that possibly be? usersInsideCircle is the output of inpolygon which will be either 1 or 0 depending on if the point is inside or outside the polygon. So summing that array will be a count of the points inside. How could it possibly be a sum of the distances????? Are you sure you summed the right array?
distances = sqrt((xUser - xCenter) .^ 2 + (yUser - yCenter) .^ 2);
usersInsideCircle = distances < R;
this way i got points within these small circles. and the array "userinsidecircle" giving distance of each user from center within circle, while the other entries are zero.
Sorry, but no it doesn't. Just look at it in the dubugger. usersInsideCircle is a logical vector of true/1, and false/0. It is not an array of doubles with various floating point distances. You'd better check again.
FYI to get the distances themselves, you'd have to use usersInsideCircle as a logical index to distances, like this:
distancesInside = distances(usersInsideCircle);
THAT will be a vector of only the distances of users inside the circle and won't include distances of users outside the circle.
i was doing in wrong way. now worked. thank you.

Sign in to comment.

More Answers (2)

pdist2() with the second argument being the coordinates of the center of the circle. Any of the points within the cutoff radius are "in" the circle and you will know the distance.

3 Comments

what will be the first argument??? as i don't know the location of the points within the circles, i used random distribution. as in my graph i want to locate the position of the points within "blue" circle. what should be the solution for this? answer please
The first argument would be the location of the points. You do know the location because you used random distribution to generate the locations.
Your figure does not appear to be attached.
Is your task to figure out which of the already-generated positions are within a particular circle, or is your task to generate positions confined to a particular circle like https://www.mathworks.com/matlabcentral/answers/294-generate-random-points-inside-a-circle

Sign in to comment.

If you have two sets of points, red crosses and blue crosses, and want to find corresponding points, like which point in blue best matches a given point in red, this is not easy, and there may be no unique solution. There are many algorithms and you can search for "point matching algorithm" for articles: https://www.google.com/#q=point+matching+algorithm

Categories

Find more on Mathematics 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!