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_quadTo_example_parametric, 512, 512, false, 0) {
5cb93a386Sopenharmony_ciSkPoint quad(SkPoint p0, SkPoint p1, SkPoint p2, float t) {
6cb93a386Sopenharmony_ci    float s = 1 - t;
7cb93a386Sopenharmony_ci    return {(s * s * p0.x()) + (2 * s * t * p1.x()) + (t * t * p2.x()),
8cb93a386Sopenharmony_ci            (s * s * p0.y()) + (2 * s * t * p1.y()) + (t * t * p2.y())};
9cb93a386Sopenharmony_ci}
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci/*
12cb93a386Sopenharmony_ci
13cb93a386Sopenharmony_ci        If the starting point is (x0, y0), then this curve is defined as the
14cb93a386Sopenharmony_ci        paramentric curve as `t` goes from 0 to 1:
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ci          s := 1 - t
17cb93a386Sopenharmony_ci          x := (s * s * x0) + (2 * s * t * x1) + (t * t * x2)
18cb93a386Sopenharmony_ci          y := (s * s * y0) + (2 * s * t * y1) + (t * t * y2)
19cb93a386Sopenharmony_ci
20cb93a386Sopenharmony_ci*/
21cb93a386Sopenharmony_ci
22cb93a386Sopenharmony_civoid draw(SkCanvas* canvas) {
23cb93a386Sopenharmony_ci    canvas->clear(SkColorSetARGB(255, 255, 255, 255));
24cb93a386Sopenharmony_ci
25cb93a386Sopenharmony_ci    SkPaint paint;
26cb93a386Sopenharmony_ci    paint.setAntiAlias(true);
27cb93a386Sopenharmony_ci    paint.setStyle(SkPaint::kStroke_Style);
28cb93a386Sopenharmony_ci    paint.setStrokeWidth(5);
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ci    SkPoint a{100, 100};
31cb93a386Sopenharmony_ci    SkPoint b{200, 400};
32cb93a386Sopenharmony_ci    SkPoint c{300, 100};
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci    SkPath twoSegments;
35cb93a386Sopenharmony_ci    twoSegments.moveTo(a);
36cb93a386Sopenharmony_ci    twoSegments.lineTo(b);
37cb93a386Sopenharmony_ci    twoSegments.lineTo(c);
38cb93a386Sopenharmony_ci
39cb93a386Sopenharmony_ci    canvas->drawPath(twoSegments, paint);
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci    paint.setColor(SkColorSetARGB(255, 0, 0, 255));
42cb93a386Sopenharmony_ci    SkPath quadraticCurve;
43cb93a386Sopenharmony_ci    quadraticCurve.moveTo(a);
44cb93a386Sopenharmony_ci    quadraticCurve.quadTo(b, c);
45cb93a386Sopenharmony_ci    canvas->drawPath(quadraticCurve, paint);
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ci    SkFont font(nullptr, 32);
48cb93a386Sopenharmony_ci    SkPaint textPaint;
49cb93a386Sopenharmony_ci    textPaint.setAntiAlias(true);
50cb93a386Sopenharmony_ci    canvas->drawString("a", a.x(), a.y(), font, textPaint);
51cb93a386Sopenharmony_ci    canvas->drawString("b", b.x() + 20, b.y() + 20, font, textPaint);
52cb93a386Sopenharmony_ci    canvas->drawString("c", c.x(), c.y(), font, textPaint);
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci    SkPaint pointPaint;
55cb93a386Sopenharmony_ci    pointPaint.setAntiAlias(true);
56cb93a386Sopenharmony_ci    pointPaint.setStrokeWidth(8);
57cb93a386Sopenharmony_ci    pointPaint.setStrokeCap(SkPaint::kRound_Cap);
58cb93a386Sopenharmony_ci    pointPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
59cb93a386Sopenharmony_ci    int N = 15;
60cb93a386Sopenharmony_ci    for (int i = 0; i <= N; ++i) {
61cb93a386Sopenharmony_ci        SkPoint p = quad(a, b, c, (float)i / N);
62cb93a386Sopenharmony_ci        canvas->drawPoint(p.x(), p.y(), pointPaint);
63cb93a386Sopenharmony_ci    }
64cb93a386Sopenharmony_ci}
65cb93a386Sopenharmony_ci}  // END FIDDLE
66