Re: Using Nearest on a group of points
- To: mathgroup at smc.vnet.net
- Subject: [mg117614] Re: Using Nearest on a group of points
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Thu, 24 Mar 2011 06:31:01 -0500 (EST)
Thinking about it further it's ambiguous what sort means. Does the closest point to the first point in the list go at the end of the sorted list or in the second position? On Mar 23, 2011, at 5:11 PM, DrMajorBob <btreat1 at austin.rr.com> wrote: > Run this a few times: > > loop = Append[#, First@#] &; > pts = RandomInteger[{1, 15}, {10, 2}]; > ch = pts[[loop@Quiet@ConvexHull@pts]]; > sorted = loop@sortPoints@pts; > Show[ListPlot[{ch, sorted}, Joined -> True, > PlotStyle -> {{Red, Thickness[.01]}, {Blue, Thickness[.015]}}], > Graphics at {PointSize[.02], Line@pts}] > > Darned if I can tell that any "shape" is really being captured. > > Bobby > > On Wed, 23 Mar 2011 02:55:09 -0500, Sseziwa Mukasa <mukasa at jeol.com> wrote: > >> >> On Mar 22, 2011, at 6:10 AM, Martin Vavpoti==C4==8D 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. >> >> >> If your list isn't too long you can do it recursively: >> >> sortPoints[{x_}] := {x} >> >> sortPoints[points_] := >> With[{index = >> First[Nearest[Rest[points] -> Automatic, First[points]]]}, {First[points], >> Sequence @@ >> sortPoints[Prepend[Drop[Rest[points], {index}], points[[index + 1]]]]}] >> >> (Debug) In[63]:= sortPoints[RandomInteger[{1, 10}, {10, 2}]] >> (Debug) Out[63]= {{10, 1}, {8, 3}, {9, 5}, {8, 8}, {8, 9}, {10, 10}, {4, 9}, {2, 10}, {4, >> 4}, {2, 2}} >> >> Regards, >> Ssezi >> > > > -- > DrMajorBob at yahoo.com