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_parametric, 512, 512, false, 0) { 5cb93a386Sopenharmony_ciSkPoint conic(SkPoint p0, SkPoint p1, SkPoint p2, float w, float t) { 6cb93a386Sopenharmony_ci float s = 1 - t; 7cb93a386Sopenharmony_ci return {((s * s * p0.x()) + (2 * s * t * w * p1.x()) + (t * t * p2.x())) / 8cb93a386Sopenharmony_ci ((s * s) + (w * 2 * s * t) + (t * t)), 9cb93a386Sopenharmony_ci ((s * s * p0.y()) + (2 * s * t * w * p1.y()) + (t * t * p2.y())) / 10cb93a386Sopenharmony_ci ((s * s) + (w * 2 * s * t) + (t * t))}; 11cb93a386Sopenharmony_ci} 12cb93a386Sopenharmony_ci 13cb93a386Sopenharmony_civoid draw(SkCanvas* canvas) { 14cb93a386Sopenharmony_ci canvas->clear(SkColorSetARGB(255, 255, 255, 255)); 15cb93a386Sopenharmony_ci 16cb93a386Sopenharmony_ci SkPaint paint; 17cb93a386Sopenharmony_ci paint.setAntiAlias(true); 18cb93a386Sopenharmony_ci paint.setStyle(SkPaint::kStroke_Style); 19cb93a386Sopenharmony_ci paint.setStrokeWidth(2.5); 20cb93a386Sopenharmony_ci 21cb93a386Sopenharmony_ci SkPoint center = {256, 256}; 22cb93a386Sopenharmony_ci float r = 192; 23cb93a386Sopenharmony_ci SkRect oval = {center.x() - r, center.y() - r, center.x() + r, center.y() + r}; 24cb93a386Sopenharmony_ci canvas->drawOval(oval, paint); 25cb93a386Sopenharmony_ci float startAngle = 0; 26cb93a386Sopenharmony_ci float sweepAngle = 179; 27cb93a386Sopenharmony_ci 28cb93a386Sopenharmony_ci SkPath arc; 29cb93a386Sopenharmony_ci arc.arcTo(oval, startAngle, sweepAngle, false); 30cb93a386Sopenharmony_ci 31cb93a386Sopenharmony_ci paint.setStrokeWidth(5); 32cb93a386Sopenharmony_ci paint.setColor(SkColorSetARGB(255, 0, 0, 255)); 33cb93a386Sopenharmony_ci canvas->drawPath(arc, paint); 34cb93a386Sopenharmony_ci 35cb93a386Sopenharmony_ci SkPaint pointPaint; 36cb93a386Sopenharmony_ci pointPaint.setAntiAlias(true); 37cb93a386Sopenharmony_ci pointPaint.setStrokeWidth(8); 38cb93a386Sopenharmony_ci pointPaint.setStrokeCap(SkPaint::kRound_Cap); 39cb93a386Sopenharmony_ci pointPaint.setColor(SkColorSetARGB(255, 0, 255, 0)); 40cb93a386Sopenharmony_ci 41cb93a386Sopenharmony_ci float finalAngle = startAngle + sweepAngle; 42cb93a386Sopenharmony_ci float middleAngle = startAngle + 0.5f * sweepAngle; 43cb93a386Sopenharmony_ci float weight = cos(SkDegreesToRadians(sweepAngle) / 2); 44cb93a386Sopenharmony_ci SkPoint p0 = {r * SkScalarCos(SkDegreesToRadians(startAngle)), 45cb93a386Sopenharmony_ci r * SkScalarSin(SkDegreesToRadians(startAngle))}; 46cb93a386Sopenharmony_ci float d = r / weight; 47cb93a386Sopenharmony_ci SkPoint p1 = {d * SkScalarCos(SkDegreesToRadians(middleAngle)), 48cb93a386Sopenharmony_ci d * SkScalarSin(SkDegreesToRadians(middleAngle))}; 49cb93a386Sopenharmony_ci SkPoint p2 = {r * SkScalarCos(SkDegreesToRadians(finalAngle)), 50cb93a386Sopenharmony_ci r * SkScalarSin(SkDegreesToRadians(finalAngle))}; 51cb93a386Sopenharmony_ci p0 += center; 52cb93a386Sopenharmony_ci p1 += center; 53cb93a386Sopenharmony_ci p2 += center; 54cb93a386Sopenharmony_ci 55cb93a386Sopenharmony_ci const int N = 8; 56cb93a386Sopenharmony_ci for (int i = 0; i <= N; ++i) { 57cb93a386Sopenharmony_ci SkPoint p = conic(p0, p1, p2, weight, (float)i / N); 58cb93a386Sopenharmony_ci canvas->drawPoint(p.x(), p.y(), pointPaint); 59cb93a386Sopenharmony_ci } 60cb93a386Sopenharmony_ci pointPaint.setColor(SkColorSetARGB(255, 255, 0, 0)); 61cb93a386Sopenharmony_ci canvas->drawPoint(p0.x(), p0.y(), pointPaint); 62cb93a386Sopenharmony_ci canvas->drawPoint(p1.x(), p1.y(), pointPaint); 63cb93a386Sopenharmony_ci canvas->drawPoint(p2.x(), p2.y(), pointPaint); 64cb93a386Sopenharmony_ci 65cb93a386Sopenharmony_ci SkPath weightedQuadratic; 66cb93a386Sopenharmony_ci weightedQuadratic.moveTo(p0); 67cb93a386Sopenharmony_ci weightedQuadratic.conicTo(p1, p2, weight); 68cb93a386Sopenharmony_ci paint.setColor(SK_ColorCYAN); 69cb93a386Sopenharmony_ci paint.setStrokeWidth(1); 70cb93a386Sopenharmony_ci canvas->drawPath(weightedQuadratic, paint); 71cb93a386Sopenharmony_ci} 72cb93a386Sopenharmony_ci} // END FIDDLE 73