Flutter Engine
The Flutter Engine
SkPath_arcto_conic_parametric.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_parametric, 512, 512, false, 0) {
5SkPoint 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
13void draw(SkCanvas* canvas) {
14 canvas->clear(SkColorSetARGB(255, 255, 255, 255));
15
17 paint.setAntiAlias(true);
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);
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);
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
constexpr SkColor SK_ColorCYAN
Definition: SkColor.h:143
static constexpr SkColor SkColorSetARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
Definition: SkColor.h:49
REG_FIDDLE(SkPath_arcto_conic_parametric, 512, 512, false, 0)
#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)
Definition: SkCanvas.cpp:1698
void drawPoint(SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2695
void clear(SkColor color)
Definition: SkCanvas.h:1199
void drawPath(const SkPath &path, const SkPaint &paint)
Definition: SkCanvas.cpp:1747
@ 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
Definition: SkPath.h:59
SkPath & arcTo(const SkRect &oval, SkScalar startAngle, SkScalar sweepAngle, bool forceMoveTo)
Definition: SkPath.cpp:1220
SkPath & moveTo(SkScalar x, SkScalar y)
Definition: SkPath.cpp:688
SkPath & conicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar w)
Definition: SkPath.cpp:766
const Paint & paint
Definition: color_source.cc:38
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
struct MyStruct s
SkRect oval
Definition: SkRecords.h:249
SkScalar startAngle
Definition: SkRecords.h:250
SkScalar sweepAngle
Definition: SkRecords.h:251
AI float conic(float tolerance, const SkPoint pts[], float w, const VectorXform &vectorXform=VectorXform())
Definition: WangsFormula.h:287
SkScalar w
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181