6
$\begingroup$

I have a two-dimensional mesh that I am visualizing in Mathematica, with ListPlot[]. The mesh looks like this:

enter image description here

Any ideas for how to connect all the nearest neighboring points with lines?

Thanks, ahead of time.

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Dr. belisarius Nov 21 '14 at 1:32
  • $\begingroup$ If the purpose is to recreate some triangulated mesh from points - you can use e.g. DelaunayMesh. $\endgroup$ – funnyp0ny Nov 21 '14 at 21:34
  • $\begingroup$ Thanks! This was my purpose. I will keep this in mind in the future. I actually wound up figuring a way to visualize the mesh within the program I started using to create the mesh. "gmsh". But this will still be useful for me in the future! $\endgroup$ – wgwz Nov 22 '14 at 18:26
11
$\begingroup$

ListPlot[] isn't the "right" tool. It can be done with Epilog ->, but it's more natural to use Graphics[] and Nearest[]:

(*  Generate a distribution similar to your example *)
n = 1000;
rs = RandomVariate[TransformedDistribution[Sqrt@x,x\[Distributed] UniformDistribution[{.1, 1}]], n];
phis = RandomReal[{0, 2 Pi}, n];
pts = #1 {Cos@#2, Sin@#2} & @@@ Transpose[{rs, phis}];

(* Find the lines and plot them*)
p = Nearest[pts];
pts1 = p[#, 2][[2]] & /@ pts;
Graphics[{Point@pts, Line[{##}] & @@@ Transpose[{pts, pts1}]},  Axes -> True]

Mathematica graphics

The standard doc for Nearest[] is not complete. This one is much better.

If you want something like "percolation style" neighbors (almost, but not exactly), you can do:

k = {};
AppendTo[k, {#, (h = p[#, 100])[[2 + 
        Sum[Boole@MemberQ[k, {h[[i]], #}], {i, 2, 100}]]]}] & /@ pts;

Mathematica graphics

$\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.