lottie/vector: added angleAt() api to bezier class. 24/193124/1
authorsubhransu mohanty <sub.mohanty@samsung.com>
Thu, 15 Nov 2018 02:42:10 +0000 (11:42 +0900)
committersubhransu mohanty <sub.mohanty@samsung.com>
Thu, 15 Nov 2018 02:42:10 +0000 (11:42 +0900)
Change-Id: Ie5a3f68ad849d1a283363e1eae50ce9520bddb7a

src/vector/vbezier.cpp
src/vector/vbezier.h

index d33d235..de52839 100644 (file)
@@ -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
index f41e532..12e9923 100644 (file)
@@ -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);