MATLAB: How to locate/extract users in circle from randomly distributed many points..

circledistributionlocationpoints

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.

Best 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.