How to obtain the minimum and maximum values from a list in Grasshopper

Sometimes you need to know what the minimum and the maximum values are in a list or from a collection of lists. In Grasshopper, there are min and max components which will take two values and return the minimum and maximum respectively, but there isn’t a component for finding the min/max values within a list or multiple lists. There are a couple of methods to achieve this with a workaround by passing data from the lists as individual items and, in the case of multiple lists, running the outputs of individual lists a second time to obtain the final min/max values from all the lists. This is inefficient and may require some coding knowledge if you intend to write a C# script to achieve this. This post aims to provide alternative methods, one being a the C# component code which you can copy and paste, the second being a compiled and built Grasshopper component which you can download and reuse without having to continuously create a C# component every time you need this functionality.

Using Grasshopper components

You can find the min and max values from a list using a two-step process in Grasshopper. You can get the bounds of the list (using the bounds component) and then deconstruct it (using the deconstruct domain component) which gives the start and end of the domain, equivalent to the min and max values of the list.

Finding the min/max values using Grasshopper components

Finding the min/max values using Grasshopper components

C# component

Inputs

If you wish to script your own component to find the min/max values from a list, then you need to begin with a blank C# component on the canvas. Modify the input x to have list access and give it a type hint of double. You can give it a type hint of object and do the cast yourself within the component (both examples will be provided here) and if you have large data sets this may be preferable. James wrote a good post here detailing the speed differences between using a data type and using the generic object class. If you’re looking into increasing the performance of your canvas with your own scripted components, I would recommend reading this.

Outputs

After you’ve modified your inputs, increase your outputs to include a B output (so that you can have the min and the max values coming out – if you only want one value then you can skip this step.

Process of setting up the C# component and how it should look at the end

Process of setting up the C# component and how it should look at the end

Code – using input hint of type double

In the ‘RunScript’ section of the C# component, copy and paste this code.

double min = 1e10;
double max = 0;

for(int a = 0; a < x.Count; a++) {
    min = Math.Min(min, x[a]);
    max = Math.Max(max, x[a]);
}

A = min;
B = max;

Code – using input hint of type object

In the ‘RunScript’ section of the C# component, copy and paste this code.

double min = 1e10;
double max = 0;

for(int a = 0; a < x.Count; a++) {
    double val = Convert.ToDouble(x[a]);
    min = Math.Min(min, val);
    max = Math.Max(max, val);
}

A = min;
B = max;
Getting the min/max values using the C# component

Getting the min/max values using the C# component

Using a pre-made component

If the above options are not your style (or you don’t wish to mess around with C# programming), then you can download this component that I made which will find the min/max values of lists for you.

You can download the component here

Component for finding the min/max values

Component for finding the min/max values

If you find any bugs with this component, or have suggestions for others, feel free to leave a comment or contact me.

Leave a Reply

Your email address will not be published. Required fields are marked *