4
$\begingroup$

So we have two graphs overlapping with the Show[] function, one is a network of connected points, and the other displays three circles over the top of the points. I'm trying to check which points lie within the circles, and put them into separate lists. I first tried to use the distance equation, checking if the distances from the nodes to the centre of the circle is less than the radius:

   For[m = 1, m <= Length[coordinates], 
 nodex = Part[Part[coordinates, m], 1];
 nodey = Part[Part[coordinates, m], 2]; 
 circlecheck1 = 
  distanceFunction[Part[circle1Centre, 1], nodex, 
   Part[circle1Centre, 2], nodey];
 If[circlecheck1 <= circle1Radius, 
  AppendTo[inCircle1, {nodex, nodey}]]; m = m + 1]

(Note that all of the functions and Part[] variables function just fine in other parts of the code, so I'm certain this is not the issue.)

However, I ran into a problem: since we're using the Show[] function, it seems that the two "graphs" have different axes size, and so my assumption that they will lie on the same axis and obey the distance function is wrong.

Is there a way to either check whether the circles overlap anyway, or to force the axes to share the same length?

$\endgroup$
2
  • 2
    $\begingroup$ You should try to include a minimal working example with just enough code that someone can reproduce your problem. Once enough info is present you'll get much better feedback, otherwise it'll just be speculation about what might be going wrong. $\endgroup$
    – N.J.Evans
    Apr 28 at 12:36
  • $\begingroup$ What is the relation between the circles in 2D graphics space and the vertices of your graph? A graph is not a graphics primitive see image. $\endgroup$
    – Syed
    Apr 28 at 15:54

2 Answers 2

5
$\begingroup$

You can pick points within a tolerance with Disk, that is a circular region including its inside. MemberQ generates a function which checks if coordinate is in the region, Select chooses those points which fulfil this requirement. By applying the list of selector functions to your list of random points you can get separate lists of matched points per circle:

With[{pts = RandomReal[{-10, 10}, {10000, 2}], 
  disks = Table[Disk[RandomReal[{-5, 5}, 2], RandomReal[{1, 2}]], {3}]}, 
 Show[ListPlot[(Select@RegionMember@#)[pts] & /@ disks],
  Graphics[RegionBoundary /@ disks, PlotRange -> Full], 
  AspectRatio -> Automatic]]

enter image description here

$\endgroup$
2
  • $\begingroup$ I'm a little confused about this answer; are you plotting points directly into the discs or are you plotting points and eliminating any that lie outwith the discs? $\endgroup$
    – K_Theyers
    Apr 28 at 14:15
  • $\begingroup$ Okay, the selection method is so concise that it might be a bit hard to understand. I'm selecting from random points distributed over a much larger rectangle, and only those points which correspond each Disk region get selected in each group (coloured differently). So, there's elimination, I was a bit cheap on not plotting the background... $\endgroup$
    – kirma
    Apr 28 at 14:23
0
$\begingroup$

You have a circle and a set of points

disk = Graphics[Disk[{0, 0}, 2]];
pts = RandomReal[{-3, 3}, {10, 2}]
ptsplot = Graphics[{PointSize[Large], Red, Point[pts]}];

pts

We can plot the for inspection and illustrational purposes

Show[disk, ptsplot]

niceplot

And a True or False statement for each of the previously considered points:

Table[Element[pts[[i]], Disk[{0, 0}, 2]], {i, 1, Length@pts}]

logical

We can, also, count how many of said points lie inside the circle with the following command:

Length@Select[
  Table[Element[pts[[i]], Disk[{0, 0}, 2]], {i, 1, Length@pts}], # == 
    True &]

3

Finally, I am assuming that you want to know which ones are inside the circle. More precisely, getting the position of the points from the initial list that contains all of them. We can use the previously obtained Boolean True and False list to extract the positions of the True elements like so:

pos = Position[
  Table[Element[pts[[i]], Disk[{0, 0}, 2]], {i, 1, 
    Length@pts}], _?(# == True &)]

pos

Finally, I assume that you might, also, want the points themselves in an automatic way. You can use the previous output to Extract them:

Extract[pts, pos]

final

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.