External/OxyPlot/OxyPlot/Foundation/FractionHelper.cs
changeset 391 5be8f2773237
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/OxyPlot/OxyPlot/Foundation/FractionHelper.cs	Sat Jun 08 16:53:22 2013 +0000
     1.3 @@ -0,0 +1,155 @@
     1.4 +// --------------------------------------------------------------------------------------------------------------------
     1.5 +// <copyright file="FractionHelper.cs" company="OxyPlot">
     1.6 +//   The MIT License (MIT)
     1.7 +//
     1.8 +//   Copyright (c) 2012 Oystein Bjorke
     1.9 +//
    1.10 +//   Permission is hereby granted, free of charge, to any person obtaining a
    1.11 +//   copy of this software and associated documentation files (the
    1.12 +//   "Software"), to deal in the Software without restriction, including
    1.13 +//   without limitation the rights to use, copy, modify, merge, publish,
    1.14 +//   distribute, sublicense, and/or sell copies of the Software, and to
    1.15 +//   permit persons to whom the Software is furnished to do so, subject to
    1.16 +//   the following conditions:
    1.17 +//
    1.18 +//   The above copyright notice and this permission notice shall be included
    1.19 +//   in all copies or substantial portions of the Software.
    1.20 +//
    1.21 +//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    1.22 +//   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    1.23 +//   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    1.24 +//   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    1.25 +//   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    1.26 +//   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    1.27 +//   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    1.28 +// </copyright>
    1.29 +// <summary>
    1.30 +//   Generates fraction strings from double values.
    1.31 +// </summary>
    1.32 +// --------------------------------------------------------------------------------------------------------------------
    1.33 +namespace OxyPlot
    1.34 +{
    1.35 +    using System;
    1.36 +    using System.Globalization;
    1.37 +
    1.38 +    /// <summary>
    1.39 +    /// Provides functionality to generate fraction strings from double values.
    1.40 +    /// </summary>
    1.41 +    /// <remarks>
    1.42 +    /// Examples: "3/4", "PI/2"
    1.43 +    /// </remarks>
    1.44 +    public static class FractionHelper
    1.45 +    {
    1.46 +        /// <summary>
    1.47 +        /// Converts a double to a fraction string.
    1.48 +        /// </summary>
    1.49 +        /// <param name="value">
    1.50 +        /// The value.
    1.51 +        /// </param>
    1.52 +        /// <param name="unit">
    1.53 +        /// The unit.
    1.54 +        /// </param>
    1.55 +        /// <param name="unitSymbol">
    1.56 +        /// The unit symbol.
    1.57 +        /// </param>
    1.58 +        /// <param name="eps">
    1.59 +        /// The tolerance.
    1.60 +        /// </param>
    1.61 +        /// <param name="formatProvider">
    1.62 +        /// The format Provider.
    1.63 +        /// </param>
    1.64 +        /// <returns>
    1.65 +        /// The convert to fraction string.
    1.66 +        /// </returns>
    1.67 +        public static string ConvertToFractionString(
    1.68 +            double value,
    1.69 +            double unit = 1,
    1.70 +            string unitSymbol = null,
    1.71 +            double eps = 1e-6,
    1.72 +            IFormatProvider formatProvider = null)
    1.73 +        {
    1.74 +            if (Math.Abs(value) < eps)
    1.75 +            {
    1.76 +                return "0";
    1.77 +            }
    1.78 +
    1.79 +            // ½, ⅝, ¾
    1.80 +            value /= unit;
    1.81 +
    1.82 +            // int whole = (int)(value - (int) value);
    1.83 +            // int N = 10000;
    1.84 +            // int frac = (int) ((value - whole)*N);
    1.85 +            // var d = GCF(N,frac);
    1.86 +            for (int d = 1; d <= 64; d++)
    1.87 +            {
    1.88 +                double n = value * d;
    1.89 +                var ni = (int)Math.Round(n);
    1.90 +                if (Math.Abs(n - ni) < eps)
    1.91 +                {
    1.92 +                    string nis = unitSymbol == null || ni != 1 ? ni.ToString(CultureInfo.InvariantCulture) : string.Empty;
    1.93 +                    if (d == 1)
    1.94 +                    {
    1.95 +                        return string.Format("{0}{1}", nis, unitSymbol);
    1.96 +                    }
    1.97 +
    1.98 +                    return string.Format("{0}{1}/{2}", nis, unitSymbol, d);
    1.99 +                }
   1.100 +            }
   1.101 +
   1.102 +            return string.Format(formatProvider ?? CultureInfo.CurrentCulture, "{0}{1}", value, unitSymbol);
   1.103 +        }
   1.104 +
   1.105 +        /// <summary>
   1.106 +        /// Finds the greates common divisor.
   1.107 +        /// </summary>
   1.108 +        /// <param name="a">
   1.109 +        /// The a.
   1.110 +        /// </param>
   1.111 +        /// <param name="b">
   1.112 +        /// The b.
   1.113 +        /// </param>
   1.114 +        /// <returns>
   1.115 +        /// The gcd.
   1.116 +        /// </returns>
   1.117 +        public static int gcd(int a, int b)
   1.118 +        {
   1.119 +            if (b == 0)
   1.120 +            {
   1.121 +                return a;
   1.122 +            }
   1.123 +
   1.124 +            return gcd(b, a % b);
   1.125 +        }
   1.126 +
   1.127 +        /// <summary>
   1.128 +        /// Finds the greatest common factor.
   1.129 +        /// </summary>
   1.130 +        /// <param name="x">
   1.131 +        /// The x.
   1.132 +        /// </param>
   1.133 +        /// <param name="y">
   1.134 +        /// The y.
   1.135 +        /// </param>
   1.136 +        /// <returns>
   1.137 +        /// The gcf.
   1.138 +        /// </returns>
   1.139 +        private static int GCF(int x, int y)
   1.140 +        {
   1.141 +            x = Math.Abs(x);
   1.142 +            y = Math.Abs(y);
   1.143 +            int z;
   1.144 +            do
   1.145 +            {
   1.146 +                z = x % y;
   1.147 +                if (z == 0)
   1.148 +                {
   1.149 +                    return y;
   1.150 +                }
   1.151 +
   1.152 +                x = y;
   1.153 +                y = z;
   1.154 +            }
   1.155 +            while (true);
   1.156 +        }
   1.157 +    }
   1.158 +}
   1.159 \ No newline at end of file