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