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.
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.