External/OxyPlot/OxyPlot/Foundation/FractionHelper.cs
author StephaneLenclud
Tue, 03 Feb 2015 10:14:18 +0100
branchMiniDisplay
changeset 450 f2d8620e2434
permissions -rw-r--r--
Rebracer update.
     1 // --------------------------------------------------------------------------------------------------------------------
     2 // <copyright file="FractionHelper.cs" company="OxyPlot">
     3 //   The MIT License (MIT)
     4 //
     5 //   Copyright (c) 2012 Oystein Bjorke
     6 //
     7 //   Permission is hereby granted, free of charge, to any person obtaining a
     8 //   copy of this software and associated documentation files (the
     9 //   "Software"), to deal in the Software without restriction, including
    10 //   without limitation the rights to use, copy, modify, merge, publish,
    11 //   distribute, sublicense, and/or sell copies of the Software, and to
    12 //   permit persons to whom the Software is furnished to do so, subject to
    13 //   the following conditions:
    14 //
    15 //   The above copyright notice and this permission notice shall be included
    16 //   in all copies or substantial portions of the Software.
    17 //
    18 //   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    19 //   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    20 //   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    21 //   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    22 //   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    23 //   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    24 //   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    25 // </copyright>
    26 // <summary>
    27 //   Generates fraction strings from double values.
    28 // </summary>
    29 // --------------------------------------------------------------------------------------------------------------------
    30 namespace OxyPlot
    31 {
    32     using System;
    33     using System.Globalization;
    34 
    35     /// <summary>
    36     /// Provides functionality to generate fraction strings from double values.
    37     /// </summary>
    38     /// <remarks>
    39     /// Examples: "3/4", "PI/2"
    40     /// </remarks>
    41     public static class FractionHelper
    42     {
    43         /// <summary>
    44         /// Converts a double to a fraction string.
    45         /// </summary>
    46         /// <param name="value">
    47         /// The value.
    48         /// </param>
    49         /// <param name="unit">
    50         /// The unit.
    51         /// </param>
    52         /// <param name="unitSymbol">
    53         /// The unit symbol.
    54         /// </param>
    55         /// <param name="eps">
    56         /// The tolerance.
    57         /// </param>
    58         /// <param name="formatProvider">
    59         /// The format Provider.
    60         /// </param>
    61         /// <returns>
    62         /// The convert to fraction string.
    63         /// </returns>
    64         public static string ConvertToFractionString(
    65             double value,
    66             double unit = 1,
    67             string unitSymbol = null,
    68             double eps = 1e-6,
    69             IFormatProvider formatProvider = null)
    70         {
    71             if (Math.Abs(value) < eps)
    72             {
    73                 return "0";
    74             }
    75 
    76             // ½, ⅝, ¾
    77             value /= unit;
    78 
    79             // int whole = (int)(value - (int) value);
    80             // int N = 10000;
    81             // int frac = (int) ((value - whole)*N);
    82             // var d = GCF(N,frac);
    83             for (int d = 1; d <= 64; d++)
    84             {
    85                 double n = value * d;
    86                 var ni = (int)Math.Round(n);
    87                 if (Math.Abs(n - ni) < eps)
    88                 {
    89                     string nis = unitSymbol == null || ni != 1 ? ni.ToString(CultureInfo.InvariantCulture) : string.Empty;
    90                     if (d == 1)
    91                     {
    92                         return string.Format("{0}{1}", nis, unitSymbol);
    93                     }
    94 
    95                     return string.Format("{0}{1}/{2}", nis, unitSymbol, d);
    96                 }
    97             }
    98 
    99             return string.Format(formatProvider ?? CultureInfo.CurrentCulture, "{0}{1}", value, unitSymbol);
   100         }
   101 
   102         /// <summary>
   103         /// Finds the greates common divisor.
   104         /// </summary>
   105         /// <param name="a">
   106         /// The a.
   107         /// </param>
   108         /// <param name="b">
   109         /// The b.
   110         /// </param>
   111         /// <returns>
   112         /// The gcd.
   113         /// </returns>
   114         public static int gcd(int a, int b)
   115         {
   116             if (b == 0)
   117             {
   118                 return a;
   119             }
   120 
   121             return gcd(b, a % b);
   122         }
   123 
   124         /// <summary>
   125         /// Finds the greatest common factor.
   126         /// </summary>
   127         /// <param name="x">
   128         /// The x.
   129         /// </param>
   130         /// <param name="y">
   131         /// The y.
   132         /// </param>
   133         /// <returns>
   134         /// The gcf.
   135         /// </returns>
   136         private static int GCF(int x, int y)
   137         {
   138             x = Math.Abs(x);
   139             y = Math.Abs(y);
   140             int z;
   141             do
   142             {
   143                 z = x % y;
   144                 if (z == 0)
   145                 {
   146                     return y;
   147                 }
   148 
   149                 x = y;
   150                 y = z;
   151             }
   152             while (true);
   153         }
   154     }
   155 }