UE 计算闭合曲线的符号面积
UE 计算闭合曲线的符号面积
计算闭合曲线的符号面积,假设闭合曲线的顶点是位于 XY 平面中的顶点。
推导
参考计算任意多边形的面积
当O点为原点时,根据向量的叉乘计算公式,各个三角形的面积计算如下:
S
Δ
O
A
B
=
1
2
O
A
⃗
×
O
B
⃗
S_{\Delta O A B} =\frac{1}{2} \vec{OA} \times \vec{OB}
SΔOAB=21OA×OB
S
Δ
O
B
C
=
1
2
O
B
⃗
×
O
C
⃗
S_{\Delta O B C} =\frac{1}{2} \vec{OB} \times \vec{OC}
SΔOBC=21OB×OC
S
Δ
O
C
D
=
1
2
O
C
⃗
×
O
D
⃗
S_{\Delta O CD} =\frac{1}{2} \vec{OC} \times \vec{OD }
SΔOCD=21OC×OD
S
Δ
O
D
E
=
1
2
O
D
⃗
×
O
E
⃗
S_{\Delta O DE} =\frac{1}{2} \vec{OD} \times \vec{OE }
SΔODE=21OD×OE
S
Δ
O
E
A
=
1
2
O
E
⃗
×
O
A
⃗
S_{\Delta O EA} =\frac{1}{2} \vec{OE} \times \vec{OA }
SΔOEA=21OE×OA
其中叉乘的计算公式:
O A ⃗ × O B ⃗ = A x ∗ B y − A y ∗ B x \vec{OA} \times \vec{OB } = A_{x} \ast B_{y}-A_{y}*B_{x} OA×OB=Ax∗By−Ay∗Bx
S Δ O A B 、 S Δ O D E 、 S Δ O E A 与 S Δ O B C 、 S Δ O C D 符号相反 S_{\Delta O A B}、 S_{\Delta O DE}、S_{\Delta O EA}与S_{\Delta O B C} 、S_{\Delta O CD}符号相反 SΔOAB、SΔODE、SΔOEA与SΔOBC、SΔOCD符号相反
S 总 = S Δ O A B + S Δ O D E + S Δ O E A + S Δ O B C + S Δ O C D S_{总}= S_{\Delta O A B}+ S_{\Delta O DE}+S_{\Delta O EA}+S_{\Delta O B C} +S_{\Delta O CD} S总=SΔOAB+SΔODE+SΔOEA+SΔOBC+SΔOCD
UE 相关代码
UE::Geometry::CurveUtil::SignedArea2
/**
* Compute the signed area of a closed curve, assuming vertices in the XY plane
*/
template<typename RealType, typename VectorType>
RealType SignedArea2(const TArrayView<const VectorType>& Vertices)
{
RealType Area = 0;
int N = Vertices.Num();
if (N == 0)
{
return 0;
}
for (int Idx = 0, PrevIdx = N-1; Idx < N; PrevIdx=Idx++)
{
const TVector2<RealType>& V1 = Vertices[PrevIdx];
const TVector2<RealType>& V2 = Vertices[Idx];
Area += V1.X * V2.Y - V1.Y * V2.X;
}
return static_cast<RealType>(Area * 0.5);
}