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