当前位置:   article > 正文

UE4 C++ 记录UE4查找点距离直线最近的点_ue4 点到向量的距离

ue4 点到向量的距离

 1. 计算点距离线段上最近的点坐标

  1. FVector FMath::ClosestPointOnLine(const FVector& LineStart, const FVector& LineEnd, const FVector& Point)
  2. {
  3. // Solve to find alpha along line that is closest point
  4. // Weisstein, Eric W. "Point-Line Distance--3-Dimensional." From MathWorld--A Switchram Web Resource. http://mathworld.wolfram.com/Point-LineDistance3-Dimensional.html
  5. const float A = (LineStart - Point) | (LineEnd - LineStart);
  6. const float B = (LineEnd - LineStart).SizeSquared();
  7. // This should be robust to B == 0 (resulting in NaN) because clamp should return 1.
  8. const float T = FMath::Clamp(-A/B, 0.f, 1.f);
  9. // Generate closest point
  10. FVector ClosestPoint = LineStart + (T * (LineEnd - LineStart));
  11. return ClosestPoint;
  12. }

 2. 计算点距离直线上最近的点坐标

  1. FVector FMath::ClosestPointOnInfiniteLine(const FVector& LineStart, const FVector& LineEnd, const FVector& Point)
  2. {
  3. const float A = (LineStart - Point) | (LineEnd - LineStart);
  4. const float B = (LineEnd - LineStart).SizeSquared();
  5. if (B < SMALL_NUMBER)
  6. {
  7. return LineStart;
  8. }
  9. const float T = -A/B;
  10. // Generate closest point
  11. const FVector ClosestPoint = LineStart + (T * (LineEnd - LineStart));
  12. return ClosestPoint;
  13. }

 

函数头文件  UE_4.27\Engine\Source\Runtime\Core\Public\Math\UnrealMath.h

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/562815
推荐阅读
相关标签
  

闽ICP备14008679号