📄 linepointclosesttopoint.cc
字号:
#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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -