4

I have seen this question, but what I want is a bit different.

I have a set of data (blue points) and I would like to end up with something like this:

enter image description here,

i.e. joining each site with all its (nearest) neighbours.

The order of the data points is random so if I just do Joined->True it plots lines at random.

I know I can use Nearest[], so I added a line

x0 = Neartest[data]

which outputs a set of sets of numbers (the nearest neighbours), but am not quite sure how to plot that on top of the original points WITH segments in between.

Any pointers?

Thanks


if you want, the data is:

data= Uncompress@Import["http://pastebin.com/raw.php?i=qvMp8BNv"]
SuperCiocia
  • 1,472
  • 8
  • 15
  • 2
    Look up DelaunayMesh[]. – J. M.'s missing motivation Nov 12 '15 at 13:12
  • 2
    What exactly do you means by "all its nearest neighbors"? It is important to define this precisely. If you connect each dot to the one that is closes to it, you will not end up with the sort of figure you have drawn. Typically each dot will have a single nearest neighbour. BTW NearestNeighborGraph will do just that. – Szabolcs Nov 12 '15 at 13:15
  • Yeah I realise that's a bit misleading sorry. I meant all the points around a given datum... – SuperCiocia Nov 12 '15 at 13:31
  • I think DelaunayMesh is the one I want. By chance, is there any way of telling it what polygons to fit? Eg. "only try to draw pentagons" – SuperCiocia Nov 12 '15 at 14:01
  • I don't think that it does, since it performs a Delaunay triangulation which exclusively generates triangles. – IPoiler Nov 12 '15 at 15:44
  • Delaunay may be what he really wants, but is not the same as simply connecting n nearest neighbors. – george2079 Nov 12 '15 at 15:55

1 Answers1

7

here is the nearest neighbors plot:

pts = RandomReal[{-1, 1}, {200, 2}];
near = Nearest[pts];
Graphics[{Function[{pt}, Line[{pt, #}] & /@ near[pt, {5, .2}]] /@ pts,
         PointSize[.015], Red, Point@pts}]

this is up to 5 near neighbors within radius of .2..

enter image description here

for comparison DelaunayMesh[pts]

enter image description here

george2079
  • 38,913
  • 1
  • 43
  • 110