I copied the exact algorithm in this post but somehow it's not working in C# Recursive function of Bezier Curve python. Here's my code:
private static Vector2 GetPointByInterpolation(List<Vector2> controlPoints, float interpolation)
{
if (interpolation < 0 || interpolation > 1)
{
throw new ArgumentException("'interpolation' value can only range from 0 to 1");
}
if (controlPoints.Count == 0)
{
throw new ArgumentException("'controlPoints' doesn't contain any points");
}
if (controlPoints.Count == 1)
{
return controlPoints[0];
}
else
{
Vector2 p1 = GetPointByInterpolation(controlPoints.GetRange(0, controlPoints.Count - 1), interpolation);
Vector2 p2 = GetPointByInterpolation(controlPoints.GetRange(1, controlPoints.Count - 1), interpolation);
return (1 - interpolation) * p1 + interpolation * p2;
}
}
private static void Main(string[] args)
{
List<Vector2> controlPoints = new List<Vector2>
{
new Vector2(0, 0),
new Vector2(0, 100),
new Vector2(100, 100)
};
for (int i = 0; i < 100; i++)
{
Console.WriteLine(GetPointByInterpolation(controlPoints, 1 / 100 * i));
}
Console.Read();
}
I tested the algorithm in the link above and it's working as expected, but after I rewrote it in C#, the function was always returning the first point in controlPoints
. I suspected the problem is because Vector2
is a value type, but that doesn't seem to be the case.