Grasshopper

algorithmic modeling for Rhino

Say i have a list of point i want to sort them by value first the by there X coordinates 

Anyone have any ideas 

Views: 3325

Replies to This Discussion

Sort Points component. Sorts by X first, Y second and Z third. Note that points that seem to have the same X coordinate when you look at the numbers through the Grasshopper UI may in fact differ in the least significant digits. Ie. 3.500000000000001 and 3.4999999999997 will sort different even though both will look like 3.5 in the UI.

The more generic answer to your question about how to sort using more than one key is actually rather complicated. There are two common approaches when doing that in code:

  1. Write a custom Comparer method which may contain any amount of code, be it simple or complicated.
  2. Convert your multiple sorting keys into a single number.

The first approach is the most flexible but also very difficult to implement in Grasshopper. It's not really possible to define methods as component networks and use those networks as input for other components. Sometimes you can do it, sometimes you can't and actual VB, C# or Python code is needed.

The second option is probably easier. You need to combine your multi-variate properties into a single value (either a number or a string) which then becomes the sorting key. For example you could use

k = x * 1000 + y / 1000

where k is the sorting key, and x and y are the point coordinates. By multiplying x by a large number and dividing y by a large number, you can weigh the relative importance of the x and y properties. A tiny difference in x will result in a big difference in k, whereas a large difference in y will result in a small difference in k. This is not a mathematically watertight approach, when two xs are very similar and the ys are very different, the sort order will not be as expected.

There is another way to sort by multiple values, as noted in this post two weeks ago.  You can create fake points where the X, Y and Z values are assigned arbitrary values before using 'Sort Pt (Sort Points)', which sorts by X/Y/Z.  In the example, I reversed X and Y so now 'Sort Pt' effectively sorts by Y first and then X but you would use some other arbitrary value instead:

For example, this code sorts first by the volume of the spheres and then by their X value:

Attachments:

Sorry, using a sphere radius of zero obviously doesn't work very well!  Fixed.

Attachments:

Not that it matters for the purpose of demonstrating this multi-value sort method but there were extraneous 'List Item'/'Round' components in the previous code that distorted the random distribution of sphere radii due to truncation instead of rounding.  This is the result I intended - choosing randomly between three possible radius values:

Attachments:

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service