| 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 |
|
| 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 |
|
| 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 = SkPathBuilder().arcTo(oval, startAngle, sweepAngle, false).detach(); |
| 46 |
|
| 47 |
SkPaint arcPaint(paint); |
| 48 |
arcPaint.setStrokeWidth(5); |
| 49 |
arcPaint.setColor(SkColorSetARGB(255, 0, 0, 255)); |
| 50 |
canvas->drawPath(arc, arcPaint); |
| 51 |
|
| 52 |
SkPaint pointPaint; |
| 53 |
pointPaint.setAntiAlias(true); |
| 54 |
pointPaint.setStrokeWidth(8); |
| 55 |
pointPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 56 |
pointPaint.setColor(SkColorSetARGB(255, 0, 255, 0)); |
| 57 |
|
| 58 |
float finalAngle = startAngle + sweepAngle; |
| 59 |
float middleAngle = startAngle + 0.5f * sweepAngle; |
| 60 |
float weight = cos(SkDegreesToRadians(sweepAngle) / 2); |
| 61 |
SkPoint p0 = {r * SkScalarCos(SkDegreesToRadians(startAngle)), |
| 62 |
r * SkScalarSin(SkDegreesToRadians(startAngle))}; |
| 63 |
float d = r / weight; |
| 64 |
SkPoint p1 = {d * SkScalarCos(SkDegreesToRadians(middleAngle)), |
| 65 |
d * SkScalarSin(SkDegreesToRadians(middleAngle))}; |
| 66 |
SkPoint p2 = {r * SkScalarCos(SkDegreesToRadians(finalAngle)), |
| 67 |
r * SkScalarSin(SkDegreesToRadians(finalAngle))}; |
| 68 |
p0 += center; |
| 69 |
p1 += center; |
| 70 |
p2 += center; |
| 71 |
|
| 72 |
canvas->drawLine(p0.x(), p0.y(), p1.x(), p1.y(), paint); |
| 73 |
canvas->drawLine(p1.x(), p1.y(), p2.x(), p2.y(), paint); |
| 74 |
|
| 75 |
const int N = 16; |
| 76 |
for (int i = 0; i <= N; ++i) { |
| 77 |
SkPoint p = conic(p0, p1, p2, weight, (float)i / N); |
| 78 |
canvas->drawPoint(p.x(), p.y(), pointPaint); |
| 79 |
} |
| 80 |
pointPaint.setColor(SkColorSetARGB(255, 255, 0, 0)); |
| 81 |
canvas->drawPoint(p0.x(), p0.y(), pointPaint); |
| 82 |
canvas->drawPoint(p1.x(), p1.y(), pointPaint); |
| 83 |
canvas->drawPoint(p2.x(), p2.y(), pointPaint); |
| 84 |
|
| 85 |
SkPathBuilder weightedQuadratic; |
| 86 |
weightedQuadratic.moveTo(p0); |
| 87 |
weightedQuadratic.conicTo(p1, p2, weight); |
| 88 |
paint.setColor(SK_ColorYELLOW); |
| 89 |
paint.setStrokeWidth(2.5); |
| 90 |
canvas->drawPath(weightedQuadratic.detach(), paint); |
| 91 |
} |
| 92 |
} // END FIDDLE |