Radial Sort In Line

I'd like to do a radial sort that apply an change on the first object and then increaingly all the way around till i reach the last object.

Check out the attached image to make more sense.

For example: I have an array of sphere and I'd like to change there diffuse color from white to black based on the radial array. Check out the image to make more sense. For the sake of the image i just did it by hand.

Thanks

JokerMartini

AttachmentSize
radialsort.png24.87 KB

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
JokerMartini's picture

I need to figure out how to

I need to figure out how to return an array which is the order of splines in a shape, but in an order based on the radial sorting of the first knot in each spline.

I've hit this wall and I've been at it for the past few days and I'm now seeking some help from anyone. I hope that makes sense. I've attached an image to help explain incase your up for the challenge.
You may be wondering why I don't just create the splines in a radial order but that is because they are randomly created based off ob particle positions.

Anyways, the knot circled in red is just to show which knot is the first knot in the spline. The number by each spline shows which spline in the splineshape it is.
So ideally the returned array if radial sorted properly wold return this
#(1,3,5,6,2,7,4,8)

AttachmentSize
radialsplines.png 21.29 KB

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

barigazy's picture

The Solution

Try this code on attachment file. Select any point helper and run code

fn RemoveItemFromArray arr itm =
(
	local idx = findItem arr itm
	if idx == 0 then false else deleteItem arr idx
)
pointArr = $Point* as array
if selection.count == 1 do
(
	posArr = #(selection[1].center)
	itm = selection[1]
	while pointArr.count != 1 do
	(
		minDist = amin (for p in pointArr where p != itm collect distance itm.center p.center)
		format "minDist = %\n" minDist
		nextnode = (for p in pointArr where p != itm and (distance itm.center p.center) == minDist collect p)[1]
		pointArr = RemoveItemFromArray pointArr itm
		itm = nextnode ; append posArr itm.center
	)
	format "posArr = %\n" posArr
)
AttachmentSize
testfile.zip 20.99 KB

bga

JokerMartini's picture

Great. Just what I needed.

Great. Just what I needed. Let me play around with this code and see what I can do.

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

JokerMartini's picture

Any?

Has anyone been able to find the best way to radial sort. These seem to only work if the objects are on the x and y.

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

josepssv's picture

Using Raphael js

Using Raphael js and Jsfiddle
http://jsfiddle.net/SquDT/
Thanks!

JokerMartini's picture

hey

Nice test result.

AttachmentSize
startscene3-result.max 232 KB

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

JokerMartini's picture

Idea

It would be cool to make a pickbutton allowing a user to choose the first object in the radial array to start from.

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

JokerMartini's picture

Results from Anubis's latest version

(
	clearListener()
 
	local theObjects = getCurrentSelection()
    local count = theObjects.count
    local center = [0, 0, 0]
    for obj in theObjects do center += obj.pos
    center /= count
 
	fn angularSort obj1 obj2 = (
		in coordSys (transMatrix center)
			obj1[3][2][3].value - obj2[3][2][3].value
	)
 
	-- create points (in case the objects has not Z_Rotation controller)
	tmpPoints = for obj in theObjects collect
		Point pos:obj.pos name:(obj.name+"_")
 
	qsort tmpPoints angularSort
 
	sortedArray = #(); sortedArray.count = theObjects.count
	for i = 1 to tmpPoints.count do (
		sortedArray[i] = getNodeByName (trimRight tmpPoints[i].name "_")
 
 		for i = 1 to theObjects.count do
		(
			obj = selection[i]
			obj.wirecolor = [i*10,i*10,i*10]
			moveKeys Obj (i*.1)
		) 
	)
 
	delete tmpPoints
	print sortedArray
)
AttachmentSize
startscene.max 240 KB

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

Anubis's picture

haha, fun...

I end with very fun anim result using attached scene :) I see you loop through in input/unsorted array theObjects insted of sortedArray, but nm, forgot this version and read Garp notes.

AttachmentSize
startscene2.max 80 KB

my recent MAXScripts RSS (archive here)

JokerMartini's picture

Garp

Quick Testing result to see the ordering based on wirecolor

(
    local theObjects = getCurrentSelection()
    local count = theObjects.count
    local center = [0, 0, 0]
    for obj in theObjects do center += obj.pos
    center /= count
 
    point pos:center
 
    fn angularSort obj1 obj2 =
    in coordSys (transMatrix center)
    (
        local ang1 = atan2 obj1.pos.y obj1.pos.x
        local ang2 = atan2 obj2.pos.y obj2.pos.x
        ang1 - ang2
    )
 
    qsort theObjects angularSort
 
    for i = 1 to count do format "%\n" theObjects[i].name
	for i = 1 to theObjects.count do
	(
		obj = selection[i]
		obj.wirecolor = [i*30,i*30,i*30]
	)
)

John Martini
Digital Artist
http://www.JokerMartini.com (new site)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.