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

Vector2<T> #1

Open
wants to merge 11 commits into
base: generic-vector
Choose a base branch
from
Open
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
112 changes: 112 additions & 0 deletions src/libraries/System.Numerics.Vectors/ref/System.Numerics.Vectors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,118 @@

namespace System.Numerics
{
public readonly struct Vector2<T> : IEquatable<Vector2<T>>, IFormattable
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to need to be autogenerated via the ref assembly generator. Won't block on this though.

where T : struct
{
// Fields

public T X { get { throw null; } } // Should this have a setter, a Vector2 WithX(T value), or be a public field like Vector2
public T Y { get { throw null; } } // Should this have a setter, a Vector2 WithY(T value), or be a public field like Vector2

// Constructors

public Vector2(T value) { throw null; }
public Vector2(T x, T y) { throw null; }

public Vector2(T[] value) { throw null; } // Doesn't exist for Vector2
public Vector2(T[] value, int offset) { throw null; } // Doesn't exist for Vector2
public Vector2(ReadOnlySpan<T> value) { throw null; } // Doesn't exist for Vector2

// Static Properties

public static Vector2<T> One { get { throw null; } }
public static Vector2<T> UnitX { get { throw null; } }
public static Vector2<T> UnitY { get { throw null; } }
public static Vector2<T> Zero { get { throw null; } }

// With methods
public Vector2<T> WithX(T x) { throw null; }
public Vector2<T> WithY(T y) { throw null; }

// Operators

public static bool operator ==(Vector2<T> left, Vector2<T> right) { throw null; }
public static bool operator !=(Vector2<T> left, Vector2<T> right) { throw null; }

public static Vector2<T> operator +(Vector2<T> value) { throw null; } // Doesn't exist for Vector2
public static Vector2<T> operator -(Vector2<T> value) { throw null; }

public static Vector2<T> operator +(Vector2<T> left, Vector2<T> right) { throw null; }
public static Vector2<T> operator -(Vector2<T> left, Vector2<T> right) { throw null; }

public static Vector2<T> operator *(Vector2<T> left, Vector2<T> right) { throw null; }
public static Vector2<T> operator /(Vector2<T> left, Vector2<T> right) { throw null; }

public static Vector2<T> operator *(Vector2<T> left, T right) { throw null; }
public static Vector2<T> operator /(Vector2<T> left, T right) { throw null; }

public static Vector2<T> operator *(T left, Vector2<T> right) { throw null; }

// "Friendly" Operators

public static Vector2<T> Plus(Vector2<T> value) { throw null; } // Doesn't exist for Vector2
public static Vector2<T> Negate(Vector2<T> value) { throw null; }

public static Vector2<T> Add(Vector2<T> left, Vector2<T> right) { throw null; }
public static Vector2<T> Subtract(Vector2<T> left, Vector2<T> right) { throw null; }

public static Vector2<T> Multiply(Vector2<T> left, Vector2<T> right) { throw null; }
public static Vector2<T> Divide(Vector2<T> left, Vector2<T> right) { throw null; }

public static Vector2<T> Multiply(Vector2<T> left, T right) { throw null; }
public static Vector2<T> Divide(Vector2<T> left, T right) { throw null; }

public static Vector2<T> Multiply(T left, Vector2<T> right) { throw null; }

// Static Methods

public static Vector2<T> Abs(Vector2<T> value) { throw null; }

public static Vector2<T> Clamp(Vector2<T> value, Vector2<T> min, Vector2<T> max) { throw null; }

public static T Distance(Vector2<T> left, Vector2<T> right) { throw null; }
public static T DistanceSquared(Vector2<T> left, Vector2<T> right) { throw null; }

public static T Dot(Vector2<T> left, Vector2<T> right) { throw null; }

public static Vector2<T> Lerp(Vector2<T> min, Vector2<T> max, T amount) { throw null; }

public static Vector2<T> Min(Vector2<T> left, Vector2<T> right) { throw null; }
public static Vector2<T> Max(Vector2<T> left, Vector2<T> right) { throw null; }

public static Vector2<T> Normalize(Vector2<T> value) { throw null; }

public static Vector2<T> Reflect(Vector2<T> incident, Vector2<T> normal) { throw null; }

public static Vector2<T> SquareRoot(Vector2<T> value) { throw null; }

//public static Vector2<T> Transform(Vector2<T> position, Matrix3x2<T> matrix) { throw null; }
//public static Vector2<T> Transform(Vector2<T> position, Matrix4x4<T> matrix) { throw null; }

//public static Vector2<T> Transform(Vector2<T> position, Quaternion<T> rotation) { throw null; } // Rotate is a better name?

//public static Vector2<T> TransformNormal(Vector2<T> normal, Matrix3x2<T> matrix) { throw null; }
//public static Vector2<T> TransformNormal(Vector2<T> normal, Matrix4x4<T> matrix) { throw null; }

// Methods

public readonly void CopyTo(T[] array) { throw null; }
public readonly void CopyTo(T[] array, int index) { throw null; }
public readonly void CopyTo(Span<T> destination) { throw null; } // Doesn't exist for Vector2

public override readonly bool Equals(object? obj) { throw null; }
public readonly bool Equals(Vector2<T> other) { throw null; }

public override readonly int GetHashCode() { throw null; }

public readonly T Length() { throw null; } // Better as a property?
public readonly T LengthSquared() { throw null; } // Better as a property?

public readonly override string ToString() { throw null; }
public readonly string ToString(string? format) { throw null; }
public readonly string ToString(string? format, IFormatProvider? formatProvider) { throw null; }
}

public partial struct Matrix3x2 : System.IEquatable<System.Numerics.Matrix3x2>
{
public float M11;
Expand Down
32 changes: 32 additions & 0 deletions src/libraries/System.Numerics.Vectors/tests/MathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics;
tannergooding marked this conversation as resolved.
Show resolved Hide resolved

namespace System.Numerics
{
static class MathHelper
Expand All @@ -24,6 +26,21 @@ public static bool Equal(float a, float b)
return (Math.Abs(a - b) < 1e-5);
}

public static bool Equal<T>(T a, T b)
john-h-k marked this conversation as resolved.
Show resolved Hide resolved
{
if (typeof(T) == typeof(float))
{
return (Math.Abs((float)(object)a - (float)(object)b) < 1e-5);
}
else if (typeof(T) == typeof(double))
{
return (Math.Abs((double)(object)a - (double)(object)b) < 1e-5);
}

Debug.Fail("Type has not been added");
return false;
}

public static bool Equal(Vector2 a, Vector2 b)
{
return Equal(a.X, b.X) && Equal(a.Y, b.Y);
Expand All @@ -39,6 +56,21 @@ public static bool Equal(Vector4 a, Vector4 b)
return Equal(a.X, b.X) && Equal(a.Y, b.Y) && Equal(a.Z, b.Z) && Equal(a.W, b.W);
}

public static bool Equal<T>(Vector2<T> a, Vector2<T> b) where T : struct
{
return Equal(a.X, b.X) && Equal(a.Y, b.Y);
}

public static bool Equal<T>(Vector3<T> a, Vector3<T> b) where T : struct
{
return Equal(a.X, b.X) && Equal(a.Y, b.Y) && Equal(a.Z, b.Z);
}

public static bool Equal<T>(Vector4<T> a, Vector4<T> b) where T : struct
{
return Equal(a.X, b.X) && Equal(a.Y, b.Y) && Equal(a.Z, b.Z) && Equal(a.W, b.W);
}

public static bool Equal(Matrix4x4 a, Matrix4x4 b)
{
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
<DependentUpon>GenericVectorTests.tt</DependentUpon>
</Compile>
<Compile Include="Util.cs" />
<Compile Include="Vector2Tests_NonGeneric.cs" />
<Compile Include="Vector2Tests.cs" />
<Compile Include="Vector3Tests.cs" />
<Compile Include="Vector4Tests.cs" />
<Compile Include="Vector2_SingleTests.cs" />
<Compile Include="Vector2_DoubleTests.cs" />
<!-- <Compile Include="Vector3Tests.cs" />
<Compile Include="Vector4Tests.cs" /> -->
<Compile Include="MathHelper.cs" />
<Compile Include="Matrix3x2Tests.cs" />
<Compile Include="Matrix4x4Tests.cs" />
Expand Down
Loading