Group Abstract Group Abstract

Message Boards Message Boards

0
|
10K Views
|
4 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Counting points in a Graphic

Posted 11 years ago

Hey, I've been set a coding task at uni for my project and prefer to use Mathematica, was wondering if there was anyone who could help!

Is there is a way to set up a script that basically takes a graph of hundreds of points, creates a small circle at a point on that graph and then counts the points inside it. The loop would then create another circle slightly bigger and count those points within (I need two, one that cumulatively adds up the points and one that annularly does it), so that eventually one can plot a graph of radius of the circles against the points counted.

Thanks very much!

Lewis

POSTED BY: Lewis Millar
4 Replies
Posted 11 years ago
(* a list of 1000 points in a range *)
points = RandomReal[{-100, 100}, {1000, 2}];

(* select all points within distance of a point and count them *)
count[center_, distance_] := 
 Length[Select[points, Norm[# - center] <= distance &]]

(* make a table of these counts over a range and plot them *)
Table[{d, count[{0, 0}, d]}, {d, 1, 250, 5}] // ListPlot

enter image description here

(* choose a different center and do it again *)
Table[{d, count[{50, 50}, d]}, {d, 1, 250, 5}] // ListPlot

enter image description here

POSTED BY: David Keith

I assume you have generated the graph from a list of points. If so, then perhaps the best place to start is with a function that acts on a set of 2-dimenstional cartesian coordinates and determines the ones that are within a circle of radius r.

Let's say you have a bunch of points specified by their {x,y} coordinates. Here is an example of some random points within a square centered at the origin with sides of length 20 :

In[1]:= myPoints = RandomReal[{-10, 10}, {10, 2}]

Out[1]= {{9.46899, 0.761273}, {-6.23467, 0.511913}, {7.35649, 
  2.65914}, {2.29141, -4.28594}, {-1.70496, 0.849794}, {5.71018, 
  2.75263}, {3.8676, 3.89367}, {-2.3946, 
  8.70738}, {2.98544, -8.53176}, {-5.03731, -0.288273}}

Then the points contained within a circle of radius r can be determined by using the following function or something like it:

pointsInR[points : {{_, _} ..}, r_ /; r > 0] := Select[points, Sqrt[#.#] < r &]

Let's try it out:

In[5]:= pointsInR[myPoints, 8]

Out[5]= {{-6.23467, 0.511913}, {7.35649, 
  2.65914}, {2.29141, -4.28594}, {-1.70496, 0.849794}, {5.71018, 
  2.75263}, {3.8676, 3.89367}, {-5.03731, -0.288273}}

And the number of points within a radius r is given by the Length of this list:

In[9]:= Length[pointsInR[myPoints, 8]]

Out[9]= 7

And

Graphics[{Point[myPoints], Circle[{0, 0}, 8]}, Frame -> True]

Gives

enter image description here

POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard
Be respectful. Review our Community Guidelines to understand your role and responsibilities. Community Terms of Use