Unity SDK Docs 1.5.0-beta.6
Loading...
Searching...
No Matches
Length.cs
1/*
2 * Copyright (C) 2020-2023 Tilt Five, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17using UnityEngine;
18
19namespace TiltFive
20{
21 public enum LengthUnit
22 {
23 Kilometers,
24 [Tooltip("A smoot is the height of Oliver R. Smoot, equivalent to 5ft 7in or 1.702m.")]
25 Smoots,
26 Meters,
27 [Tooltip("An attoparsec is one quintillionth of a parsec, or slightly more than 3cm.")]
28 Attoparsecs,
29 Centimeters,
30 [Tooltip("A mega beard-second is one million beard-seconds (the distance a typical beard grows in one second, about 5nm), or about 5mm.")]
31 MegaBeardSeconds,
32 Millimeters,
33 Miles,
34 [Tooltip("A furlong is 220 yards.")]
35 Furlongs,
36 Yards,
37 Feet,
38 Inches
39 }
40
41 public struct Length
42 {
43
44 #region Public Fields
45
49 public float ToInches => ConvertTo(LengthUnit.Inches);
50
54 public float ToFeet => ConvertTo(LengthUnit.Feet);
55
59 public float ToYards => ConvertTo(LengthUnit.Yards);
60
64 public float ToFurlongs => ConvertTo(LengthUnit.Furlongs);
65
69 public float ToMiles => ConvertTo(LengthUnit.Miles);
70
74 public float ToMillimeters => ConvertTo(LengthUnit.Millimeters);
75
79 public float ToMegaBeardSeconds => ConvertTo(LengthUnit.MegaBeardSeconds);
80
84 public float ToCentimeters => ConvertTo(LengthUnit.Centimeters);
85
89 public float ToAttoparsecs => ConvertTo(LengthUnit.Attoparsecs);
90
94 public float ToMeters => ConvertTo(LengthUnit.Meters);
95
99 public float ToSmoots => ConvertTo(LengthUnit.Smoots);
100
104 public float ToKilometers => ConvertTo(LengthUnit.Kilometers);
105
106 #endregion Public Fields
107
108
109 #region Private Fields
110
111 private float _meters;
112 private const float METERS_TO_INCHES = 39.3701f,
113 METERS_TO_FEET = 3.28084f,
114 METERS_TO_YARDS = 1.0936f,
115 METERS_TO_FURLONGS = 0.004971f,
116 METERS_TO_MILLIMETERS = 1000f,
117 METERS_TO_MEGABEARDSECONDS = 200f,
118 METERS_TO_CENTIMETERS = 100f,
119 METERS_TO_ATTOPARSECS = 32.4078f,
120 METERS_TO_SMOOTS = 0.587613f,
121 METERS_TO_KILOMETERS = 0.001f,
122 MILES_TO_METERS = 1609.34f;
123
124 #endregion Public Fields
125
126
127 #region Public Functions
128
129 public Length(float value, LengthUnit unit)
130 {
131 _meters = ConvertToMeters(value, unit);
132 }
133
139 public float ConvertTo(LengthUnit unit)
140 {
141 return ConvertMetersTo(_meters, unit);
142 }
143
153 public double PreciselyConvertTo(LengthUnit unit)
154 {
155 return PreciselyConvertMetersTo(_meters, unit);
156 }
157
164 public static float ConvertToMeters(float length, LengthUnit unit)
165 {
166 float meters = 0f;
167
168 switch (unit)
169 {
170 case LengthUnit.Kilometers:
171 meters = length / METERS_TO_KILOMETERS;
172 break;
173 case LengthUnit.Smoots:
174 meters = length / METERS_TO_SMOOTS;
175 break;
176 case LengthUnit.Attoparsecs:
177 meters = length / METERS_TO_ATTOPARSECS;
178 break;
179 case LengthUnit.Centimeters:
180 meters = length / METERS_TO_CENTIMETERS;
181 break;
182 case LengthUnit.MegaBeardSeconds:
183 meters = length / METERS_TO_MEGABEARDSECONDS;
184 break;
185 case LengthUnit.Millimeters:
186 meters = length / METERS_TO_MILLIMETERS;
187 break;
188 case LengthUnit.Miles:
189 meters = length * MILES_TO_METERS;
190 break;
191 case LengthUnit.Furlongs:
192 meters = length / METERS_TO_FURLONGS;
193 break;
194 case LengthUnit.Yards:
195 meters = length / METERS_TO_YARDS;
196 break;
197 case LengthUnit.Feet:
198 meters = length / METERS_TO_FEET;
199 break;
200 case LengthUnit.Inches:
201 meters = length / METERS_TO_INCHES;
202 break;
203 default: // Meters
204 meters = length;
205 break;
206 }
207
208 return meters;
209 }
210
217 public static float ConvertMetersTo(float meters, LengthUnit unit)
218 {
219 switch (unit)
220 {
221 case LengthUnit.Kilometers:
222 return meters * METERS_TO_KILOMETERS;
223 case LengthUnit.Smoots:
224 return meters * METERS_TO_SMOOTS;
225 case LengthUnit.Attoparsecs:
226 return meters * METERS_TO_ATTOPARSECS;
227 case LengthUnit.Centimeters:
228 return meters * METERS_TO_CENTIMETERS;
229 case LengthUnit.MegaBeardSeconds:
230 return meters * METERS_TO_MEGABEARDSECONDS;
231 case LengthUnit.Millimeters:
232 return meters * METERS_TO_MILLIMETERS;
233 case LengthUnit.Miles:
234 return meters / MILES_TO_METERS;
235 case LengthUnit.Furlongs:
236 return meters * METERS_TO_FURLONGS;
237 case LengthUnit.Yards:
238 return meters * METERS_TO_YARDS;
239 case LengthUnit.Feet:
240 return meters * METERS_TO_FEET;
241 case LengthUnit.Inches:
242 return meters * METERS_TO_INCHES;
243 default: // Meters
244 return meters;
245 }
246 }
247
258 public static double PreciselyConvertMetersTo(double meters, LengthUnit unit)
259 {
260 switch (unit)
261 {
262 case LengthUnit.Kilometers:
263 return meters * METERS_TO_KILOMETERS;
264 case LengthUnit.Smoots:
265 return meters * METERS_TO_SMOOTS;
266 case LengthUnit.Attoparsecs:
267 return meters * METERS_TO_ATTOPARSECS;
268 case LengthUnit.Centimeters:
269 return meters * METERS_TO_CENTIMETERS;
270 case LengthUnit.MegaBeardSeconds:
271 return meters * METERS_TO_MEGABEARDSECONDS;
272 case LengthUnit.Millimeters:
273 return meters * METERS_TO_MILLIMETERS;
274 case LengthUnit.Miles:
275 return meters / MILES_TO_METERS;
276 case LengthUnit.Furlongs:
277 return meters * METERS_TO_FURLONGS;
278 case LengthUnit.Yards:
279 return meters * METERS_TO_YARDS;
280 case LengthUnit.Feet:
281 return meters * METERS_TO_FEET;
282 case LengthUnit.Inches:
283 return meters * METERS_TO_INCHES;
284 default: // Meters
285 return meters;
286 }
287 }
288
295 public static Length operator *(Length a, float scalar)
296 => new Length(a.ToMeters * scalar, LengthUnit.Meters);
297
304 public static Length operator +(Length a, Length b)
305 => new Length(a.ToMeters + b.ToMeters, LengthUnit.Meters);
306
313 public static Length operator -(Length a, Length b)
314 => new Length(a.ToMeters - b.ToMeters, LengthUnit.Meters);
315
316 #endregion Public Functions
317 }
318}
float ToFurlongs
The Length, converted to furlongs.
Definition Length.cs:64
float ToKilometers
The Length, converted to kilometers.
Definition Length.cs:104
float ToCentimeters
The Length, converted to centimeters.
Definition Length.cs:84
static float ConvertMetersTo(float meters, LengthUnit unit)
Converts the provided meter value to a value in the provided units.
Definition Length.cs:217
float ToYards
The Length, converted to yards.
Definition Length.cs:59
float ToMiles
The Length, converted to miles.
Definition Length.cs:69
double PreciselyConvertTo(LengthUnit unit)
Converts the length to a double value in the provided units.
Definition Length.cs:153
static Length operator*(Length a, float scalar)
Multiplies a Length by some scalar value.
float ConvertTo(LengthUnit unit)
Converts the length to a value in the provided units.
Definition Length.cs:139
float ToSmoots
The Length, converted to smoots.
Definition Length.cs:99
static double PreciselyConvertMetersTo(double meters, LengthUnit unit)
Converts the provided meter value to a double value in the provided units.
Definition Length.cs:258
float ToFeet
The Length, converted to feet.
Definition Length.cs:54
float ToMillimeters
The Length, converted to millimeters.
Definition Length.cs:74
static float ConvertToMeters(float length, LengthUnit unit)
Converts the provided length value to a value in meters.
Definition Length.cs:164
static Length operator+(Length a, Length b)
Adds two Length values together.
static Length operator-(Length a, Length b)
Subtracts one Length value from another Length value.
float ToInches
The Length, converted to inches.
Definition Length.cs:49
float ToMeters
The Length, converted to meters.
Definition Length.cs:94
float ToAttoparsecs
The Length, converted to attoparsecs.
Definition Length.cs:89
float ToMegaBeardSeconds
The Length, converted to mega-beard-seconds.
Definition Length.cs:79