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