Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkPath_arcto_conic_parametric2.cpp
Go to the documentation of this file.
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.
4REG_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
22SkPoint 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
30void draw(SkCanvas* canvas) {
31 canvas->clear(SkColorSetARGB(255, 255, 255, 255));
32
34 paint.setAntiAlias(true);
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);
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
constexpr SkColor SK_ColorYELLOW
Definition SkColor.h:139
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition SkColor.h:49
#define SkDegreesToRadians(degrees)
Definition SkScalar.h:77
#define SkScalarSin(radians)
Definition SkScalar.h:45
#define SkScalarCos(radians)
Definition SkScalar.h:46
static SkScalar center(float pos0, float pos1)
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition aaclip.cpp:27
#define N
Definition beziers.cpp:19
void drawOval(const SkRect &oval, const SkPaint &paint)
void drawPoint(SkScalar x, SkScalar y, const SkPaint &paint)
void drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint &paint)
void clear(SkColor color)
Definition SkCanvas.h:1199
void drawPath(const SkPath &path, const SkPaint &paint)
@ kRound_Cap
adds circle
Definition SkPaint.h:335
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition SkPaint.h:170
void setStrokeCap(Cap cap)
Definition SkPaint.cpp:179
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
SkPath & arcTo(const SkRect &oval, SkScalar startAngle, SkScalar sweepAngle, bool forceMoveTo)
Definition SkPath.cpp:1156
SkPath & moveTo(SkScalar x, SkScalar y)
Definition SkPath.cpp:678
SkPath & conicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar w)
Definition SkPath.cpp:756
const Paint & paint
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition main.cc:19
#define REG_FIDDLE(NAME, W, H, TEXT, I)
Definition examples.h:60
struct MyStruct s
SkScalar w
constexpr float y() const
constexpr float x() const
constexpr float x() const
Definition SkRect.h:720