# Linear DATA Inperpolation in C#

Hi C# gurus,

Could you help me to reproduce the Interpolate Data component but with 2 inputs to avoid this unnecessary pairing.

I tried to use the linear interpolation in C#, but how can this work with points and numbers (like the standard one).

Another issue that this C# takes a lot of time calculation a kind of bottleneck because of the implicit formula. I would like to optimize it as well.

Thank you!

Linear Data Interpolation.gh (219.6 KB)

Linear Data Interpolation.gh (208.8 KB)

Points3D accepts + - / * operators both in Grasshopper canvas AND Rhinocommon/c# script
(apart from mupliplying or dividing one point with anotherâ€¦)

To make it accepts points and numbers you have to make the inputs enter as generic â€śobjectâ€ť and only later cast them as points or numbers (â€śx = (int) y;â€ť is casting object y to x as int type).

Also, to make it fast youâ€™ll want to have the c# script load its input/sources as Lists:

Note: I created a pre-determined size array to store results (object`[]` c = new object[n];); this is to ensure that each thread/process works without the risk of â€ścollidingâ€ť with others (for example by accessing the same object simultaneously, causing your pc to explode).

Code:

``````using System.Threading.Tasks;

private void RunScript(List<System.Object> A, List<System.Object> B, List<double> t, ref object C)
{
int n = A.Count;
object[] c = new object[n];
Parallel.For(0, n, i => {
//for(int i = 0;i < n;i++){
if(A[i] is double && B[i] is double){
double a = (double) A[i];
double b = (double) B[i];
c[i] = (b - a) * t[i] + a;
}
if(A[i] is Rhino.Geometry.Point3d && B[i] is Rhino.Geometry.Point3d){
Rhino.Geometry.Point3d a = (Rhino.Geometry.Point3d) A[i];
Rhino.Geometry.Point3d b = (Rhino.Geometry.Point3d) B[i];
c[i] = (b - a) * t[i] + a;
}
//}
});
C = c;
}
``````
1 Like

Thank you for the detailed explanation @maje90.
This works really great! and how about in the case that I just want to use a single value in the â€śtâ€ť parameter,
Do I need to clone a single value with my list for it to work?
Thank you again!

Disable c# component.
Set c# input â€śtâ€ť as â€śitem accessâ€ť.
Edit code from â€śt[i]â€ť to just â€śtâ€ť.
Enable c# component.