?1. 计算点距离线段上最近的点坐标
FVector FMath::ClosestPointOnLine(const FVector& LineStart, const FVector& LineEnd, const FVector& Point)
{
// Solve to find alpha along line that is closest point
// Weisstein, Eric W. "Point-Line Distance--3-Dimensional." From MathWorld--A Switchram Web Resource. http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
const float A = (LineStart - Point) | (LineEnd - LineStart);
const float B = (LineEnd - LineStart).SizeSquared();
// This should be robust to B == 0 (resulting in NaN) because clamp should return 1.
const float T = FMath::Clamp(-A/B, 0.f, 1.f);
// Generate closest point
FVector ClosestPoint = LineStart + (T * (LineEnd - LineStart));
return ClosestPoint;
}
?2. 计算点距离直线上最近的点坐标
FVector FMath::ClosestPointOnInfiniteLine(const FVector& LineStart, const FVector& LineEnd, const FVector& Point)
{
const float A = (LineStart - Point) | (LineEnd - LineStart);
const float B = (LineEnd - LineStart).SizeSquared();
if (B < SMALL_NUMBER)
{
return LineStart;
}
const float T = -A/B;
// Generate closest point
const FVector ClosestPoint = LineStart + (T * (LineEnd - LineStart));
return ClosestPoint;
}
?
函数头文件? UE_4.27\Engine\Source\Runtime\Core\Public\Math\UnrealMath.h
|