linepointclosesttopoint.cc

来自「2007年机器人足球世界杯3D仿真组亚军」· CC 代码 · 共 37 行

CC
37
字号
#include "math.hh"namespace bats {  /**   *  Calculates the nearest point on a line to   *  another point.   *   *  @param l0 the start point of the line.   *  @param lVect the not normalized line vector.   *  @param point the other point.   */  Vector3D Math::linePointClosestToPoint(Vector3D const &l0,					 Vector3D const &lVect,					 Vector3D const &point)  {    Vector3D v = lVect.normalize();    Vector3D s = calcPerpend(v);    // There probably is a more efficient formula.    double u = ((s[0]/s[1])*(l0.getY() - point.getY()) + (point.getX() - l0.getX()))/(v[0] - (s[0]/s[1])*v[1]);    // When no perpendicular line is posible within the segment, use    // the closest endpoint.    if (u > lVect.length()) {      if ((l0 - point).length() < ((l0 + lVect) - point).length())	return l0;      else	return l0+lVect;    }    return l0 + v*u;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?