Sorting position array by shortest distance
hi,
i would like to ask how to make sorting faster.
lets say that the posArr is collection of random positions and I need to sort them by distances so that>
posArr[n + 1] asi clossest point to posArr[n]
and
posArr[n + 2] asi clossest point to posArr[n + 1]
and so on....
-- random positions 1000 posArr = for i = 1 to 1000 collect random [-100,-100,0] [100,100,200] fn sortByMin v1 v2 mN:[-100,-100,0] = ---- sorting by minimum ( local d1 = distance v1 mN local d2 = distance v2 mN case of ( (d1 < d2) : -1 (d1 > d2) : 1 default: 0 ) ) fn compareDistance v1 v2 pA:#() pass:1 = --- sorting by distance to pos ( if pass > 1 then ( local p0 = pA[pass-1] ) else local p0 = pA[pass] local d1 = (distance v1 p0) local d2 = (distance v2 p0) case of ( (d1 < d2) : -1 (d1 > d2) : 1 default: 0 ) ) fn compareNeighbArr pA = ( --- for loop for search at next array index qsort pA sortByMin for i in 1 to pA.count-1 do ( qsort pA compareDistance pA:Pa pass:i start:i ) ) clearlistener() st = timestamp() compareNeighbArr posArr t= timestamp() - st format "sorted in % s " (t / 1000.0) format "%" (distance posArr[1] posArr[2]) -- test 1 2 format "%" (distance posArr[1] posArr[3]) -- test 1 3
----
right now it takes 11 seconds for 1000 member array
but 45 seconds for 2000 array
And I need to make it much much faster
Comments
`
Hi,
If you still need it, I tried some sorting methods and I got to 2s for 1000 array and 10s for 2000 array,
but trying to sort :
posArr[n + 1] asi clossest point to posArr[n]
and
posArr[n + 2] asi clossest point to posArr[n + 1]
and so on....
In some point the closest pos to previous is totaly on other side of space/line...
When I quickly tried your method it sorted only in growing order starting closest to 0... (without finding smallest distance between 2 points)
For eg this random Z axis 0 to 200:
posArr = for i = 1 to 1000 collect random [100,100,0] [100,100,200]
It sorted only z positions like this: 0.1,0.5,1,1.25,5,10,15,16,22,28,39...
After some tests I got something like this:
Hope it helps a bit,
Best,
`
ah, maybe this is better
it sorts 2000 array in 9s
But it still find smallest distance on beggining and then look for next points.
if you to declare 0 position [100,100,0]
then it can be 3s for 2000 array:
Hope it helps,
Pixamoon
...
What about this? I use 10000 pos
-->time:0.384 memory:1504L
bga
`
I think he ask for faster sorting by distance between points in array, not to one _min point.
but maybe not
But I've just learned dataPair from your comment! good to know, Thanks