/// <summary> /// returns a parameter t such that the distance between curve[t] and targetPoint is minimal /// and t belongs to the closed segment [low,high] /// </summary> /// <param name="targetPoint">the point to find the closest point</param> /// <param name="high">the upper bound of the parameter</param> /// <param name="low">the low bound of the parameter</param> /// <returns></returns> public double ClosestParameterWithinBounds(Point targetPoint, double low, double high) { const int numberOfTestPoints = 8; double t = (high - low) / (numberOfTestPoints + 1); double closest = low; double minDist = Double.MaxValue; for (int i = 0; i <= numberOfTestPoints; i++) { double par = low + i * t; Point p = targetPoint - this[par]; double d = p * p; if (d < minDist) { minDist = d; closest = par; } } if (closest == 0 && high == Math.PI * 2) { low = -Math.PI; } double ret = ClosestPointOnCurve.ClosestPoint(this, targetPoint, closest, low, high); if (ret < 0) { ret += 2 * Math.PI; } return(ret); }
/// <summary> /// returns a parameter t such that the distance between curve[t] and a is minimal /// </summary> /// <param name="targetPoint"></param> /// <returns></returns> public double ClosestParameter(Point targetPoint) { double t = 1.0 / 8; double closest = 0; double minDist = Double.MaxValue; for (int i = 0; i < 9; i++) { Point p = targetPoint - this[i * t]; double d = p * p; if (d < minDist) { minDist = d; closest = i * t; } } return(ClosestPointOnCurve.ClosestPoint(this, targetPoint, closest, 0, 1)); }
/// <summary> /// returns a parameter t such that the distance between curve[t] and targetPoint is minimal /// and t belongs to the closed segment [low,high] /// </summary> /// <param name="targetPoint">the point to find the closest point</param> /// <param name="high">the upper bound of the parameter</param> /// <param name="low">the low bound of the parameter</param> /// <returns></returns> public double ClosestParameterWithinBounds(Point targetPoint, double low, double high) { System.Diagnostics.Debug.Assert(high <= 1 && low >= 0); System.Diagnostics.Debug.Assert(low <= high); double t = (high - low) / 8; double closest = 0; double minDist = Double.MaxValue; for (int i = 0; i < 9; i++) { Point p = targetPoint - this[i * t + low]; double d = p * p; if (d < minDist) { minDist = d; closest = i * t + low; } } return(ClosestPointOnCurve.ClosestPoint(this, targetPoint, closest, low, high)); }
/// <summary> /// returns a parameter t such that the distance between curve[t] and a is minimal /// </summary> /// <param name="targetPoint"></param> /// <returns>the parameter of the closest point</returns> public double ClosestParameter(Point targetPoint) { double savedParStart = 0; const int numberOfTestPoints = 8; double t = (ParEnd - ParStart) / (numberOfTestPoints + 1); double closest = ParStart; double minDist = Double.MaxValue; for (int i = 0; i <= numberOfTestPoints; i++) { double par = ParStart + i * t; Point p = targetPoint - this[par]; double d = p * p; if (d < minDist) { minDist = d; closest = par; } } bool parStartWasChanged = false; if (closest == 0 && ParEnd == Math.PI * 2) { parStartWasChanged = true; savedParStart = ParStart; ParStart = -Math.PI; } double ret = ClosestPointOnCurve.ClosestPoint(this, targetPoint, closest, ParStart, ParEnd); if (ret < 0) { ret += 2 * Math.PI; } if (parStartWasChanged) { ParStart = savedParStart; } return(ret); }