blob: cdb8ed814eaa74675e1ad98bd72522752cff7cfb [file] [log] [blame]
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "include/core/SkCanvas.h"
8#include "include/core/SkPath.h"
9#include "include/utils/SkRandom.h"
10#include "samplecode/Sample.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000011
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040012class HairCurvesView : public Sample {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000013public:
14 HairCurvesView() {
15 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000016
bsalomon@google.comaeb21602011-08-30 18:13:44 +000017protected:
Hal Canary8a027312019-07-03 10:55:44 -040018 SkString name() override { return SkString("HairCurves"); }
bsalomon@google.comaeb21602011-08-30 18:13:44 +000019
Hal Canary8a027312019-07-03 10:55:44 -040020 void onDrawContent(SkCanvas* canvas) override {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000021 SkPaint paint;
22 paint.setAntiAlias(true);
23 paint.setStyle(SkPaint::kStroke_Style);
egdaniel@google.coma3088832013-07-17 19:30:41 +000024 paint.setStrokeWidth(0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000025 canvas->save();
26 canvas->scale(1000 * SK_Scalar1, 1000 * SK_Scalar1);
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000027 SkRandom rand;
28 SkRandom randW;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000029 SkPath curves;
30 SkPath hulls;
31 SkPath ctrlPts;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000032 for (int i = 0; i < 100; ++i) {
33 SkScalar pts[] = {
34 rand.nextUScalar1(), rand.nextUScalar1(),
35 rand.nextUScalar1(), rand.nextUScalar1(),
36 rand.nextUScalar1(), rand.nextUScalar1(),
37 rand.nextUScalar1(), rand.nextUScalar1()
38 };
39 curves.moveTo(pts[0], pts[1]);
40 curves.cubicTo(pts[2], pts[3],
41 pts[4], pts[5],
42 pts[6], pts[7]);
43
44 hulls.moveTo(pts[0], pts[1]);
45 hulls.lineTo(pts[2], pts[3]);
46 hulls.lineTo(pts[4], pts[5]);
47 hulls.lineTo(pts[6], pts[7]);
48
49 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
50 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
51 ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
52 ctrlPts.addCircle(pts[6], pts[7], SK_Scalar1 / 200);
53 }
54 for (int i = 0; i < 100; ++i) {
55 SkScalar pts[] = {
56 rand.nextUScalar1(), rand.nextUScalar1(),
57 rand.nextUScalar1(), rand.nextUScalar1(),
58 rand.nextUScalar1(), rand.nextUScalar1(),
59 };
60 curves.moveTo(pts[0], pts[1]);
61 curves.quadTo(pts[2], pts[3],
62 pts[4], pts[5]);
63
64 hulls.moveTo(pts[0], pts[1]);
65 hulls.lineTo(pts[2], pts[3]);
66 hulls.lineTo(pts[4], pts[5]);
67
68 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
69 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
70 ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
71 }
72 for (int i = 0; i < 100; ++i) {
73 SkScalar pts[] = {
74 rand.nextUScalar1(), rand.nextUScalar1(),
75 rand.nextUScalar1(), rand.nextUScalar1(),
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +000076 rand.nextUScalar1(), rand.nextUScalar1(),
77 };
78 SkScalar weight = randW.nextUScalar1() * 2.0f;
79
80 curves.moveTo(pts[0], pts[1]);
81 curves.conicTo(pts[2], pts[3],
82 pts[4], pts[5],
83 weight);
84
85 hulls.moveTo(pts[0], pts[1]);
86 hulls.lineTo(pts[2], pts[3]);
87 hulls.lineTo(pts[4], pts[5]);
88
89 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
90 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
91 ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
92 }
93 for (int i = 0; i < 100; ++i) {
94 SkScalar pts[] = {
95 rand.nextUScalar1(), rand.nextUScalar1(),
96 rand.nextUScalar1(), rand.nextUScalar1(),
bsalomon@google.comaeb21602011-08-30 18:13:44 +000097 };
98 curves.moveTo(pts[0], pts[1]);
99 curves.lineTo(pts[2], pts[3]);
100
101 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
102 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
103 }
104
105 paint.setColor(SK_ColorBLACK);
106 canvas->drawPath(curves, paint);
107 paint.setColor(SK_ColorRED);
108 //canvas->drawPath(hulls, paint);
109 paint.setStyle(SkPaint::kFill_Style);
110 paint.setColor(SK_ColorBLUE);
111 //canvas->drawPath(ctrlPts, paint);
112
113 canvas->restore();
114 }
115
116private:
John Stiles7571f9e2020-09-02 22:42:33 -0400117 using INHERITED = Sample;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000118};
119
120//////////////////////////////////////////////////////////////////////////////
121
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400122DEF_SAMPLE( return new HairCurvesView(); )