I think there are 2 issues here.
- Your ranges don't look right.
- You performing integer division, and it's not going to work out well.
For the operands of integer types, the result of the /
operator is of an integer type and equals the quotient of the two operands rounded towards zero... And, I know in the cockles of my heart you don't want this.
Given
private static Vector2 GetPointByInterpolation(ReadOnlySpan<Vector2> controlPoints, float interpolation)
{
if (interpolation < 0 || interpolation > 1)
throw new ArgumentException("value can only range from 0 to 1",nameof(interpolation));
if (controlPoints.Length == 0)
throw new ArgumentException("doesn't contain any points",nameof(controlPoints));
if (controlPoints.Length == 1)
return controlPoints[0];
// first to last - 1
var p1 = GetPointByInterpolation(controlPoints[0..^1], interpolation);
// second to last
var p2 = GetPointByInterpolation(controlPoints[1..], interpolation);
var nt = 1 - interpolation;
return new Vector2(nt * p1.X + interpolation * p2.X, nt * p1.Y + interpolation * p2.Y);
}
Note : I am using ReadOnlySpan
, because well... why not, it's super efficient to slice and can use C# ranges.
Usage
var controlPoints = new[]
{
new Vector2(0, 0),
new Vector2(0, 100),
new Vector2(100, 100)
};
// take special note of (float)100, we are now performing floating point division
for (int i = 0; i < 100; i++)
Console.WriteLine(GetPointByInterpolation(controlPoints.AsSpan(), 1 / (float)100 * i));
Disclaimer : I have never written python code in my life (and don't want to start :p), Also I have no idea of what a Recursive Bezier Curve Algorithm actually does, so I am not really sure if the ranges are right, or if there is anything else wrong with the code.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…