Beispiel #1
0
        /// <summary>
        /// Determine what ratio amounts to a given pixel distance
        /// </summary>
        /// <param name="p_points">
        /// A <see cref="Point"/> array of 4 points specifying the Bezier curve to analyze
        /// </param>
        /// <param name="p_pixels">
        /// A <see cref="System.Int32"/> indicating how many pixels the returned ratio should indicate
        /// </param>
        /// <returns>
        /// A <see cref="System.Double"/> representing the ratio on the curve that amounts to p_pixels distance
        /// </returns>
        static public double CalculateRatioSizeToPixelAccuracy(Point [] p_points, int p_pixels)
        {
            if (p_points.Length != 4)
            {
                throw new Exception("p_points array passed to GetRatioSizeToPixelAccuracy must have exactly 4 items, this one has " + p_points.Length);
            }

            double testRatio = 1.0d;
            PointF prevPoint = BezierSprite.CalculatePoint(p_points, 0.0d);

            // first cut test ratio in half repeatedly until we fall below the requested
            // pixel threshold, then increment the ratio back up by .01 until we hit the
            // threshold again.
            while (true)
            {
                testRatio /= 2.0d;
                PointF nextPoint = BezierSprite.CalculatePoint(p_points, testRatio);

                if (BezierSprite.Hypot(prevPoint, nextPoint) < p_pixels)
                {
                    while (true)
                    {
                        testRatio += 0.01d;
                        PointF nextPoint2 = BezierSprite.CalculatePoint(p_points, testRatio);
                        if (BezierSprite.Hypot(prevPoint, nextPoint2) > p_pixels)
                        {
                            break;
                        }
                    }
                    break;
                }
            }

            return(testRatio);
        }