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(SkPath_cubicTo_example_parametric, 512, 512, false, 0) {
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    float s = 1 - t;
22cb93a386Sopenharmony_ci    return {(s * s * s * p0.x()) + (3 * s * s * t * p1.x()) + (3 * s * t * t * p2.x()) +
23cb93a386Sopenharmony_ci                    (t * t * t * p3.x()),
24cb93a386Sopenharmony_ci            (s * s * s * p0.y()) + (3 * s * s * t * p1.y()) + (3 * s * t * t * p2.y()) +
25cb93a386Sopenharmony_ci                    (t * t * t * p3.y())};
26cb93a386Sopenharmony_ci}
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_civoid draw(SkCanvas* canvas) {
29cb93a386Sopenharmony_ci    canvas->clear(SkColorSetARGB(255, 255, 255, 255));
30cb93a386Sopenharmony_ci    SkFont font(nullptr, 32);
31cb93a386Sopenharmony_ci
32cb93a386Sopenharmony_ci    SkPaint paint;
33cb93a386Sopenharmony_ci    paint.setAntiAlias(true);
34cb93a386Sopenharmony_ci    paint.setStyle(SkPaint::kStroke_Style);
35cb93a386Sopenharmony_ci    paint.setStrokeWidth(5);
36cb93a386Sopenharmony_ci
37cb93a386Sopenharmony_ci    SkPoint a{136, 64};
38cb93a386Sopenharmony_ci    SkPoint b{448, 448};
39cb93a386Sopenharmony_ci    SkPoint c{64, 448};
40cb93a386Sopenharmony_ci    SkPoint d{376, 64};
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ci    SkPath threeSegments;
43cb93a386Sopenharmony_ci    threeSegments.moveTo(a);
44cb93a386Sopenharmony_ci    threeSegments.lineTo(b);
45cb93a386Sopenharmony_ci    threeSegments.lineTo(c);
46cb93a386Sopenharmony_ci    threeSegments.lineTo(d);
47cb93a386Sopenharmony_ci
48cb93a386Sopenharmony_ci    canvas->drawPath(threeSegments, paint);
49cb93a386Sopenharmony_ci
50cb93a386Sopenharmony_ci    paint.setColor(SkColorSetARGB(255, 0, 0, 255));
51cb93a386Sopenharmony_ci    SkPath cubicCurve;
52cb93a386Sopenharmony_ci    cubicCurve.moveTo(a);
53cb93a386Sopenharmony_ci    cubicCurve.cubicTo(b, c, d);
54cb93a386Sopenharmony_ci    canvas->drawPath(cubicCurve, paint);
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_ci    SkPaint textPaint;
57cb93a386Sopenharmony_ci    textPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
58cb93a386Sopenharmony_ci    textPaint.setAntiAlias(true);
59cb93a386Sopenharmony_ci    canvas->drawString("a", a.x(), a.y(), font, textPaint);
60cb93a386Sopenharmony_ci    canvas->drawString("b", b.x(), b.y(), font, textPaint);
61cb93a386Sopenharmony_ci    canvas->drawString("c", c.x() - 20, c.y(), font, textPaint);
62cb93a386Sopenharmony_ci    canvas->drawString("d", d.x(), d.y(), font, textPaint);
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_ci    SkPaint pointPaint;
65cb93a386Sopenharmony_ci    pointPaint.setAntiAlias(true);
66cb93a386Sopenharmony_ci    pointPaint.setStrokeWidth(8);
67cb93a386Sopenharmony_ci    pointPaint.setStrokeCap(SkPaint::kRound_Cap);
68cb93a386Sopenharmony_ci    pointPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
69cb93a386Sopenharmony_ci    const int N = 16;
70cb93a386Sopenharmony_ci    for (int i = 0; i <= N; ++i) {
71cb93a386Sopenharmony_ci        SkPoint p = cubic(a, b, c, d, (float)i / N);
72cb93a386Sopenharmony_ci        canvas->drawPoint(p.x(), p.y(), pointPaint);
73cb93a386Sopenharmony_ci    }
74cb93a386Sopenharmony_ci    pointPaint.setColor(SkColorSetARGB(255, 255, 0, 0));
75cb93a386Sopenharmony_ci}
76cb93a386Sopenharmony_ci}  // END FIDDLE
77