|
| 1 | +// Copyright © 2007 by Initial Force AS. All rights reserved. |
| 2 | +// https://github.com/InitialForce/SIUnits |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | + |
| 22 | +using System; |
| 23 | + |
| 24 | +// ReSharper disable once CheckNamespace |
| 25 | +namespace UnitsNet |
| 26 | +{ |
| 27 | + /// <summary> |
| 28 | + /// In everyday use and in kinematics, the speed of an object is the magnitude of its velocity (the rate of change of its position); it is thus a scalar quantity.[1] The average speed of an object in an interval of time is the distance travelled by the object divided by the duration of the interval;[2] the instantaneous speed is the limit of the average speed as the duration of the time interval approaches zero. |
| 29 | + /// </summary> |
| 30 | + public partial struct Speed : IComparable, IComparable<Speed> |
| 31 | + { |
| 32 | + /// <summary> |
| 33 | + /// Base unit of Speed. |
| 34 | + /// </summary> |
| 35 | + public readonly double MetersPerSecond; |
| 36 | + |
| 37 | + public Speed(double meterspersecond) : this() |
| 38 | + { |
| 39 | + MetersPerSecond = meterspersecond; |
| 40 | + } |
| 41 | + |
| 42 | + #region Unit Properties |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Get Speed in FeetPerSecond. |
| 46 | + /// </summary> |
| 47 | + /// <remarks>Example: x = (y - b) / a where x is value in FeetPerSecond and y is value in base unit MetersPerSecond.</remarks> |
| 48 | + public double FeetPerSecond |
| 49 | + { |
| 50 | + get { return (MetersPerSecond - (0)) / 0.3048; } |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Get Speed in KilometersPerHour. |
| 55 | + /// </summary> |
| 56 | + /// <remarks>Example: x = (y - b) / a where x is value in KilometersPerHour and y is value in base unit MetersPerSecond.</remarks> |
| 57 | + public double KilometersPerHour |
| 58 | + { |
| 59 | + get { return (MetersPerSecond - (0)) / 0.277777777777778; } |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Get Speed in Knots. |
| 64 | + /// </summary> |
| 65 | + /// <remarks>Example: x = (y - b) / a where x is value in Knots and y is value in base unit MetersPerSecond.</remarks> |
| 66 | + public double Knots |
| 67 | + { |
| 68 | + get { return (MetersPerSecond - (0)) / 0.514444; } |
| 69 | + } |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Get Speed in MilesPerHour. |
| 73 | + /// </summary> |
| 74 | + /// <remarks>Example: x = (y - b) / a where x is value in MilesPerHour and y is value in base unit MetersPerSecond.</remarks> |
| 75 | + public double MilesPerHour |
| 76 | + { |
| 77 | + get { return (MetersPerSecond - (0)) / 0.44704; } |
| 78 | + } |
| 79 | + |
| 80 | + #endregion |
| 81 | + |
| 82 | + #region Static |
| 83 | + |
| 84 | + public static Speed Zero |
| 85 | + { |
| 86 | + get { return new Speed(); } |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Get Speed from FeetPerSecond. |
| 91 | + /// </summary> |
| 92 | + /// <remarks>Example: y = ax + b where x is value in FeetPerSecond and y is value in base unit MetersPerSecond.</remarks> |
| 93 | + public static Speed FromFeetPerSecond(double feetpersecond) |
| 94 | + { |
| 95 | + return new Speed(0.3048 * feetpersecond + 0); |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Get Speed from KilometersPerHour. |
| 100 | + /// </summary> |
| 101 | + /// <remarks>Example: y = ax + b where x is value in KilometersPerHour and y is value in base unit MetersPerSecond.</remarks> |
| 102 | + public static Speed FromKilometersPerHour(double kilometersperhour) |
| 103 | + { |
| 104 | + return new Speed(0.277777777777778 * kilometersperhour + 0); |
| 105 | + } |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Get Speed from Knots. |
| 109 | + /// </summary> |
| 110 | + /// <remarks>Example: y = ax + b where x is value in Knots and y is value in base unit MetersPerSecond.</remarks> |
| 111 | + public static Speed FromKnots(double knots) |
| 112 | + { |
| 113 | + return new Speed(0.514444 * knots + 0); |
| 114 | + } |
| 115 | + |
| 116 | + /// <summary> |
| 117 | + /// Get Speed from MetersPerSecond. |
| 118 | + /// </summary> |
| 119 | + /// <remarks>Example: y = ax + b where x is value in MetersPerSecond and y is value in base unit MetersPerSecond.</remarks> |
| 120 | + public static Speed FromMetersPerSecond(double meterspersecond) |
| 121 | + { |
| 122 | + return new Speed(1 * meterspersecond + 0); |
| 123 | + } |
| 124 | + |
| 125 | + /// <summary> |
| 126 | + /// Get Speed from MilesPerHour. |
| 127 | + /// </summary> |
| 128 | + /// <remarks>Example: y = ax + b where x is value in MilesPerHour and y is value in base unit MetersPerSecond.</remarks> |
| 129 | + public static Speed FromMilesPerHour(double milesperhour) |
| 130 | + { |
| 131 | + return new Speed(0.44704 * milesperhour + 0); |
| 132 | + } |
| 133 | + |
| 134 | + #endregion |
| 135 | + |
| 136 | + #region Arithmetic Operators |
| 137 | + |
| 138 | + public static Speed operator -(Speed right) |
| 139 | + { |
| 140 | + return new Speed(-right.MetersPerSecond); |
| 141 | + } |
| 142 | + |
| 143 | + public static Speed operator +(Speed left, Speed right) |
| 144 | + { |
| 145 | + return new Speed(left.MetersPerSecond + right.MetersPerSecond); |
| 146 | + } |
| 147 | + |
| 148 | + public static Speed operator -(Speed left, Speed right) |
| 149 | + { |
| 150 | + return new Speed(left.MetersPerSecond - right.MetersPerSecond); |
| 151 | + } |
| 152 | + |
| 153 | + public static Speed operator *(double left, Speed right) |
| 154 | + { |
| 155 | + return new Speed(left*right.MetersPerSecond); |
| 156 | + } |
| 157 | + |
| 158 | + public static Speed operator *(Speed left, double right) |
| 159 | + { |
| 160 | + return new Speed(left.MetersPerSecond*right); |
| 161 | + } |
| 162 | + |
| 163 | + public static Speed operator /(Speed left, double right) |
| 164 | + { |
| 165 | + return new Speed(left.MetersPerSecond/right); |
| 166 | + } |
| 167 | + |
| 168 | + public static double operator /(Speed left, Speed right) |
| 169 | + { |
| 170 | + return left.MetersPerSecond/right.MetersPerSecond; |
| 171 | + } |
| 172 | + |
| 173 | + #endregion |
| 174 | + |
| 175 | + #region Equality / IComparable |
| 176 | + |
| 177 | + public int CompareTo(object obj) |
| 178 | + { |
| 179 | + if (obj == null) throw new ArgumentNullException("obj"); |
| 180 | + if (!(obj is Speed)) throw new ArgumentException("Expected type Speed.", "obj"); |
| 181 | + return CompareTo((Speed) obj); |
| 182 | + } |
| 183 | + |
| 184 | + public int CompareTo(Speed other) |
| 185 | + { |
| 186 | + return MetersPerSecond.CompareTo(other.MetersPerSecond); |
| 187 | + } |
| 188 | + |
| 189 | + public static bool operator <=(Speed left, Speed right) |
| 190 | + { |
| 191 | + return left.MetersPerSecond <= right.MetersPerSecond; |
| 192 | + } |
| 193 | + |
| 194 | + public static bool operator >=(Speed left, Speed right) |
| 195 | + { |
| 196 | + return left.MetersPerSecond >= right.MetersPerSecond; |
| 197 | + } |
| 198 | + |
| 199 | + public static bool operator <(Speed left, Speed right) |
| 200 | + { |
| 201 | + return left.MetersPerSecond < right.MetersPerSecond; |
| 202 | + } |
| 203 | + |
| 204 | + public static bool operator >(Speed left, Speed right) |
| 205 | + { |
| 206 | + return left.MetersPerSecond > right.MetersPerSecond; |
| 207 | + } |
| 208 | + |
| 209 | + public static bool operator ==(Speed left, Speed right) |
| 210 | + { |
| 211 | + return left.MetersPerSecond == right.MetersPerSecond; |
| 212 | + } |
| 213 | + |
| 214 | + public static bool operator !=(Speed left, Speed right) |
| 215 | + { |
| 216 | + return left.MetersPerSecond != right.MetersPerSecond; |
| 217 | + } |
| 218 | + |
| 219 | + public override bool Equals(object obj) |
| 220 | + { |
| 221 | + if (obj == null || GetType() != obj.GetType()) |
| 222 | + { |
| 223 | + return false; |
| 224 | + } |
| 225 | + |
| 226 | + return MetersPerSecond.Equals(((Speed) obj).MetersPerSecond); |
| 227 | + } |
| 228 | + |
| 229 | + public override int GetHashCode() |
| 230 | + { |
| 231 | + return MetersPerSecond.GetHashCode(); |
| 232 | + } |
| 233 | + |
| 234 | + #endregion |
| 235 | + |
| 236 | + public override string ToString() |
| 237 | + { |
| 238 | + return string.Format("{0:0.##} {1}", MetersPerSecond, UnitSystem.Create().GetDefaultAbbreviation(Unit.MeterPerSecond)); |
| 239 | + } |
| 240 | + } |
| 241 | +} |
0 commit comments