using System.Numerics;
using MoonTools.Core.Curve.Extensions;
namespace MoonTools.Core.Curve
{
///
/// A 2-dimensional Bezier curve defined by 4 points.
///
public struct CubicBezierCurve2D
{
///
/// The start point.
///
public Vector2 p0;
///
/// The first control point.
///
public Vector2 p1;
///
/// The second control point.
///
public Vector2 p2;
///
/// The end point.
///
public Vector2 p3;
/// The start point.
/// The first control point.
/// The second control point.
/// The end point.
public CubicBezierCurve2D(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3)
{
this.p0 = p0;
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
///
/// Returns the curve coordinate given by t.
///
/// A value between 0 and 1.
public Vector2 Point(float t) => Point(p0, p1, p2, p3, t);
///
/// Returns the curve coordinate given by a normalized time value.
///
/// A value between startTime and endTime.
///
///
public Vector2 Point(float t, float startTime, float endTime) => Point(p0, p1, p2, p3, t, startTime, endTime);
///
/// Returns the instantaneous velocity on the curve given by t.
///
/// A value between 0 and 1.
public Vector2 Velocity(float t) => Velocity(p0, p1, p2, p3, t);
///
/// Returns the instantaneous velocity on the curve given by a normalized time value.
///
/// A value between startTime and endTime.
///
///
public Vector2 Velocity(float t, float startTime, float endTime) => Velocity(p0, p1, p2, p3, TimeHelper.Normalized(t, startTime, endTime));
///
/// Returns the curve coordinate given by 4 points and a time value.
///
/// The start point.
/// The first control point.
/// The second control point.
/// The end point.
/// A value between 0 and 1.
public static Vector2 Point(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t)
{
return CubicBezierCurve3D.Point(
new Vector3(p0.X, p0.Y, 0),
new Vector3(p1.X, p1.Y, 0),
new Vector3(p2.X, p2.Y, 0),
new Vector3(p3.X, p3.Y, 0),
t
).XY();
}
///
/// Returns the curve coordinate given by 4 points and a normalized time value.
///
/// The start point.
/// The first control point.
/// The second control point.
/// The end point.
/// A value between startTime and endTime.
///
///
public static Vector2 Point(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t, float startTime, float endTime)
{
return Point(p0, p1, p2, p3, TimeHelper.Normalized(t, startTime, endTime));
}
///
/// Returns the instantaneous velocity given by 4 points and a time value.
///
/// The start point.
/// The first control point.
/// The second control point.
/// The end point.
/// A value between 0 and 1.
public static Vector2 Velocity(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t)
{
return CubicBezierCurve3D.Velocity(
new Vector3(p0.X, p0.Y, 0),
new Vector3(p1.X, p1.Y, 0),
new Vector3(p2.X, p2.Y, 0),
new Vector3(p3.X, p3.Y, 0),
t
).XY();
}
///
/// Returns the instantaneous velocity given by 4 points and a normalized time value.
///
/// The start point.
/// The first control point.
/// The second control point.
/// The end point.
/// A value between startTime and endTime.
///
///
public static Vector2 Velocity(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3, float t, float minT, float maxT)
{
return Velocity(p0, p1, p2, p3, TimeHelper.Normalized(t, minT, maxT));
}
}
}