Flutter Engine
The Flutter Engine
Functions
SkPath_cubicTo_example_parametric_animated.cpp File Reference
#include "tools/fiddle/examples.h"

Go to the source code of this file.

Functions

 REG_FIDDLE_ANIMATED (SkPath_cubicTo_example_parametric_animated, 512, 512, false, 0, 3)
 

Function Documentation

◆ REG_FIDDLE_ANIMATED()

REG_FIDDLE_ANIMATED ( SkPath_cubicTo_example_parametric_animated  ,
512  ,
512  ,
false  ,
,
 
)

Definition at line 4 of file SkPath_cubicTo_example_parametric_animated.cpp.

4 {
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 // a simple mathematical definition of cubic curve.
22 // There are faster ways to calculate this.
23 float s = 1 - t;
24 return {(s * s * s * p0.x()) +
25 (3 * s * s * t * p1.x()) +
26 (3 * s * t * t * p2.x()) +
27 (t * t * t * p3.x()),
28 (s * s * s * p0.y()) +
29 (3 * s * s * t * p1.y()) +
30 (3 * s * t * t * p2.y()) +
31 (t * t * t * p3.y())};
32}
33
34static SkPoint interpolate(SkPoint a, SkPoint b, float t) {
35 return {SkScalarInterp(a.x(), b.x(), t),
36 SkScalarInterp(a.y(), b.y(), t)};
37}
38
39void draw(SkCanvas* canvas) {
40 canvas->clear(SkColorSetARGB(255,255,255,255));
41
42 SkPoint a{136, 64};
43 SkPoint b{448, 448};
44 SkPoint c{64, 448};
45 SkPoint d{376, 64};
46
48 SkPoint bc = interpolate(b, c, frame);
49 SkPoint cd = interpolate(c, d, frame);
50 SkPoint abc = interpolate(ab, bc, frame);
51 SkPoint bcd = interpolate(bc, cd, frame);
52
54 paint.setAntiAlias(true);
56 paint.setStrokeWidth(1);
57
58 canvas->drawLine(ab, bc, paint);
59 canvas->drawLine(bc, cd, paint);
60 canvas->drawLine(abc, bcd, paint);
61
62 paint.setStrokeWidth(3);
63 canvas->drawLine(a, b, paint);
64 canvas->drawLine(b, c, paint);
65 canvas->drawLine(c, d, paint);
66
67 paint.setStrokeWidth(5);
68 paint.setColor(SkColorSetARGB(255, 0, 0, 255));
69 SkPath cubicCurve;
70 cubicCurve.moveTo(a);
71 cubicCurve.cubicTo(b, c, d);
72 canvas->drawPath(cubicCurve, paint);
73
74 SkFont font(fontMgr->matchFamilyStyle(nullptr, {}), 32);
75 SkPaint textPaint;
76 textPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
77 textPaint.setAntiAlias(true);
78
79 sk_sp<SkTypeface> face(fontMgr->matchFamilyStyle("DejaVu Sans Mono", SkFontStyle()));
80 canvas->drawString("a", a.x(), a.y(), font, textPaint);
81 canvas->drawString("b", b.x(), b.y(), font, textPaint);
82 canvas->drawString("c", c.x()-20, c.y(), font, textPaint);
83 canvas->drawString("d", d.x(), d.y(), font, textPaint);
84 SkString msg = SkStringPrintf("%.4f", frame);
85 textPaint.setColor(SkColorSetARGB(255, 204, 204, 204));
86 canvas->drawString(msg.c_str(), 4, 36, font, textPaint);
87
88 SkPaint pointPaint;
89 pointPaint.setAntiAlias(true);
90 pointPaint.setStrokeWidth(8);
92
93 pointPaint.setColor(SkColorSetARGB(255, 255, 0, 0));
94 canvas->drawPoint(interpolate(abc, bcd, frame), pointPaint);
95
96 pointPaint.setColor(SkColorSetARGB(255, 0, 255, 0));
97 pointPaint.setStrokeWidth(7);
98 canvas->drawPoint(cubic(a, b, c, d, frame), pointPaint);
99}
100} // END FIDDLE
static double interpolate(double A, double B, double t)
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
static SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t)
Definition: SkScalar.h:131
SK_API SkString SkStringPrintf(const char *format,...) SK_PRINTF_LIKE(1
Creates a new string and writes into it using a printf()-style format.
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
void drawPoint(SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2695
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
Definition: SkCanvas.cpp:2700
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 & cubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar x3, SkScalar y3)
Definition: SkPath.cpp:799
const char * c_str() const
Definition: SkString.h:133
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
double frame
Definition: examples.cpp:31
static bool b
struct MyStruct s
struct MyStruct a[10]
Definition: ab.py:1
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