Flutter Engine
The Flutter Engine
SkPath_cubicTo_example_parametric.cpp
Go to the documentation of this file.
1// 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.
4REG_FIDDLE(SkPath_cubicTo_example_parametric, 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*/
19
20SkPoint cubic(SkPoint p0, SkPoint p1, SkPoint p2, SkPoint p3, float t) {
21 float s = 1 - t;
22 return {(s * s * s * p0.x()) + (3 * s * s * t * p1.x()) + (3 * s * t * t * p2.x()) +
23 (t * t * t * p3.x()),
24 (s * s * s * p0.y()) + (3 * s * s * t * p1.y()) + (3 * s * t * t * p2.y()) +
25 (t * t * t * p3.y())};
26}
27
28void draw(SkCanvas* canvas) {
29 canvas->clear(SkColorSetARGB(255, 255, 255, 255));
30 SkFont font(fontMgr->matchFamilyStyle(nullptr, {}), 32);
31
33 paint.setAntiAlias(true);
35 paint.setStrokeWidth(5);
36
37 SkPoint a{136, 64};
38 SkPoint b{448, 448};
39 SkPoint c{64, 448};
40 SkPoint d{376, 64};
41
42 SkPath threeSegments;
43 threeSegments.moveTo(a);
44 threeSegments.lineTo(b);
45 threeSegments.lineTo(c);
46 threeSegments.lineTo(d);
47
48 canvas->drawPath(threeSegments, paint);
49
50 paint.setColor(SkColorSetARGB(255, 0, 0, 255));
51 SkPath cubicCurve;
52 cubicCurve.moveTo(a);
53 cubicCurve.cubicTo(b, c, d);
54 canvas->drawPath(cubicCurve, paint);
55
56 SkPaint textPaint;
57 textPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
58 textPaint.setAntiAlias(true);
59 canvas->drawString("a", a.x(), a.y(), font, textPaint);
60 canvas->drawString("b", b.x(), b.y(), font, textPaint);
61 canvas->drawString("c", c.x() - 20, c.y(), font, textPaint);
62 canvas->drawString("d", d.x(), d.y(), font, textPaint);
63
64 SkPaint pointPaint;
65 pointPaint.setAntiAlias(true);
66 pointPaint.setStrokeWidth(8);
68 pointPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
69 const int N = 16;
70 for (int i = 0; i <= N; ++i) {
71 SkPoint p = cubic(a, b, c, d, (float)i / N);
72 canvas->drawPoint(p.x(), p.y(), pointPaint);
73 }
74 pointPaint.setColor(SkColorSetARGB(255, 255, 0, 0));
75}
76} // END FIDDLE
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
REG_FIDDLE(SkPath_cubicTo_example_parametric, 512, 512, false, 0)
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
#define N
Definition: beziers.cpp:19
void drawPoint(SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2695
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
sk_sp< SkTypeface > matchFamilyStyle(const char familyName[], const SkFontStyle &) const
Definition: SkFontMgr.cpp:109
Definition: SkFont.h:35
@ kRound_Cap
adds circle
Definition: SkPaint.h:335
void setColor(SkColor color)
Definition: SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition: SkPaint.h:170
void setStrokeCap(Cap cap)
Definition: SkPaint.cpp:179
@ kStroke_Style
set to stroke geometry
Definition: SkPaint.h:194
void setStrokeWidth(SkScalar width)
Definition: SkPaint.cpp:159
Definition: SkPath.h:59
SkPath & moveTo(SkScalar x, SkScalar y)
Definition: SkPath.cpp:688
SkPath & lineTo(SkScalar x, SkScalar y)
Definition: SkPath.cpp:728
SkPath & cubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar x3, SkScalar y3)
Definition: SkPath.cpp:799
const Paint & paint
Definition: color_source.cc:38
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
sk_sp< SkFontMgr > fontMgr
Definition: examples.cpp:32
static bool b
struct MyStruct s
struct MyStruct a[10]
font
Font Metadata and Metrics.
AI float cubic(float precision, const SkPoint pts[], const VectorXform &vectorXform=VectorXform())
Definition: WangsFormula.h:195
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181