Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beginning vector #801

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions UnitsNet.Tests/VectorTests.cs
Original file line number Diff line number Diff line change
@@ -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<Length>(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<Length>(length1X, length1Y, length1Z);
var vector2 = new Vector3<Length>(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<Length>( expectedX, expectedY, expectedZ ), result );
}
}
}
90 changes: 90 additions & 0 deletions UnitsNet/Vector3.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.

using System;

namespace UnitsNet
{
/// <summary>
/// Represents a three-dimensional vector.
/// </summary>
/// <typeparam name="T">The unit type of the quantity.</typeparam>
public class Vector3<T> : IEquatable<Vector3<T>>
{
/// <summary>
/// Creates a new three-dimensional vector.
/// </summary>
/// <param name="x">The X component of the vector.</param>
/// <param name="y">The Y component of the vector.</param>
/// <param name="z">The Z component of the vector.</param>
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));
}

/// <inheritdoc />
public override bool Equals(object obj)
{
if(obj is null || !(obj is Vector3<T> vector))
return false;

return Equals(vector);
}

/// <inheritdoc />
public bool Equals(Vector3<T> other)
{
if(other is null)
throw new ArgumentNullException(nameof(other));

return X!.Equals(other.X) && Y!.Equals(other.Y) && Z!.Equals(other.Z);
}

/// <inheritdoc />
public override int GetHashCode()
{
return new {X, Y, Z}.GetHashCode();
}

/// <summary>
/// Adds two three-dimensional vectors together.
/// </summary>
/// <param name="left">The left hand side vector.</param>
/// <param name="right">The right hand side vector.</param>
/// <returns>The added vector.</returns>
public static Vector3<T> operator+(Vector3<T> left, Vector3<T> 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<T>(x, y, z);
}

/// <summary>
/// The X component of the vector.
/// </summary>
public T X
{
get;
}

/// <summary>
/// The Y component of the vector.
/// </summary>
public T Y
{
get;
}

/// <summary>
/// The Z component of the vector.
/// </summary>
public T Z
{
get;
}
}
}