Exemplo n.º 1
0
        private Bitmap FractionToBitmap(Fraction f)
        {
            //Measuring numbers to determine bitmap width
            var num = f.Numerator;
            var den = f.Denominator;
            var font = new Font("Verdana", 170, GraphicsUnit.Pixel);
            int bmpwidth;
            SizeF numsize, densize;

            using (var g = this.CreateGraphics())
            {
                numsize = g.MeasureString(num.ToString(), font);
                densize = g.MeasureString(den.ToString(), font);
                bmpwidth = (int)Math.Max(numsize.Width, densize.Width) + 10;
            }

            var bmp = new Bitmap(bmpwidth, 480);

            //Draw fraction
            using (var g = Graphics.FromImage(bmp))
            {
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;

                //Numerator
                var numcenter = new Point(bmp.Width / 2, bmp.Height * 1 / 4);
                g.DrawString(num.ToString(), font, Brushes.Black , new PointF(numcenter.X - numsize.Width / 2, numcenter.Y - numsize.Height / 2));

                //Denominator
                var dencenter = new Point(bmp.Width / 2, bmp.Height * 3 / 4);
                g.DrawString(den.ToString(), font, Brushes.Black, new PointF(dencenter.X - densize.Width / 2, dencenter.Y - densize.Height / 2));

                //The fraction line
                var pen = new Pen(Brushes.Black, 10) {StartCap = LineCap.Round, EndCap = LineCap.Round};
                g.DrawLine(pen, 10, bmp.Height / 2, bmp.Width - 10, bmp.Height / 2);
            }

            return bmp;
        }
Exemplo n.º 2
0
 private static Fraction Negate(Fraction frac1)
 {
     var num = -frac1.Numerator;
     var den = frac1.Denominator;
     return new Fraction(num, den);
 }
Exemplo n.º 3
0
 private static Fraction Multiply(Fraction frac1, Fraction frac2)
 {
     try
     {
         checked
         {
             var num = frac1.Numerator * frac2.Numerator;
             var den = frac1.Denominator * frac2.Denominator;
             return (new Fraction(num, den));
         }
     }
     catch (OverflowException)
     {
         throw new FractionException("Overflow occurred while performing arithemetic operation");
     }
     catch (Exception)
     {
         throw new FractionException("An error occurred while performing arithemetic operation");
     }
 }
Exemplo n.º 4
0
        public static void ReduceFraction(Fraction frac)
        {
            if (frac.Numerator == 0)
                {
                    frac.Denominator = 1;
                    return;
                }

                if (frac.Denominator < 0)	// if -ve sign in denominator
                {
                    //pass -ve sign to numerator
                    frac.Numerator *= -1;
                    frac.Denominator *= -1;
                }

                var gcd = GCD(frac.Numerator, frac.Denominator);
                frac.Numerator /= gcd;
                frac.Denominator /= gcd;
        }
Exemplo n.º 5
0
        public static Fraction Inverse(Fraction frac1)
        {
            if (frac1.Numerator == 0)
                throw new FractionException("Operation not possible (Denominator cannot be assigned a ZERO Value)");

            var num = frac1.Denominator;
            var den = frac1.Numerator;
            return (new Fraction(num, den));
        }
Exemplo n.º 6
0
 public static Fraction FareyAdd(Fraction frac1, Fraction frac2)
 {
     var num = frac1.Numerator + frac2.Numerator;
     var den = frac1.Denominator + frac2.Denominator;
     return (new Fraction(num, den));
 }