ReMap values from list to new Min Max Interval - c# code

Hi,

Another newbie question for coding. I understand how to remap values from one interval to a new interval in c# using the Linq methods, but I am having trouble getting that to work in C# for Gh and using a list of distances already created in my code.

So, I found the distances from a focus point (focus) to all other points in a grid using Distance.Add(p.distanceto(focus)), and so now I have output B which is all the distances calculated.

I now want to remap that outputted list of distances to a new Min/Max interval, for which I have created inputs w/ number sliders in the component.

How exactly do I write the code to use the Min/Max from the slider and the list of distances from my code? I have tried something like :
public static double mapToUnit(double x, double min, double max)
{
return (x - min) / (max - min);
}
or
public double MapValue(double a0, double a1, double b0, double b1, double a)
{
return b0 + (b1 - b0) * ((a-a0)/(a1-a0));
}

but I cannot get them to work with my inputs/outputs.

Thank you!

To remap the values between 0 and 10, you would use the second function:

You would need the minimum distance as a0 and maximum distance as a1. b0 = min and b1 = max.

hi, thanks, what would a be then?

The distance you are checking at the time.

I feel totally dumb right now, but it is totally not working and I do not know why. I have done it like this:

public double MapValue (double min, double max, double distance.Min, double distance.Max, double x)
{
return double distance.Min + (distance.Max - distance.Min) * ((x - min)/(max - min));
}

and I am getting all sorts of syntax errors like “,” is invalid, “;” expected, invalid expression term “double”, etc.

Can you post the whole code /gh script to the forum. Just drag and drop it into your reply.
Makes it easier to work with.


Thank you so much, by the way. It is #7, in question :slight_smile:
03_Points_Assignment_rsm278.gh (24.8 KB)

Typehint for min and max need to be set to double.

using System;
using System.Collections;
using System.Collections.Generic;

using Rhino;
using Rhino.Geometry;

using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;

using System.Linq;

/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
  /// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
  /// <param name="text">String to print.</param>
  private void Print(string text) { /* Implementation hidden. */ }
  /// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
  /// <param name="format">String format.</param>
  /// <param name="args">Formatting parameters.</param>
  private void Print(string format, params object[] args) { /* Implementation hidden. */ }
  /// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj) { /* Implementation hidden. */ }
  /// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
  /// <param name="obj">Object instance to parse.</param>
  private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion

#region Members
  /// <summary>Gets the current Rhino document.</summary>
  private readonly RhinoDoc RhinoDocument;
  /// <summary>Gets the Grasshopper document that owns this script.</summary>
  private readonly GH_Document GrasshopperDocument;
  /// <summary>Gets the Grasshopper script component that owns this script.</summary>
  private readonly IGH_Component Component;
  /// <summary>
  /// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
  /// Any subsequent call within the same solution will increment the Iteration count.
  /// </summary>
  private readonly int Iteration;
#endregion

  /// <summary>
  /// This procedure contains the user code. Input parameters are provided as regular arguments,
  /// Output parameters as ref arguments. You don't have to assign output parameters,
  /// they will have a default value.
  /// </summary>
  private void RunScript(Point3d Pt1, Point3d Pt2, int columns, int rows, Point3d focus, object min, object max, ref object A, ref object B)
  {
    // Make a list of points
    List<Point3d> points = new List<Point3d>();
    List<double> distance = new List<double>();

    // Deifine the step in Y and X, using the two points as the bounds
    double stepY = (Pt2.Y - Pt1.Y) / (rows - 1.0);
    double stepX = (Pt2.X - Pt1.X) / (columns - 1.0);

    // Loop to create the rows
    for(int j = 0; j < rows; ++j) {
      double y = Pt1.Y + j * stepY;
      // loop to create columns
      for(int i = 0; i < columns; ++i) {
        double x = Pt1.X + i * stepX;

        //create points
        Point3d p = new Point3d(x, y, 0.0);
        // add points and measure distance to focus point
        points.Add(p);
        distance.Add(p.DistanceTo(focus));
      }
    }
    double Min = distance.Min();
    double Max = distance.Max();
    for(int i = 0; i < distance.Count;i++)
    {
      distance[i] = MapValue(min, max, Min, Max, distance[i]);
    }


    A = points;
    B = distance;
  }

  // <Custom additional code> 
  public double MapValue (double min, double max, double Min, double Max, double x)
  {
    return Min + (Max - Min) * ((x - min) / (max - min));
  }
  // </Custom additional code> 
}

MapValue is a function and should not be put within the Runscript as you call it multiple times.
You needed to get the min and max value before remapping.
The remapping takes place after you find all the distances so you know the actual minimum and maximum distance.


You need to connect the minimum and maximum to the lower and upper limits for the gradient to colour properly.

Updated script:
03_Points_Assignment_rsm278.gh (28.9 KB)

Seventh post before showing your geometry?

He did say to use #7 group

Thanks so much for all the help, it really helped me to understand this!