Half day at work today (all our servers are moving to another state) so I took the time to write some code to demonstrate:
public partial class Form1 : Form
{
Point[] bezPoints;
Point[] keyPoints = new Point[] {
new Point(50,20),
new Point(200, 40),
new Point(100, 150),
new Point(200, 200)};
public Form1()
{
bezPoints = GenerateBezierPoints(keyPoints, 40);
}
Point[] GenerateBezierPoints(Point[] keyPoints, int totalPoints)
{
Point[] result = new Point[totalPoints];
int degree = keyPoints.Length - 1;
int[] factorials = new int[degree]; // i! is at factorials[i-1]
int[] binomialCoefficients = new int[degree + 1];
factorials[0] = 1; // 1! = 1
binomialCoefficients[0] = binomialCoefficients[degree] = 1;
for(int i = 1; i < degree; i++)
factorials[i] = factorials[i - 1] * (i + 1);
for (int keyIndex = 1; keyIndex < degree; keyIndex++)
{
binomialCoefficients[keyIndex] = factorials[degree - 1] /
(factorials[keyIndex - 1] * factorials[degree - 1 - keyIndex]);
}
for (int ptIndex = 0; ptIndex < totalPoints; ptIndex++)
{
for (int keyIndex = 0; keyIndex < keyPoints.Length; keyIndex++)
{
double t = (double)ptIndex / (double)(totalPoints - 1);
double weight = Math.Pow(1 - t, keyPoints.Length - 1 - keyIndex) *
Math.Pow(t, keyIndex);
weight *= binomialCoefficients[keyIndex];
result[ptIndex].X += (int)(weight * keyPoints[keyIndex].X);
result[ptIndex].Y += (int)(weight * keyPoints[keyIndex].Y);
}
}
return result;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
for (int i = 0; i < keyPoints.Length; i++)
e.Graphics.FillEllipse(SystemBrushes.Highlight, keyPoints[i].X-3, keyPoints[i].Y-3, 6, 6);
for (int i = 0; i < bezPoints.Length; i++)
e.Graphics.FillEllipse(SystemBrushes.GrayText, bezPoints[i].X - 2, bezPoints[i].Y - 2, 2, 2);
}
}
you can probably copy the GenerateBezierPoints function directly to SGDK2 code.