/// @file Bezier.hpp /// @desc Cubic Bezier Curve impl. using De Casteljau's Method /// @author Joshua O'Leary /// @edited 20 June 2024 /// @version 2 /// @copyright (c) Redacted Software 2024 /// @license Unlicense - Public Domain #pragma once // Bezier Method: // p = (1-t)^3 * P0 + 3*t*(1-t)^2*P1 + 3*t^2*(1-t)*P2 + t^3*P3; // For cubics: // p = (1-t)^2 * P0 + 2*(1-t)*t*P1 + t*t*P2; // Transcribed from here: explicit form and derivative // https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B%C3%A9zier_curves #include "J3ML/LinearAlgebra/Vector2.h" namespace J3ML::Algorithm { using namespace J3ML::LinearAlgebra; template inline T Square(T f) { return f * f; } template inline T Cube(T f) { return f * f * f; } template inline T Bezier(float t, const T& p0, const T& p1, const T& p2, const T& p3) { return Cube(1 - t) * p0 + 3 * Square(1 - t) * t * p1 + 3 * (1 - t) * Square(t) * p2 + Cube(t) * p3; } inline Vector2 Bezier(float t, const Vector2& p0, const Vector2& p1, const Vector2& p2, const Vector2& p3); // Tangent Vector2 BezierDerivative(float t, const Vector2& p0, const Vector2& p1, const Vector2& p2, const Vector2& p3); // Normal Vector2 BezierNormal(float t, const Vector2& p0, const Vector2& p1, const Vector2& p2, const Vector2& p3); }