namespace Zinnia.Extension
{
using UnityEngine;
///
/// Extended methods for the Type.
///
public static class Vector2Extensions
{
///
/// Determines if two values are equal based on a given tolerance.
///
/// The to compare against.
/// The to compare with.
/// The tolerance in which the two values can be within to be considered equal.
/// if the two values are considered equal.
public static bool ApproxEquals(this Vector2 a, Vector2 b, float tolerance = float.Epsilon)
{
return Vector2.Distance(a, b) <= tolerance;
}
///
/// Divides each component of the given against the given .
///
/// The value to divide by each component.
/// The components to divide with.
/// The quotient.
public static Vector2 Divide(float dividend, Vector2 divisor)
{
return new Vector2(dividend / divisor.x, dividend / divisor.y);
}
///
/// Divides two s component-wise.
///
/// The value to divide by each component.
/// The components to divide with.
/// The quotient.
public static Vector2 Divide(this Vector2 dividend, Vector2 divisor)
{
return Vector2.Scale(dividend, Divide(1, divisor));
}
///
/// Check whether the source and target points are within a given tolerance distance of each other.
///
/// The source point.
/// The target point.
/// The tolerance of distance equality.
/// Whether the source and target are within the distance equality tolerance.
public static bool WithinDistance(this Vector2 a, Vector2 b, Vector2 tolerance)
{
return a.x.ApproxEquals(b.x, tolerance.x) && a.y.ApproxEquals(b.y, tolerance.y);
}
///
/// Converts an unsigned Euler angle into a signed Euler angle.
///
/// The unsigned Euler angle to convert.
/// The converted signed Euler angle.
public static Vector2 UnsignedEulerToSignedEuler(this Vector2 eulerAngles)
{
return new Vector2(eulerAngles.x.GetSignedDegree(), eulerAngles.y.GetSignedDegree());
}
///
/// Gets the direction from a source to a target.
///
/// The starting point.
/// The finishing point.
/// Whether to normalize the direction.
/// The direction that the target is in from the source.
public static Vector2 Direction(this Vector2 source, Vector2 target, bool isNormalized = false)
{
Vector3 heading = target - source;
return heading / (isNormalized ? heading.magnitude : 1f);
}
}
}