line3d::getClosestPoint can now chose between using line or line segment

Also the case of start/end points being identical now avoids invalid numbers from division by 0 and just returns the start point
Basically it's now the same again as line2d::getClosestPoint (which also got a comment fix)

git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@6593 dfc29bdd-3216-0410-991c-e03cc46cb475
This commit is contained in:
cutealien
2024-01-30 14:51:08 +00:00
parent c7dddff71c
commit ffec673146
3 changed files with 14 additions and 6 deletions

View File

@ -297,7 +297,7 @@ class line2d
//! Get the closest point on this line to a point
/** \param point: Starting search at this point
\param checkOnlySegments: Default (true) is to return a point on the line-segment (between begin and end) of the line.
When set to false the function will check for the first the closest point on the the line even when outside the segment. */
When set to false the function will check for the closest point on the the line even when outside the segment. */
vector2d<T> getClosestPoint(const vector2d<T>& point, bool checkOnlySegments=true) const
{
vector2d<f64> c((f64)(point.X-start.X), (f64)(point.Y- start.Y));

View File

@ -85,19 +85,26 @@ class line3d
//! Get the closest point on this line to a point
/** \param point The point to compare to.
\param checkOnlySegments: Default (true) is to return a point on the line-segment (between begin and end) of the line.
When set to false the function will check for the closest point on the the line even when outside the segment.
\return The nearest point which is part of the line. */
vector3d<T> getClosestPoint(const vector3d<T>& point) const
vector3d<T> getClosestPoint(const vector3d<T>& point, bool checkOnlySegments=true) const
{
vector3d<T> c = point - start;
vector3d<T> v = end - start;
T d = (T)v.getLength();
if ( d == 0 ) // line is just a single point
return start;
v /= d;
T t = v.dotProduct(c);
if (t < (T)0.0)
return start;
if (t > d)
return end;
if ( checkOnlySegments )
{
if (t < (T)0.0)
return start;
if (t > d)
return end;
}
v *= t;
return start + v;