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(SkPath_arcto_conic_parametric2, 512, 512, false, 0) { 5cb93a386Sopenharmony_ci/** Add a weighted quadratic bezier from the last point, approaching control point 6cb93a386Sopenharmony_ci (x1,y1), and ending at (x2,y2). If no moveTo() call has been made for 7cb93a386Sopenharmony_ci this contour, the first point is automatically set to (0,0). 8cb93a386Sopenharmony_ci If the starting point is (x0, y0), then this curve is defined as the 9cb93a386Sopenharmony_ci paramentric curve as `t` goes from 0 to 1: 10cb93a386Sopenharmony_ci s := 1 - t 11cb93a386Sopenharmony_ci x := ((s * s * x0) + (w * 2 * s * t * x1) + (t * t * x2)) / 12cb93a386Sopenharmony_ci ((s * s) + (w * 2 * s * t) + (t * t)) 13cb93a386Sopenharmony_ci y := ((s * s * y0) + (w * 2 * s * t * y1) + (t * t * y2)) / 14cb93a386Sopenharmony_ci ((s * s) + (w * 2 * s * t) + (t * t)) 15cb93a386Sopenharmony_ci @param x1 The x-coordinate of the control point on a quadratic curve 16cb93a386Sopenharmony_ci @param y1 The y-coordinate of the control point on a quadratic curve 17cb93a386Sopenharmony_ci @param x2 The x-coordinate of the end point on a quadratic curve 18cb93a386Sopenharmony_ci @param y2 The y-coordinate of the end point on a quadratic curve 19cb93a386Sopenharmony_ci @param w The weight of the control point (x1,y1) 20cb93a386Sopenharmony_ci*/ 21cb93a386Sopenharmony_ci 22cb93a386Sopenharmony_ciSkPoint conic(SkPoint p0, SkPoint p1, SkPoint p2, float w, float t) { 23cb93a386Sopenharmony_ci float s = 1 - t; 24cb93a386Sopenharmony_ci return {((s * s * p0.x()) + (2 * s * t * w * p1.x()) + (t * t * p2.x())) / 25cb93a386Sopenharmony_ci ((s * s) + (w * 2 * s * t) + (t * t)), 26cb93a386Sopenharmony_ci ((s * s * p0.y()) + (2 * s * t * w * p1.y()) + (t * t * p2.y())) / 27cb93a386Sopenharmony_ci ((s * s) + (w * 2 * s * t) + (t * t))}; 28cb93a386Sopenharmony_ci} 29cb93a386Sopenharmony_ci 30cb93a386Sopenharmony_civoid draw(SkCanvas* canvas) { 31cb93a386Sopenharmony_ci canvas->clear(SkColorSetARGB(255, 255, 255, 255)); 32cb93a386Sopenharmony_ci 33cb93a386Sopenharmony_ci SkPaint paint; 34cb93a386Sopenharmony_ci paint.setAntiAlias(true); 35cb93a386Sopenharmony_ci paint.setStyle(SkPaint::kStroke_Style); 36cb93a386Sopenharmony_ci paint.setStrokeWidth(1); 37cb93a386Sopenharmony_ci 38cb93a386Sopenharmony_ci SkPoint center = {256, 256}; 39cb93a386Sopenharmony_ci float r = 192; 40cb93a386Sopenharmony_ci SkRect oval = {center.x() - r, center.y() - r, center.x() + r, center.y() + r}; 41cb93a386Sopenharmony_ci canvas->drawOval(oval, paint); 42cb93a386Sopenharmony_ci float startAngle = 15; 43cb93a386Sopenharmony_ci float sweepAngle = 75; 44cb93a386Sopenharmony_ci 45cb93a386Sopenharmony_ci SkPath arc; 46cb93a386Sopenharmony_ci arc.arcTo(oval, startAngle, sweepAngle, false); 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ci SkPaint arcPaint(paint); 49cb93a386Sopenharmony_ci arcPaint.setStrokeWidth(5); 50cb93a386Sopenharmony_ci arcPaint.setColor(SkColorSetARGB(255, 0, 0, 255)); 51cb93a386Sopenharmony_ci canvas->drawPath(arc, arcPaint); 52cb93a386Sopenharmony_ci 53cb93a386Sopenharmony_ci SkPaint pointPaint; 54cb93a386Sopenharmony_ci pointPaint.setAntiAlias(true); 55cb93a386Sopenharmony_ci pointPaint.setStrokeWidth(8); 56cb93a386Sopenharmony_ci pointPaint.setStrokeCap(SkPaint::kRound_Cap); 57cb93a386Sopenharmony_ci pointPaint.setColor(SkColorSetARGB(255, 0, 255, 0)); 58cb93a386Sopenharmony_ci 59cb93a386Sopenharmony_ci float finalAngle = startAngle + sweepAngle; 60cb93a386Sopenharmony_ci float middleAngle = startAngle + 0.5f * sweepAngle; 61cb93a386Sopenharmony_ci float weight = cos(SkDegreesToRadians(sweepAngle) / 2); 62cb93a386Sopenharmony_ci SkPoint p0 = {r * SkScalarCos(SkDegreesToRadians(startAngle)), 63cb93a386Sopenharmony_ci r * SkScalarSin(SkDegreesToRadians(startAngle))}; 64cb93a386Sopenharmony_ci float d = r / weight; 65cb93a386Sopenharmony_ci SkPoint p1 = {d * SkScalarCos(SkDegreesToRadians(middleAngle)), 66cb93a386Sopenharmony_ci d * SkScalarSin(SkDegreesToRadians(middleAngle))}; 67cb93a386Sopenharmony_ci SkPoint p2 = {r * SkScalarCos(SkDegreesToRadians(finalAngle)), 68cb93a386Sopenharmony_ci r * SkScalarSin(SkDegreesToRadians(finalAngle))}; 69cb93a386Sopenharmony_ci p0 += center; 70cb93a386Sopenharmony_ci p1 += center; 71cb93a386Sopenharmony_ci p2 += center; 72cb93a386Sopenharmony_ci 73cb93a386Sopenharmony_ci canvas->drawLine(p0.x(), p0.y(), p1.x(), p1.y(), paint); 74cb93a386Sopenharmony_ci canvas->drawLine(p1.x(), p1.y(), p2.x(), p2.y(), paint); 75cb93a386Sopenharmony_ci 76cb93a386Sopenharmony_ci const int N = 16; 77cb93a386Sopenharmony_ci for (int i = 0; i <= N; ++i) { 78cb93a386Sopenharmony_ci SkPoint p = conic(p0, p1, p2, weight, (float)i / N); 79cb93a386Sopenharmony_ci canvas->drawPoint(p.x(), p.y(), pointPaint); 80cb93a386Sopenharmony_ci } 81cb93a386Sopenharmony_ci pointPaint.setColor(SkColorSetARGB(255, 255, 0, 0)); 82cb93a386Sopenharmony_ci canvas->drawPoint(p0.x(), p0.y(), pointPaint); 83cb93a386Sopenharmony_ci canvas->drawPoint(p1.x(), p1.y(), pointPaint); 84cb93a386Sopenharmony_ci canvas->drawPoint(p2.x(), p2.y(), pointPaint); 85cb93a386Sopenharmony_ci 86cb93a386Sopenharmony_ci SkPath weightedQuadratic; 87cb93a386Sopenharmony_ci weightedQuadratic.moveTo(p0); 88cb93a386Sopenharmony_ci weightedQuadratic.conicTo(p1, p2, weight); 89cb93a386Sopenharmony_ci paint.setColor(SK_ColorYELLOW); 90cb93a386Sopenharmony_ci paint.setStrokeWidth(2.5); 91cb93a386Sopenharmony_ci canvas->drawPath(weightedQuadratic, paint); 92cb93a386Sopenharmony_ci} 93cb93a386Sopenharmony_ci} // END FIDDLE 94