C# – Convert Length/Area to Points With Ease

C# – Convert Length/Area to Points With Ease

C_Sharp_Code

I’ve been doing quite a bit with Adobe IDML files and SVG recently, both formats work natively in Points. So I created an extension method for .NET 4 that converts a number into the equivalent value in Points.

At the moment it supports converting a double input value in millimeters (“mm”), centimeters (“cm”), meters (“m”), inches (“in”), feet (“ft”) and picas (“pc”).

You can convert a length or an area, the default values are input unit is mm, convert a length and return the answer to 2 decimal places. Usage is simple;

Using default values.

double number = 100;
double numberInPoints = number.ToPoints();

Convert an Inch value to 3 decimal places.

double number = 100;
double numberInPoints = number.ToPoints("in",3);

Convert an area in Centimeters to 4 decimal places.

double number = 100;
double numberInPoints = number.ToPoints("cm",4,true);

And the Code is:

using System;

namespace Odd.Maths.Convert
{
    public static class ConversionFactors
    {
        public const double mmToPoint = 2.834645669291;
        public const double cmToPoint = mmToPoint*10;
        public const double mToPoint = mmToPoint*1000;
        public const double inToPoint = mmToPoint * 25.4;
        public const double ftToPoint = (mmToPoint * 25.4) * 12;
        public const double picaToPoint = 72;
    }

    public static class ConversionExtensionMethods
    {
        public static double ToPoints(this double d, string fromUnit = "mm", int precision = 2, bool isArea = false)
        {
            switch (fromUnit)
            {
                case "mm":
                    return (isArea == true)
                        ? Math.Round(d * Math.Pow(ConversionFactors.mmToPoint, 2), precision)
                        : Math.Round(d * ConversionFactors.mmToPoint, precision);
                case "cm":
                    return (isArea == true)
                        ? Math.Round(d * Math.Pow(ConversionFactors.cmToPoint, 2), precision)
                        : Math.Round(d * ConversionFactors.cmToPoint, precision);
                case "m":
                    return (isArea == true)
                        ? Math.Round(d * Math.Pow(ConversionFactors.mToPoint, 2), precision)
                        : Math.Round(d * ConversionFactors.mToPoint, precision);
                case "in":
                    return (isArea == true)
                        ? Math.Round(d * Math.Pow(ConversionFactors.inToPoint, 2), precision)
                        : Math.Round(d * ConversionFactors.inToPoint, precision);
                case "ft":
                    return (isArea == true)
                        ? Math.Round(d * Math.Pow(ConversionFactors.ftToPoint, 2), precision)
                        : Math.Round(d * ConversionFactors.ftToPoint, precision);
                case "pc":
                    return (isArea == true)
                        ? Math.Round(d * Math.Pow(ConversionFactors.picaToPoint, 2), precision)
                        : Math.Round(d * ConversionFactors.picaToPoint, precision);
                default:
                    //Returns 0 if an incorrect fromUnit unit is specified
                    return 0;
            }

        }

    }
}

It’s all quite basic with minimal (read none!) error checking but hopefully it might help someone out :)

About the Author

Simon Owen

Simon qualified as an mechanical engineer, moved in to technical publications for a while, and then on in to the design and print industry. After a stint managing design studios and busy print shops he's now the proud owner of a start-up.

0

 

No comments yet

What do you think