1
$\begingroup$

Here I have a list of points on the plane:

lst = RandomReal[{-5, 5}, {55, 2}];

and a disk with the radius r=1 drawn around the point number i. i will be taken equal 5 in this example below.

I want to select all points with the distance to the disk larger than r. Here I am doing it:

    Clear[lst2, lst3, i, r];
i = 5;
r = 1;

(* This makes a list with a dropped i-th element *)
lstA = lst /. x_ /; x == lst[[i]] -> Nothing;

(* This selects a sublist from the lstA that fulfills the requirement *)

lst1 = Select[lstA, RegionDistance[Disk[lst[[i]], 1], #] >= 2 r &];

For the visual purpose here is a list of points that are closer than 1 to the disk:

lst2 = Select[lstA, RegionDistance[Disk[lst[[i]], 1], #] < 2 r &];

Now let us draw this, showing the points that must be closer in red, and those far - in black:

    Graphics[{Blue, Opacity[0.3], Disk[lst[[i]], 1], Opacity[1], Black, 
  Point[#] & /@ lst1, Red, Point[#] & /@ lst2}]

enter image description here

We see something quite unexpected. Some red points are outside it, though must be inside. Do I strongly miss something?

$\endgroup$
1
  • $\begingroup$ The red dots do not have to be inside: they can be outside as long as the distance to the closest point in the disk is less than r. From the docs of RegionDistance: "Region distance is effectively given by MinValue[{Norm[p-q],q\[Element]reg},q]." $\endgroup$ Commented Jan 18, 2017 at 14:54

3 Answers 3

4
$\begingroup$

You forgot to put the correct origin in the Disk plot. And the distance should be smaler or equal otherwise you dont get point inside (with distance equal to zero).

lst = RandomReal[{-5, 5}, {55, 2}];
Clear[lst1, lst2, i, r];
i = 5;
r = 0;

lst1 = Select[lst, RegionDistance[Disk[lst[[3]], 1], #] >= r &];

lst2 = Select[lst, RegionDistance[Disk[lst[[3]], 1], #] <= r &];

 Graphics[{Blue, Opacity[0.3], Disk[lst[[3]], 1], Opacity[1], Black, Point[#] & /@ lst1, Red, Point[#] & /@ lst2}]

enter image description here

$\endgroup$
1
  • $\begingroup$ You are right. I tried to remove the question, but failed $\endgroup$ Commented Jan 18, 2017 at 17:57
5
$\begingroup$

I think RegionMember is what you are looking for.

lst = RandomReal[{-5, 5}, {55, 2}];

i = 5;
r = 1;

regionDisk = Disk[lst[[i]], 1];

and then

lst2 = Select[lst, RegionMember[regionDisk, #] &]

Graphics[{
  Blue,
  Opacity[0.3],
  regionDisk,
  Opacity[1],
  Black,
  Point[lst],
  Red,
  Point[lst2]
  }]

Mathematica graphics

$\endgroup$
0
$\begingroup$

Other methods could be RegionDisjoint and RegionWithin:

Clear["Global`*"];
SeedRandom[1];
lst = RandomReal[{-5, 5}, {55, 2}];

i = 5;
r = 1;
regionDisk = Disk[lst[[i]], 1];
pts = Point /@ Rest@lst;
ptsinside = Select[pts, RegionWithin[regionDisk, #] &];
ptsoutside = Select[pts, RegionDisjoint[regionDisk, #] &];

Graphics[{
  Opacity[1, Black], regionDisk
  , Yellow, AbsolutePointSize[3], ptsinside
  , Red, ptsoutside
  }
 , Frame -> True
 ]

points inside disk

$\endgroup$
2
  • 1
    $\begingroup$ You could also do it without the Select by consolidating all the points into a region and doing an intersection: pts = Point[Rest@lst]; ptsinside = RegionIntersection[regionDisk, pts]; $\endgroup$
    – flinty
    Commented Nov 30 at 14:07
  • $\begingroup$ Thanks for the tip @flinty. On v12.2.0, I haven't had reliable experiences using RegionUnion and RegionIntersection with mildly complicated regions, so this was probably my unconscious fallback. $\endgroup$
    – Syed
    Commented Nov 30 at 14:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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