Currently I'm coding a Vector
class in C# and I'm coming to the point, where I've to figure out, how I want to implement a function/method to interpolate between two vectors. The (obvious) and also my first solution was to simply implement it as a method in the vector class itself:
public class Vector3D
{
public static Vector3D LinearInterpolate(Vector3D vector1,
Vector3D vector2, double factor) { ... }
public Vector3D LinearInterpolate(Vector3D other, double factor { ... }
}
(I decided to offer both a static method with two vectors as parameters and one non-static, with only one vector as the parameter)
But then I got the idea to use extension methods (defined in a separate class called e.g. Interpolation
for example), since interpolation isn't really limited to vectors only. So this might be another approach:
public class Vector3D { ... }
public static class Interpolation
{
public static Vector3D LinearInterpolate(this Vector3D vector,
Vector3D other, double factor) { ... }
}
And here is an example showing how one would use the different implementations:
{
var vec1 = new Vector3D(5, 3, 1);
var vec2 = new Vector3D(4, 2, 0);
Vector3D vec3;
vec3 = vec1.LinearInterpolate(vec2, 0.5); //1
vec3 = Vector3D.LinearInterpolate(vec1, vec2, 0.5); //2
//or with extension-methods
vec3 = vec1.LinearInterpolate(vec2, 0.5); //3 (same as 1)
vec3 = Interpolation.LinearInterpolation(vec1, vec2,
0.5); //4
}
I can't really decide on which design is preferable. Is there a rule of thumb on how to implement methods/functions similar to the one above or is it rather a matter of preference? I really would like to hear your opinions on what's better and - if possible - why.