using UnityEngine;
using M = System.Math;
// ReSharper disable InconsistentNaming
namespace ARLocation
{
[System.Serializable]
public struct DVector2
{
public double x;
public double y;
///
/// Gets the magnitude of the vector.
///
/// The magnitude.
public double magnitude
{
get
{
return M.Sqrt(x * x + y * y);
}
}
///
/// Gets the normalized version of this vector.
///
/// The normalized.
public DVector2 normalized
{
get
{
var m = magnitude;
if (m < 0.00000001)
{
return new DVector2();
}
return new DVector2(x, y) / magnitude;
}
}
public DVector2 Clone()
{
return new DVector2(x, y);
}
///
/// Initializes a new instance of the struct.
///
/// The x coordinate.
/// The y coordinate.
public DVector2(double x = 0.0, double y = 0.0)
{
this.x = x;
this.y = y;
}
///
/// Converts to a Vector2.
///
/// The vector2.
public Vector2 toVector2()
{
return new Vector2((float)x, (float)y);
}
///
/// Equals the specified v and e.
///
/// The equals.
/// V.
/// E.
public bool Equals(DVector2 v, double e = 0.00005)
{
return (M.Abs(x - v.x) <= e) && (M.Abs(y - v.y) <= e);
}
///
/// Normalize this instance.
///
public void Normalize()
{
double m = magnitude;
x /= m;
y /= m;
}
///
/// Set the specified x and y.
///
///
///
public void Set(double newX = 0.0, double newY = 0.0)
{
x = newX;
y = newY;
}
///
/// Returns a that represents the current .
///
/// A that represents the current .
override public string ToString()
{
return "(" + x + ", " + y + ")";
}
///
/// Dot the specified a and b.
///
/// The dot.
/// The alpha component.
/// The blue component.
public static double Dot(DVector2 a, DVector2 b)
{
return a.x * b.x + a.y * b.y;
}
///
/// Distance the specified a and b.
///
/// The distance.
/// The alpha component.
/// The blue component.
public static double Distance(DVector2 a, DVector2 b)
{
return M.Sqrt(a.x * b.x + a.y * b.y);
}
///
/// Lerp the specified a, b and t.
///
/// The lerp.
/// The alpha component.
/// The blue component.
/// T.
public static DVector2 Lerp(DVector2 a, DVector2 b, double t)
{
double s = M.Max(0, M.Min(t, 1));
return a * (1 - s) + b * s;
}
///
/// Computes the product of a and b, yielding a new .
///
/// The to multiply.
/// The to multiply.
/// The that is the a * b.
public static DVector2 operator *(DVector2 a, double b)
{
return new DVector2(
b * a.x,
b * a.y
);
}
///
/// Computes the division of a and b, yielding a new .
///
/// The to divide (the divident).
/// The to divide (the divisor).
/// The that is the a / b.
public static DVector2 operator /(DVector2 a, double b)
{
return new DVector2(
a.x / b,
a.y / b
);
}
///
/// Adds a to a , yielding a new .
///
/// The first to add.
/// The second to add.
/// The that is the sum of the values of a and b.
public static DVector2 operator +(DVector2 a, DVector2 b)
{
return new DVector2(
a.x + b.x,
a.y + b.y
);
}
///
/// Subtracts a from a , yielding a new .
///
/// The to subtract from (the minuend).
/// The to subtract (the subtrahend).
/// The that is the a minus b.
public static DVector2 operator -(DVector2 a, DVector2 b)
{
return new DVector2(
a.x - b.x,
a.y - b.y
);
}
}
}