Notifications
Clear all

[Closed] Ordering QSort Nodes

Is there a better way to organize nodes into groups based on their distances from targeted base nodes?

Test script.


delete objects
--scene setup
point wirecolor:red pos:[-50,50,0] size:10
point wirecolor:green pos:[50,20,0] size:10
point wirecolor:yellow pos:[-15,-30,0] size:10
fn Rand = (return (random -80.0 80.0))
for i = 1 to 30 do 
(
	plane lengthsegs:1 widthsegs:1 length:15 width:15 pos:[Rand(), Rand(), 0] wirecolor:gray
)
childObjs = $Plane*
baseObjs = $Point*
clearlistener()


for c in childObjs do
(
	distArr = #()
	for b = 1 to baseObjs.count do
	(
		append distArr (distance c baseObjs[b])
		min = amin distArr
		closestObj = baseObjs[finditem distArr min]
		
		if (finditem c.children closestObj) > 0 then
		(
			deleteitem c.children closestObj
		)
		else()
		c.wirecolor = closestObj.wirecolor
	)
)

2 Replies

Hi John, try this:

delete objects
--scene setup
point wirecolor:red pos:[-50,50,0] size:10
point wirecolor:green pos:[50,20,0] size:10
point wirecolor:yellow pos:[-15,-30,0] size:10
fn Rand = (return (random -80.0 80.0))
for i = 1 to 100 do 
(
	plane lengthsegs:1 widthsegs:1 length:15 width:15 pos:[Rand(), Rand(), 0] wirecolor:gray
)
childObjs = $Plane*
baseObjs = $Point*
clearlistener()


for c in childObjs do
(
	bestDist = distance c baseObjs[1]
	c.wirecolor = baseObjs[1].wirecolor
	for i = 2 to baseObjs.count do
	(
		local distTmp = distance c baseObjs[i]
		if (distTmp < bestDist) then
		(
			bestDist = distTmp
			c.wirecolor = baseObjs[i].wirecolor
		)
	)
)

I like this much better than mine.
Thanks for the help.