Notifications
Clear all

[Closed] array sort objects by position

there is some serious mistake in my algorithm. i have to review it. as i have a time i will show the fixed version.

If I missed the meaning of the task, please excuse me in advance.
The main aim is to delete objects with very closer position (using tolerance precision), right?

But what if we have objects with linear distribution with regular offset of 1 unit (no matter in what scale) and we compare the distance using tolerance 2 units (in the same scale)? Then, in this case, the script will gather everything, so will delete all objects at the end. I wonder, is that a part of expected result?
In that case I’ll expect to be deleted just each second object (not all of them).

In other words, if I get it right (of’cuz), the logic of that script s’d be different.
The optimization of the code is (s’d be) the last task, right?

Ok, to present my thoughts quite understandable, I’ll illustrate this with a very simple example.
Here we go an array of 10 points distributed with offset of 1 unit along Z axis:

nodes = for i = 1 to 10 collect (Point pos:[0,0,i])

We know here how they’re distributed and can do “delete(for n=1 to nodes.count by 2 collect nodes[n])”. But you know, this is just for illustration and clearness, so we need some generic function for any kind of distribution, and here is what I have in mind:

mapped fn getCloser arr obj tolerance &out = (
	if (distance arr.pos obj.pos) < tolerance do append out arr
)

And now the interesting part.
I think that deleting on steps is good enough. (feel free to optimize this)
Here is an example recursive function that handle all procedure:

fn DelNodesByPos nodes tolerance = (
	local n = nodes[1], tmp = #()
	deleteItem nodes 1
	getCloser nodes n tolerance &tmp
	delete tmp
	nodes = for i in nodes where IsValidNode i collect i
	if nodes.count > 1 do DelNodesByPos nodes tolerance
	OK
)

Now if I call my function with tolerance 2, it will delete each 2nd object:

DelNodesByPos nodes 2

Ah, and I fully skip the optimization part at this point.
So as I said, if I missed the goal here, let me know.

P.S. – just to add note –
I not checked Denis function, too much maths there for my brain

1 Reply
(@raytracer05)
Joined: 11 months ago

Posts: 0

You have understood the problem correctly and you’re right the solution I posted fails this test as it deletes all but the first object.

I like the idea of a recursive function, I with I’d thought of that.

Page 2 / 2