1cb93a386Sopenharmony_ci// Copyright 2020 Google LLC.
2cb93a386Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3cb93a386Sopenharmony_ci#include "tools/fiddle/examples.h"
4cb93a386Sopenharmony_ciREG_FIDDLE_ANIMATED(SkPath_cubicTo_example_parametric_animated, 512, 512, false, 0, 3) {
5cb93a386Sopenharmony_ci/*
6cb93a386Sopenharmony_ci        If the starting point is (x0, y0), then this curve is defined as the
7cb93a386Sopenharmony_ci        paramentric curve as `t` goes from 0 to 1:
8cb93a386Sopenharmony_ci          s := 1 - t
9cb93a386Sopenharmony_ci          x := (s * s * s * x0) +
10cb93a386Sopenharmony_ci               (3 * s * s * t * x1) +
11cb93a386Sopenharmony_ci               (3 * s * t * t * x2) +
12cb93a386Sopenharmony_ci               (t * t * t * x3)
13cb93a386Sopenharmony_ci          y := (s * s * s * y0) +
14cb93a386Sopenharmony_ci               (3 * s * s * t * y1) +
15cb93a386Sopenharmony_ci               (3 * s * t * t * y2) +
16cb93a386Sopenharmony_ci               (t * t * t * y3)
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ci*/
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_ciSkPoint cubic(SkPoint p0, SkPoint p1, SkPoint p2, SkPoint p3, float t) {
21cb93a386Sopenharmony_ci    // a simple mathematical definition of cubic curve.
22cb93a386Sopenharmony_ci    // There are faster ways to calculate this.
23cb93a386Sopenharmony_ci    float s = 1 - t;
24cb93a386Sopenharmony_ci    return {(s * s * s * p0.x()) +
25cb93a386Sopenharmony_ci            (3 * s * s * t * p1.x()) +
26cb93a386Sopenharmony_ci            (3 * s * t * t * p2.x()) +
27cb93a386Sopenharmony_ci            (t * t * t * p3.x()),
28cb93a386Sopenharmony_ci            (s * s * s * p0.y()) +
29cb93a386Sopenharmony_ci            (3 * s * s * t * p1.y()) +
30cb93a386Sopenharmony_ci            (3 * s * t * t * p2.y()) +
31cb93a386Sopenharmony_ci            (t * t * t * p3.y())};
32cb93a386Sopenharmony_ci}
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_cistatic SkPoint interpolate(SkPoint a, SkPoint b, float t) {
35cb93a386Sopenharmony_ci   return {SkScalarInterp(a.x(), b.x(), t),
36cb93a386Sopenharmony_ci           SkScalarInterp(a.y(), b.y(), t)};
37cb93a386Sopenharmony_ci}
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_civoid draw(SkCanvas* canvas) {
40cb93a386Sopenharmony_ci    canvas->clear(SkColorSetARGB(255,255,255,255));
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci    SkPoint a{136,  64};
43cb93a386Sopenharmony_ci    SkPoint b{448, 448};
44cb93a386Sopenharmony_ci    SkPoint c{64,  448};
45cb93a386Sopenharmony_ci    SkPoint d{376, 64};
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ci    SkPoint ab = interpolate(a, b, frame);
48cb93a386Sopenharmony_ci    SkPoint bc = interpolate(b, c, frame);
49cb93a386Sopenharmony_ci    SkPoint cd = interpolate(c, d, frame);
50cb93a386Sopenharmony_ci    SkPoint abc = interpolate(ab, bc, frame);
51cb93a386Sopenharmony_ci    SkPoint bcd = interpolate(bc, cd, frame);
52cb93a386Sopenharmony_ci
53cb93a386Sopenharmony_ci    SkPaint paint;
54cb93a386Sopenharmony_ci    paint.setAntiAlias(true);
55cb93a386Sopenharmony_ci    paint.setStyle(SkPaint::kStroke_Style);
56cb93a386Sopenharmony_ci    paint.setStrokeWidth(1);
57cb93a386Sopenharmony_ci
58cb93a386Sopenharmony_ci    canvas->drawLine(ab, bc, paint);
59cb93a386Sopenharmony_ci    canvas->drawLine(bc, cd, paint);
60cb93a386Sopenharmony_ci    canvas->drawLine(abc, bcd, paint);
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci    paint.setStrokeWidth(3);
63cb93a386Sopenharmony_ci    canvas->drawLine(a, b, paint);
64cb93a386Sopenharmony_ci    canvas->drawLine(b, c, paint);
65cb93a386Sopenharmony_ci    canvas->drawLine(c, d, paint);
66cb93a386Sopenharmony_ci
67cb93a386Sopenharmony_ci    paint.setStrokeWidth(5);
68cb93a386Sopenharmony_ci    paint.setColor(SkColorSetARGB(255, 0, 0, 255));
69cb93a386Sopenharmony_ci    SkPath cubicCurve;
70cb93a386Sopenharmony_ci    cubicCurve.moveTo(a);
71cb93a386Sopenharmony_ci    cubicCurve.cubicTo(b, c, d);
72cb93a386Sopenharmony_ci    canvas->drawPath(cubicCurve, paint);
73cb93a386Sopenharmony_ci
74cb93a386Sopenharmony_ci    SkFont font(nullptr, 32);
75cb93a386Sopenharmony_ci    SkPaint textPaint;
76cb93a386Sopenharmony_ci    textPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
77cb93a386Sopenharmony_ci    textPaint.setAntiAlias(true);
78cb93a386Sopenharmony_ci    sk_sp<SkFontMgr> mgr(SkFontMgr::RefDefault());
79cb93a386Sopenharmony_ci    sk_sp<SkTypeface> face(mgr->matchFamilyStyle("DejaVu Sans Mono", SkFontStyle()));
80cb93a386Sopenharmony_ci    canvas->drawString("a", a.x(),    a.y(), font, textPaint);
81cb93a386Sopenharmony_ci    canvas->drawString("b", b.x(),    b.y(), font, textPaint);
82cb93a386Sopenharmony_ci    canvas->drawString("c", c.x()-20, c.y(), font, textPaint);
83cb93a386Sopenharmony_ci    canvas->drawString("d", d.x(),    d.y(), font, textPaint);
84cb93a386Sopenharmony_ci    SkString msg = SkStringPrintf("%.4f", frame);
85cb93a386Sopenharmony_ci    textPaint.setColor(SkColorSetARGB(255, 204, 204, 204));
86cb93a386Sopenharmony_ci    canvas->drawString(msg.c_str(), 4, 36, font, textPaint);
87cb93a386Sopenharmony_ci
88cb93a386Sopenharmony_ci    SkPaint pointPaint;
89cb93a386Sopenharmony_ci    pointPaint.setAntiAlias(true);
90cb93a386Sopenharmony_ci    pointPaint.setStrokeWidth(8);
91cb93a386Sopenharmony_ci    pointPaint.setStrokeCap(SkPaint::kRound_Cap);
92cb93a386Sopenharmony_ci
93cb93a386Sopenharmony_ci    pointPaint.setColor(SkColorSetARGB(255, 255, 0, 0));
94cb93a386Sopenharmony_ci    canvas->drawPoint(interpolate(abc, bcd, frame), pointPaint);
95cb93a386Sopenharmony_ci
96cb93a386Sopenharmony_ci    pointPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
97cb93a386Sopenharmony_ci    pointPaint.setStrokeWidth(7);
98cb93a386Sopenharmony_ci    canvas->drawPoint(cubic(a, b, c, d, frame), pointPaint);
99cb93a386Sopenharmony_ci}
100cb93a386Sopenharmony_ci}  // END FIDDLE
101