that’s OK. but you have to understand that the sort function executes all its expressions on every call, so:
fn fnSortValues a b = if (a.dist < b.dist) then -1 else if (a.dist > b.dist) then 1 else 0
will be faster
Hum would this work for you ?
fn sortByOrder order1 order2 =
(
if order1 != undefined and order2 != undefined then
(
Order = (order1.value) > (order2.value)
case Order of
(
true: 1
false: -1
default: 0
)
)
else
(
0
)
)
stArray = #()
valueArray = #(10,5,2,4,1,3,6,9,7,8)
struct theValue (value)
for val in valueArray do
(
theSt = theValue()
theSt.value = val
append stArray theSt
)
print stArray
qSort stArray sortByOrder
print stArray
seems to work for me
He is trying to sort an array of structs based on the value of one of its parameters I did that , care to enlightenment me on what I am missing here? I know I am doing a different comparison using > instead of – but the idea is the same no?
this is wrong:
case a < b of
(
true: -1
false: 1
default: 0
)
it’s sad if you used it in many of your scripts… might be a lot of work to fix …
I could implement this…this scripts creates pipes with a rotated direction towards a targeted object.
Just open the file and run the script.
Try moving around the purple ring and watch the objects be created in the direction of the target. I’m sure Denis will have some clever cleaner way of doing this.
(
local pipeNodes = #($pt_start_01,$pt_start_02,$pt_start_03)
local rots = #(-90,0,90,180)
local trgt = $target
struct ThePoint (node, dist, angle)
fn fnSortValues a b = if (a.dist < b.dist) then -1 else if (a.dist > b.dist) then 1 else 0 --sort the rotations in an order to find the closest position
fn fnRotateTowardsTarget obj = (
results = #()
for m = 1 to 8 do (--Calculate the rotations
obj.transform = (prerotatez obj.transform 45)
dist = distance obj.children[1].pos trgt.pos
append results (ThePoint node:obj dist:dist angle:(m*45))
)
qsort results fnSortValues
print results
obj.transform = (prerotatez obj.transform (results[1].angle)) --apply closest rotation
)
exPipes = #()
clearlistener()
for i = 1 to 10 do (
_thePipe = pipeNodes[random 1 pipeNodes.count]--random pipe
if i == 1 then
(
maxOps.cloneNodes _thePipe expandHierarchy:true cloneType:#copy actualNodeList:&refPipes newNodes:&newPipes
pipeCtrl = newPipes[1]
pipeCtrl.transform = copy (prerotatez (matrix3 1) rots[random 1 rots.count])
ends = for o in newPipes where findString o.name "end" != 0 collect o
join exPipes newPipes[2]
)
else
(
maxOps.cloneNodes _thePipe expandHierarchy:true cloneType:#copy actualNodeList:&refPipes newNodes:&newPipes
pipeCtrl = newPipes[1]
tm = exPipes[exPipes.count].transform
pipeCtrl.transform = copy tm
fnRotateTowardsTarget pipeCtrl
join exPipes newPipes[2]
)
)
print exPipes.count
)
I use the method Denis has posted.
Have you guys been able to check this file out and see how it works?
What the script above does, is it takes every pipe and rotates it on an increment of 45 degrees attempting to find the shorts distance between the ‘end helper’ and the target position. Once it finds the shortest distance it will then use that rotation angle to apply to the pipe end.
it does work for me If I qSort an array of random numbers, it will return a properly sorted array. I have no clue why you keep saying it is wrong as I can visually see it is right, If there is a reason of why you say it is wrong regardless of me getting the proper desired result then please explain. You are much, much better at maxScript then I am and I appreciate the help.
Thank you see I never noticed that and have never run into a problem even when having duplicated values in the array it still worked as it should.
Correct code that now returns 0
fn sortByOrder order1 order2 =
(
if ((order1.value) < (order2.value)) then 1 else if ((order1.value) > (order2.value)) then -1 else 0
)
stArray = #()
valueArray = #(10,5,2,4,1,3,6,9,7,8)
struct theValue (value)
for val in valueArray do
(
theSt = theValue()
theSt.value = val
append stArray theSt
)
print stArray
qSort stArray sortByOrder
print stArray
and this wont be hard at all to fix in my scripts as any time I used this it called onto a function library!
try it if you have enough free time
arr = for k=1 to 10000 collect (random 1 10)
fn sortIt a b = case a < b of
(
true: -1
false: 1
default: 0
)
qsort arr sortIt