/// <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)
 {
     return(curve.ClosestParameter(targetPoint));
 }
        public void ClosestParameterWithinBounds() {
#if GDI_DEBUG_VIEWER
            if (!MsaglTestBase.DontShowTheDebugViewer()) {
                DisplayGeometryGraph.SetShowFunctions();
            }
#endif
            var ellipse = CurveFactory.CreateEllipse(8, 10, new Point());
            var point = new Point(20, 1);
            var t = ellipse.ClosestParameter(point);
            var low = t - 1;
            var high = t + 1;
            var t1 = ellipse.ClosestParameterWithinBounds(point, low, high);
#if GDI_DEBUG_VIEWER
            if (!MsaglTestBase.DontShowTheDebugViewer()) {
                LayoutAlgorithmSettings.ShowDebugCurves(new DebugCurve(100, 0.1, "black", ellipse),
                        new DebugCurve(100, 0.01, "brown", new LineSegment(ellipse[t], point)),
                        new DebugCurve(100, 0.01, "green", new LineSegment(ellipse[t1], point)));
            }
#endif
            var dist = point - ellipse[t];
            var dist1 = point - ellipse[t1];
            Assert.IsTrue(ApproximateComparer.Close(dist.Length, dist1.Length) && ApproximateComparer.Close(t, t1));
#if GDI_DEBUG_VIEWER
            if (!MsaglTestBase.DontShowTheDebugViewer()) {
                LayoutAlgorithmSettings.ShowDebugCurves(new DebugCurve(ellipse), new DebugCurve("red", new LineSegment(point, ellipse[t])));
            }
#endif

            var curve = new Curve();
            curve.AddSegment(new LineSegment(new Point(-10, -10), new Point(10, -10)));
            curve.AddSegment(new Ellipse(0, Math.PI / 2, new Point(0, -10), new Point(10, 0), new Point(10, 0)));
            Curve.ContinueWithLineSegment(curve, new Point(20, 10));
            curve.AddSegment(new CubicBezierSegment(curve.End, new Point(20, 20), new Point(15, 25), new Point(10, 25)));
            Curve.ContinueWithLineSegment(curve, new Point(-10, 25));
            Point p = new Point(11, 0);
            t = curve.ClosestParameter(p);

#if GDI_DEBUG_VIEWER
            if (!MsaglTestBase.DontShowTheDebugViewer()) {
                LayoutAlgorithmSettings.ShowDebugCurves(new DebugCurve(curve), new DebugCurve("red", new LineSegment(p, curve[t])));
            }
#endif
            t1 = curve.ClosestParameterWithinBounds(p, 1 + Math.PI / 4, 2);
#if GDI_DEBUG_VIEWER
            if (!MsaglTestBase.DontShowTheDebugViewer()) {
                LayoutAlgorithmSettings.ShowDebugCurves(new DebugCurve(curve), new DebugCurve("red", new LineSegment(p, curve[t1])));
            }
#endif
            Assert.IsTrue(t1 < t);
            p = new Point(30, 30);
            t = curve.ClosestParameter(p);
            t1 = curve.ClosestParameterWithinBounds(p, t - 0.5, t + 0.5);
            Assert.IsTrue(t > 1 + Math.PI / 2 + 1 && t < 1 + Math.PI / 2 + 2);
            Assert.IsTrue(ApproximateComparer.Close(t, t1));

            var poly = new Polyline(new Point(0, 0), new Point(10, 0), new Point(20, 10));
            p = new Point(9, 9);
            const double l = 0.7;
            const double h = 1.3;
            t = poly.ClosestParameterWithinBounds(p, l, h);
#if GDI_DEBUG_VIEWER
            if (!MsaglTestBase.DontShowTheDebugViewer()) {
                LayoutAlgorithmSettings.ShowDebugCurves(new DebugCurve(poly), new DebugCurve("red", new LineSegment(p, poly[t])));
            }
#endif
            var d = (p - poly[t]).Length;

            Assert.IsTrue(d <= (p - poly[l]).Length + ApproximateComparer.Tolerance && d < (p - poly[h]).Length + ApproximateComparer.Tolerance && d < (p - poly[(l + h) / 2]).Length + ApproximateComparer.Tolerance);
        }