From 18bb0afcfe5399abbfb84b9ec2f201e637172a1d Mon Sep 17 00:00:00 2001 From: subhransu mohanty Date: Thu, 15 Nov 2018 11:42:10 +0900 Subject: [PATCH] lottie/vector: added angleAt() api to bezier class. Change-Id: Ie5a3f68ad849d1a283363e1eae50ce9520bddb7a --- src/vector/vbezier.cpp | 25 +++++++++++++++++++++++++ src/vector/vbezier.h | 1 + 2 files changed, 26 insertions(+) diff --git a/src/vector/vbezier.cpp b/src/vector/vbezier.cpp index d33d235..de52839 100644 --- a/src/vector/vbezier.cpp +++ b/src/vector/vbezier.cpp @@ -93,4 +93,29 @@ void VBezier::splitAtLength(float len, VBezier *left, VBezier *right) right->parameterSplitLeft(t, left); } +VPointF VBezier::derivative(float t) const +{ + // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 * t^2) * p2 + t^2 * p3) + + float m_t = 1. - t; + + float d = t * t; + float a = -m_t * m_t; + float b = 1 - 4 * t + 3 * d; + float c = 2 * t - 3 * d; + + return 3 * VPointF(a * x1 + b * x2 + c * x3 + d * x4, + a * y1 + b * y2 + c * y3 + d * y4); +} + + +float VBezier::angleAt(float t) const +{ + if (t < 0 || t > 1) { + return 0; + } + return VLine({}, derivative(t)).angle(); +} + + V_END_NAMESPACE diff --git a/src/vector/vbezier.h b/src/vector/vbezier.h index f41e532..12e9923 100644 --- a/src/vector/vbezier.h +++ b/src/vector/vbezier.h @@ -9,6 +9,7 @@ class VBezier { public: VBezier() = default; VPointF pointAt(float t) const; + float angleAt(float t) const; VBezier onInterval(float t0, float t1) const; float length() const; static void coefficients(float t, float &a, float &b, float &c, float &d); -- 2.7.4