Files
j3ml/include/J3ML/Algorithm/Bezier.h
scientiist 26cd349417
Some checks failed
Run tests / Explore-Gitea-Actions (push) Failing after 25s
Build Docs With Doxygen / Explore-Gitea-Actions (push) Successful in 27s
Implement 2D Quadratic and Cubic Bezier Curve algorithm
2024-06-20 11:09:38 -05:00

45 lines
1.4 KiB
C++

/// @file Bezier.h
/// @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 <typename T>
inline T Square(T f) { return f * f; }
template <typename T>
inline T Cube(T f) { return f * f * f; }
template <typename T>
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);
}