Sort Vector Array by Y Value in VEX

   Views 515   Replies 1   Subscribers 0
User Avatar
Member
21 posts
Joined: Oct. 2019
Offline
Hello everyone! I need to reorder an array of vectors from smallest to largest according to the Y values. Is this possible?
I have tried to do it with the usual comparison with nested arrays but for some reason it is not working for me when removing the indexes at run time from the arrays with the pop() function.
The intention is to create an upward curve taking into account the Y values.
I am attaching an example scene with an Add node and an Attribute Wrangle with the simplified code.

Attachments:
SortVectorArrayBy_Y.hipnc (155.4 KB)

User Avatar
Member
318 posts
Joined: Jan. 2013
Offline
Here it will be more convenient to use the argsort function to correctly reorder the order of points in the array.
int roots[] = expandpointgroup(0, "roots");
float y_coords[];
resize(y_coords, len(roots));

for(int i = 0; i < len(roots); ++i){
    y_coords[i] = vector(point(0, "P", i)).y;
}
int ordering[] = argsort(y_coords);
addprim(geoself(),"polyline", ordering);
Edited by alexwheezy - June 29, 2024 02:14:35
  • Quick Links