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