diff --git a/UnitsNet.Tests/VectorTests.cs b/UnitsNet.Tests/VectorTests.cs new file mode 100644 index 0000000000..bcbbbae565 --- /dev/null +++ b/UnitsNet.Tests/VectorTests.cs @@ -0,0 +1,45 @@ +using System; +using UnitsNet.Units; +using Xunit; + +namespace UnitsNet.Tests +{ + public class VectorTests + { + [Fact] + public void Constructor_InitializesProperties() + { + var lengthX = Length.FromMeters(1.0); + var lengthY = Length.FromMeters(2.0); + var lengthZ = Length.FromMeters(3.0); + + var vector = new Vector3(lengthX, lengthY, lengthZ); + + Assert.Equal(lengthX, vector.X); + Assert.Equal(lengthY, vector.Y); + Assert.Equal(lengthZ, vector.Z); + } + + [Fact] + public void Add_WithSameUnits() + { + var length1X = Length.FromMeters(1.0); + var length1Y = Length.FromMeters(2.0); + var length1Z = Length.FromMeters(3.0); + var length2X = Length.FromMeters(4.0); + var length2Y = Length.FromMeters(5.0); + var length2Z = Length.FromMeters(6.0); + + var vector1 = new Vector3(length1X, length1Y, length1Z); + var vector2 = new Vector3(length2X, length2Y, length2Z); + + var result = vector1 + vector2; + + var expectedX = Length.FromMeters(5.0); + var expectedY = Length.FromMeters(7.0); + var expectedZ = Length.FromMeters(9.0); + + Assert.Equal( new Vector3( expectedX, expectedY, expectedZ ), result ); + } + } +} diff --git a/UnitsNet/Vector3.cs b/UnitsNet/Vector3.cs new file mode 100644 index 0000000000..9efbe5f5af --- /dev/null +++ b/UnitsNet/Vector3.cs @@ -0,0 +1,90 @@ +// Licensed under MIT No Attribution, see LICENSE file at the root. +// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet. + +using System; + +namespace UnitsNet +{ + /// + /// Represents a three-dimensional vector. + /// + /// The unit type of the quantity. + public class Vector3 : IEquatable> + { + /// + /// Creates a new three-dimensional vector. + /// + /// The X component of the vector. + /// The Y component of the vector. + /// The Z component of the vector. + public Vector3(T x, T y, T z) + { + X = x ?? throw new ArgumentNullException(nameof(x)); + Y = y ?? throw new ArgumentNullException(nameof(y)); + Z = z ?? throw new ArgumentNullException(nameof(z)); + } + + /// + public override bool Equals(object obj) + { + if(obj is null || !(obj is Vector3 vector)) + return false; + + return Equals(vector); + } + + /// + public bool Equals(Vector3 other) + { + if(other is null) + throw new ArgumentNullException(nameof(other)); + + return X!.Equals(other.X) && Y!.Equals(other.Y) && Z!.Equals(other.Z); + } + + /// + public override int GetHashCode() + { + return new {X, Y, Z}.GetHashCode(); + } + + /// + /// Adds two three-dimensional vectors together. + /// + /// The left hand side vector. + /// The right hand side vector. + /// The added vector. + public static Vector3 operator+(Vector3 left, Vector3 right) + { + var x = CompiledLambdas.Add(left.X, right.X); + var y = CompiledLambdas.Add(left.Y, right.Y); + var z = CompiledLambdas.Add(left.Z, right.Z); + + return new Vector3(x, y, z); + } + + /// + /// The X component of the vector. + /// + public T X + { + get; + } + + /// + /// The Y component of the vector. + /// + public T Y + { + get; + } + + /// + /// The Z component of the vector. + /// + public T Z + { + get; + } + } +}