Beispiel #1
0
        /// <summary>
        /// Gets the conversion multiplier applied to the current value needed to associate the value with a different unit
        /// </summary>
        /// <param name="currentUnit">The current units</param>
        /// <param name="newUnit">The units to convert to</param>
        /// <returns>A multiplier as a double. current * multiplier = new</returns>
        public static double GetUnitConversion(IGeneralUnit currentUnit, IGeneralUnit newUnit)
        {
            double conversion = 1;

            CompoundUnit currentCompound = new CompoundUnit(currentUnit);
            CompoundUnit newCompound     = new CompoundUnit(newUnit);

            List <UnitPowerPair> currentValues = currentCompound.Values.OrderBy(new Func <UnitPowerPair, Quantities>((UnitPowerPair pair) => pair.Unit.GetQuantity())).ToList();
            List <UnitPowerPair> newValues     = newCompound.Values.OrderBy(new Func <UnitPowerPair, Quantities>((UnitPowerPair pair) => pair.Unit.GetQuantity())).ToList();

            if (currentValues.Count == newValues.Count)
            {
                for (int i = 0; i < currentValues.Count; i++)
                {
                    if (currentValues[i].Power == newValues[i].Power)
                    {
                        conversion *= Math.Pow(GetUnitConversion(currentValues[i].Unit, newValues[i].Unit), currentValues[i].Power);
                    }
                    else
                    {
                        // The units have different powers and therfore aren't convertable
                        throw new ArgumentException("Unit conversion failed - the units provided had different powers so they don't represent the same physical quantity and therfore can't be converted.");
                    }
                }
            }
            else
            {
                // Pre convert all units to their system's base unit
                for (int i = 0; i < currentValues.Count; i++)
                {
                    Units preConvertedUnit = currentValues[i].Unit.GetQuantity().GetSystemBaseUnit(currentValues[i].Unit.GetSystem());
                    conversion *= Math.Pow(GetUnitConversion(currentValues[i].Unit, preConvertedUnit), currentValues[i].Power);

                    currentValues[i] = new UnitPowerPair {
                        Unit = preConvertedUnit, Power = currentValues[i].Power
                    };
                }

                for (int i = 0; i < newValues.Count; i++)
                {
                    Units preConvertedUnit = newValues[i].Unit.GetQuantity().GetSystemBaseUnit(newValues[i].Unit.GetSystem());
                    conversion *= Math.Pow(GetUnitConversion(newValues[i].Unit, preConvertedUnit), newValues[i].Power);

                    newValues[i] = new UnitPowerPair {
                        Unit = preConvertedUnit, Power = newValues[i].Power
                    };
                }

                conversion *= GetUnitConversion(new CompoundUnit(currentValues.ToArray()), new CompoundUnit(newValues.ToArray()));
            }

            return(conversion);
        }
Beispiel #2
0
 public Polar(IGeneralUnit xUnit, IGeneralUnit yUnit)
 {
     Axes = new Dictionary <string, IGeneralUnit> {
         { "r", xUnit }, { "theta", yUnit }
     };
 }
Beispiel #3
0
 public Cartesian_2D(IGeneralUnit xUnit, IGeneralUnit yUnit)
 {
     Axes = new Dictionary <string, IGeneralUnit> {
         { "x", xUnit }, { "y", yUnit }
     };
 }
Beispiel #4
0
 public Line(IGeneralUnit xUnit)
 {
     Axes = new Dictionary <string, IGeneralUnit> {
         { "x", xUnit }
     };
 }
Beispiel #5
0
 public SphericalPolar(IGeneralUnit xUnit, IGeneralUnit yUnit, IGeneralUnit zUnit)
 {
     Axes = new Dictionary <string, IGeneralUnit> {
         { "r", xUnit }, { "theta", yUnit }, { "phi", zUnit }
     };
 }
Beispiel #6
0
 /// <summary>
 /// Converts the value to one with an equivilant unit
 /// </summary>
 /// <param name="unit">The new value's units</param>
 /// <returns>The corisponding IValue object</returns>
 public IValue As(IGeneralUnit unit)
 {
     return(new Value(GetMagnitude() / UnitsMethods.GetUnitConversion(Unit, unit), unit));
 }
Beispiel #7
0
        /// <summary>
        /// Creates a new Value object
        /// </summary>
        /// <param name="size">The size of the value</param>
        /// <param name="unit">The unit</param>
        public Value(double size, IGeneralUnit unit)
        {
            Unit = unit;

            Magnitude = size;
        }
Beispiel #8
0
        /// <summary>
        /// Creates a new value, altering the provided value and power (if nessessary) to express the value in standard form
        /// </summary>
        /// <param name="size">The size of the value</param>
        /// <param name="standardPower">The standard form power (for the provided value).</param>
        /// <param name="unit">The unit</param>
        public StandardValue(double size, IGeneralUnit unit, int standardPower = 0) : base(size, unit)
        {
            StandardPower = standardPower;

            NormaliseValue();
        }