using DotSDL.Exceptions;
using System;
using System.Collections.Generic;
namespace DotSDL.Math.Vector {
///
/// Represents a generic vector type.
///
public class Vector2 : IEquatable> {
public T X { get; set; }
public T Y { get; set; }
///
/// Initializes a new object.
///
/// Thrown if this is created with an unsupported type.
public Vector2() {
if(!VectorBase.IsNumericType())
throw new InvalidTypeException(GetType().ToString(), typeof(T));
}
///
/// Initializes a new object using the values from another .
///
/// The that should be used to initialize the new object.
/// Thrown if this is created with an unsupported type.
public Vector2(Vector2 initialValue) : this() {
X = initialValue.X;
Y = initialValue.Y;
}
///
/// Initializes a new object with the X and Y values both set to a given value.
///
/// The value to initialize the X and Y values to.
/// Thrown if this is created with an unsupported type.
public Vector2(T initialValue) : this() {
X = Y = initialValue;
}
///
/// Initializes a new object with preset X and Y values.
///
/// The initial value to set the X component to.
/// The initial value to set the Y component to.
/// Thrown if this is created with an unsupported type.
public Vector2(T initialX, T initialY) : this() {
X = initialX;
Y = initialY;
}
///
/// Checks to see if two objects are equal.
///
/// The first in the comparison.
/// The second in the comparison.
/// true if the two objects are equal, otherwise false.
public static bool operator ==(Vector2 left, Vector2 right) {
if(left is null && right is null) return true;
if(left is null || right is null) return false;
if(left.GetType() != right.GetType()) return false;
return left.X.Equals(right.X) && left.Y.Equals(right.Y);
}
///
/// Checks to see if two objects are different.
///
/// The first in the comparison.
/// The second in the comparison.
/// true if the two objects are different, otherwise false.
public static bool operator !=(Vector2 left, Vector2 right) {
return !(left == right);
}
/// Returns a new with both X and Y set to 1.
public static Vector2 One => new Vector2(VectorBase.GetOne());
/// Returns a new containing (1, 0).
public static Vector2 UnitX => new Vector2(VectorBase.GetOne(), VectorBase.GetZero());
/// Returns a new containing (0, 1).
public static Vector2 UnitY => new Vector2(VectorBase.GetZero(), VectorBase.GetOne());
/// Returns a new with both X and Y set to 0.
public static Vector2 Zero => new Vector2(VectorBase.GetZero());
///
public bool Equals(Vector2 other) {
if(ReferenceEquals(null, other)) return false;
if(ReferenceEquals(this, other)) return true;
return EqualityComparer.Default.Equals(X, other.X) && EqualityComparer.Default.Equals(Y, other.Y);
}
///
/// Determines whether this is equal to another one.
///
/// Another to compare this instance to.
/// true if the two objects are equal, otherwise false.
public override bool Equals(object obj) {
if(!(obj is Vector2)) return false;
return this == (Vector2)obj;
}
///
public override int GetHashCode() {
unchecked {
return (EqualityComparer.Default.GetHashCode(X) * 397) ^ EqualityComparer.Default.GetHashCode(Y);
}
}
}
}