using Newtonsoft.Json;
using RhythmBase.Converters;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
namespace RhythmBase.Components
{
///
/// A point whose horizontal and vertical coordinates are non-nullable
///
[JsonConverter(typeof(RDPointsConverter))]
[DebuggerDisplay($"{{{nameof(GetDebuggerDisplay)}(),nq}}")]
public struct RDPointN(float x, float y) : IRDVortex, IRDVortex
{
///
/// Initializes a new instance of the struct with the specified size.
///
/// The size to initialize the point with.
public RDPointN(RDSizeN sz) : this(sz.Width, sz.Height) { }
///
/// Gets or sets the X coordinate of the point.
///
public float X { get; set; } = x;
///
/// Gets or sets the Y coordinate of the point.
///
public float Y { get; set; } = y;
///
/// Offsets the point by the specified size.
///
/// The size to offset the point by.
public void Offset(RDSizeN p)
{
X += p.Width;
Y += p.Height;
}
///
/// Offsets the point by the specified point.
///
/// The point to offset the point by.
public void Offset(RDPointN p)
{
X += p.X;
Y += p.Y;
}
///
/// Offsets the point by the specified horizontal and vertical amounts.
///
/// The horizontal amount to offset the point by.
/// The vertical amount to offset the point by.
public void Offset(float dx, float dy)
{
X += dx;
Y += dy;
}
///
/// Adds the specified size to the point.
///
/// The point to add to.
/// The size to add.
/// A new point that is the result of the addition.
public static RDPointN Add(RDPointN pt, RDSizeNI sz)
{
RDPointN Add = new(pt.X + (float)sz.Width, pt.Y + (float)sz.Height);
return Add;
}
///
/// Adds the specified size to the point.
///
/// The point to add to.
/// The size to add.
/// A new point that is the result of the addition.
public static RDPointN Add(RDPointN pt, RDSizeN sz)
{
RDPointN Add = new(pt.X + sz.Width, pt.Y + sz.Height);
return Add;
}
///
/// Subtracts the specified size from the point.
///
/// The point to subtract from.
/// The size to subtract.
/// A new point that is the result of the subtraction.
public static RDPointN Subtract(RDPointN pt, RDSizeNI sz)
{
RDPointN Subtract = new(pt.X - (float)sz.Width, pt.Y - (float)sz.Height);
return Subtract;
}
///
/// Subtracts the specified size from the point.
///
/// The point to subtract from.
/// The size to subtract.
/// A new point that is the result of the subtraction.
public static RDPointN Subtract(RDPointN pt, RDSizeN sz)
{
RDPointN Subtract = new(pt.X - sz.Width, pt.Y - sz.Height);
return Subtract;
}
///
/// Multiplies the point by the specified 2x2 matrix.
///
/// The 2x2 matrix to multiply the point by.
/// A new point that is the result of the multiplication.
/// Thrown when the matrix is not a 2x2 matrix.
public readonly RDPointN MultipyByMatrix(float[,] matrix)
{
if (matrix.Rank == 2 && matrix.Length == 4)
{
RDPointN MultipyByMatrix = new(X * matrix[0, 0] + Y * matrix[1, 0], X * matrix[0, 1] + Y * matrix[1, 1]);
return MultipyByMatrix;
}
throw new Exception("Matrix not match, 2*2 matrix expected.");
}
///
/// Rotate.
///
public readonly RDPointN Rotate(float angle)
{
float[,] array = new float[2, 2];
array[0, 0] = (float)Math.Cos((double)angle);
array[0, 1] = (float)Math.Sin((double)angle);
array[1, 0] = (float)-(float)Math.Sin((double)angle);
array[1, 1] = (float)Math.Cos((double)angle);
return MultipyByMatrix(array);
}
///
/// Rotate at a given pivot.
///
/// Given pivot.
/// Angle.
///
public readonly RDPointN Rotate(RDPointN pivot, float angle) => (this - new RDSizeN(pivot)).Rotate(angle) + new RDSizeN(pivot);
///
public override readonly bool Equals([NotNullWhen(true)] object? obj) => obj is RDPointN e && Equals(e);
///
public override readonly int GetHashCode()
{
HashCode h = default;
h.Add(X);
h.Add(Y);
return h.ToHashCode();
}
///
public override readonly string ToString() => $"[{X}, {Y}]";
///
public readonly bool Equals(RDPointN other) => other.X == X && other.Y == Y;
///
public static RDPointN operator +(RDPointN pt, RDSizeNI sz) => Add(pt, sz);
///
public static RDPointN operator +(RDPointN pt, RDSizeN sz) => Add(pt, sz);
///
public static RDPointN operator -(RDPointN pt, RDSizeNI sz) => Subtract(pt, sz);
///
public static RDPointN operator -(RDPointN pt, RDSizeN sz) => Subtract(pt, sz);
///
public static RDPointN operator *(RDPointN pt, float x) => new(pt.X * x, pt.Y * x);
///
public static RDPointN operator /(RDPointN pt, float x) => new(pt.X / x, pt.Y / x);
///
public static bool operator ==(RDPointN left, RDPointN right) => left.Equals(right);
///
public static bool operator !=(RDPointN left, RDPointN right) => !left.Equals(right);
///
/// Implicitly converts an to an .
///
/// The to convert.
/// A new with the same coordinates.
public static implicit operator RDPoint(RDPointN p) => new(new float?(p.X), new float?(p.Y));
///
/// Implicitly converts an to an .
///
/// The to convert.
/// A new with the same coordinates.
public static implicit operator RDPointE(RDPointN p) => new(p.X, p.Y);
///
/// Explicitly converts an to an .
///
/// The to convert.
/// A new with the same dimensions.
public static explicit operator RDSizeN(RDPointN p) => new(p.X, p.Y);
private readonly string GetDebuggerDisplay() => ToString();
}
}