## unity 贝塞尔曲线算法

等同于线性插值

```    // 线性
Vector3 Bezier(Vector3 p0, Vector3 p1, float t)
{
return (1 - t) * p0 + t * p1;
}```

p0p1 = (1 - t) * p0 + t * p1;

p1p2 = (1 - t) * p1 + t * p2;

B(t) = (1 - t) * p0p1 + t * p1p2;

B(t)  = ((1 - t) * ((1 - t) * p0 + t * p1)) + (t * ((1 - t) * p1 + t * p2))

```    // 二阶曲线
Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 p2, float t)
{
Vector3 p0p1 = (1 - t) * p0 + t * p1;
Vector3 p1p2 = (1 - t) * p1 + t * p2;
Vector3 result = (1 - t) * p0p1 + t * p1p2;
return result;
}```

p0p1 = (1 - t) * p0 + t * p1;

p1p2 = (1 - t) * p1 + t * p2;

p2p3 = (1 - t) * p2 + t * p3;

p0p1p2 = (1 - t) * p0p1 + t * p1p2;

p1p2p3 = (1 - t) * p1p2 + t * p2p3;

B(t) = (1 - t) * p0p1p2 + t * p1p2p3;

```    // 三阶曲线
Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
Vector3 result;
Vector3 p0p1 = (1 - t) * p0 + t * p1;
Vector3 p1p2 = (1 - t) * p1 + t * p2;
Vector3 p2p3 = (1 - t) * p2 + t * p3;
Vector3 p0p1p2 = (1 - t) * p0p1 + t * p1p2;
Vector3 p1p2p3 = (1 - t) * p1p2 + t * p2p3;
result = (1 - t) * p0p1p2 + t * p1p2p3;
return result;
}```

n次方公式

```    // n阶曲线，递归实现
public Vector3 Bezier(float t, List<Vector3> p)
{
if (p.Count < 2)
return p[0];
List<Vector3> newp = new List<Vector3>();
for (int i = 0; i < p.Count - 1; i++)
{
Debug.DrawLine(p[i], p[i + 1]);
Vector3 p0p1 = (1 - t) * p[i] + t * p[i + 1];
}
return Bezier(t, newp);
}
// transform转换为vector3，在调用参数为List<Vector3>的Bezier函数
public Vector3 Bezier(float t, List<Transform> p)
{
if (p.Count < 2)
return p[0].position;
List<Vector3> newp = new List<Vector3>();
for (int i = 0; i < p.Count; i++)
{
}
return Bezier(t, newp);
}```