# 计算机与数学 —— 使用高斯勒让得求积来获得样条的长度

## 高斯勒让得求积

11f(x)dx=11W(x)g(x)dxi=1nwig(xi)

wi=2(1x2i)[Pn(xi)2] $w_i = \frac{2}{(1-x_i^2)[P_n'(x_i)^2]}$，其中 xi $x_i$ Pn(x) $P_n(x)$的第 i $i$個根。

Pn(x)$P_n(x)$函数如下：

Pn(x)=1in(xxi)

Pi $P_i$ wi $w_i$
0 $0$ 128255$\tfrac{128}{255}$
±245147021 $\pm\tfrac{\sqrt{245-14\sqrt{70}}}{21}$ 322+1370900 $\tfrac{322+13\sqrt{70}}{900}$
±245+147021 $\pm\tfrac{\sqrt{245+14\sqrt{70}}}{21}$ 3221370900 $\tfrac{322-13\sqrt{70}}{900}$

    struct FLegendreGaussCoefficient
{
float Abscissa;
float Weight;
};

static const FLegendreGaussCoefficient LegendreGaussCoefficients[] =
{
{ 0.0f, 0.5688889f },
{ -0.5384693f, 0.47862867f },
{ 0.5384693f, 0.47862867f },
{ -0.90617985f, 0.23692688f },
{ 0.90617985f, 0.23692688f }
};

## 代码实现

float LengthOfSegment(FVector P0, FVector P1, FVector T0, FVector T1)
{

FVector Coeff1 = ((P0 - P1) * 2.0f + T0 + T1) * 3.0f;
FVector Coeff2 = (P1 - P0) * 6.0f - T0 * 4.0f - T1 * 2.0f;
FVector Coeff3 = T0;
float HalfParam = 0.5f;
float Length = 0.0f;

for (const auto& LegendreGaussCoefficient : LegendreGaussCoefficients)
{
// Calculate derivative at each Legendre-Gauss sample, and perform a weighted sum
const float Alpha = HalfParam * (1.0f + LegendreGaussCoefficient.Abscissa);
const FVector Derivative = ((Coeff1 * Alpha + Coeff2) * Alpha + Coeff3);
Length += Derivative.Size() * LegendreGaussCoefficient.Weight;
}
Length *= HalfParam;

return Length;
}

<全文完>

12-13
03-08 2万+
09-08 3930
12-15 5110
04-17 7908
07-06 3万+
08-08 5601
03-29 2477
01-09 76
03-03 1110
10-25 3898
06-08 33
03-28 2216
09-03 417