MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Using Nearest on a group of points

  • To: mathgroup at smc.vnet.net
  • Subject: [mg117585] Re: Using Nearest on a group of points
  • From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
  • Date: Wed, 23 Mar 2011 02:56:15 -0500 (EST)

Hi,

you could manage two lists: one which is already sorted und one which
contains the remaining unsorted points. Use Nearest to extract the next
point, append it to the sorted list and remove it from the remaining
points:

findNextPoint[{sorted_, unsorted_}] :=
	With[{id =
	 Nearest[Thread[unsorted -> Range[Length[unsorted]]],
	Last@sorted]}, {Join[sorted, unsorted[[id]]],
	 Delete[unsorted, id]}];
findNextPoint[{sorted_, {}}] := sorted;
findNextPoint[sorted_] := sorted;

FindPath[pts_, startpt_] := FixedPoint[findNextPoint, {{startpt}, pts}]

With[{pts = Table[{Cos[phi], Sin[phi]}, {phi, 0, Pi, Pi/20.}]},
	pstart = First[pts];
	punsrt = RandomSample[Rest[pts]];];
Graphics[Arrow /@ Partition[Prepend[punsrt, pstart], 2, 1]]
Graphics[Arrow /@ Partition[FindPath[punsrt, pstart], 2, 1]]

Cheers
Patrick


On Tue, 2011-03-22 at 05:10 -0500, Martin VavpotiÄ? wrote:
> Hello. I need some help with the function Nearest.
>
> I have a groups of points, each with two coordinates (x,y), describing
> a connected shape (the last point is a neighbor to the first one). The
> initial order of these points is completely scrambled but I need them
> to follow one another so their sequence describes a combined shape. I
> thought of using the Nearest function but there is a problem.
> Somewhere in the middle of my shape is a large gap where no points
> reside. I fear that if I use Nearest, the function will find the wrong
> point.
>
> What I need is for function Nearest to ignore points already sorted
> and search only for points that have not been used yet.
>


  • Prev by Date: Re: Chop in Mathematica 8.0.1
  • Next by Date: Re: "set" data structure in Mathematica? (speeding up
  • Previous by thread: Re: Using Nearest on a group of points
  • Next by thread: Re: Using Nearest on a group of points