blob: 2957527d4ca84cfc1adadc12c5ffe2d93aeee6f3 [file] [log] [blame]
Hal Canary6c8422c2020-01-10 15:22:09 -05001// Copyright 2020 Google LLC.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3#include "tools/fiddle/examples.h"
4REG_FIDDLE(SkPath_cubicTo_example, 512, 512, false, 0) {
5/*
6 If the starting point is (x0, y0), then this curve is defined as the
7 paramentric curve as `t` goes from 0 to 1:
8 s := 1 - t
9 x := (s * s * s * x0) +
10 (3 * s * s * t * x1) +
11 (3 * s * t * t * x2) +
12 (t * t * t * x3)
13 y := (s * s * s * y0) +
14 (3 * s * s * t * y1) +
15 (3 * s * t * t * y2) +
16 (t * t * t * y3)
17
18*/
19void draw(SkCanvas* canvas) {
20 canvas->clear(SkColorSetARGB(255, 255, 255, 255));
21 SkFont font(nullptr, 32);
22
23 SkPaint paint;
24 paint.setAntiAlias(true);
25 paint.setStyle(SkPaint::kStroke_Style);
26 paint.setStrokeWidth(5);
27
28 SkPoint a{128, 64};
29 SkPoint b{448, 448};
30 SkPoint c{64, 448};
31 SkPoint d{384, 64};
32
33 SkPath threeSegments;
34 threeSegments.moveTo(a);
35 threeSegments.lineTo(b);
36 threeSegments.lineTo(c);
37 threeSegments.lineTo(d);
38
39 canvas->drawPath(threeSegments, paint);
40
41 paint.setColor(SkColorSetARGB(255, 0, 0, 255));
42 SkPath cubicCurve;
43 cubicCurve.moveTo(a);
44 cubicCurve.cubicTo(b, c, d);
45 canvas->drawPath(cubicCurve, paint);
46
47 SkPaint textPaint;
48 textPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
49 textPaint.setAntiAlias(true);
50 canvas->drawString("a", a.x(), a.y(), font, textPaint);
51 canvas->drawString("b", b.x(), b.y(), font, textPaint);
52 canvas->drawString("c", c.x() - 20, c.y(), font, textPaint);
53 canvas->drawString("d", d.x(), d.y(), font, textPaint);
54}
55} // END FIDDLE