blob: a8658153f3d05948e6b6e61ddbf457dd58f8eddd [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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +00008#include "SkCanvas.h"
9#include "SkPath.h"
10#include "SkRandom.h"
11
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:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040018 virtual bool onQuery(Sample::Event* evt) {
19 if (Sample::TitleQ(*evt)) {
20 Sample::TitleR(evt, "HairCurves");
bsalomon@google.comaeb21602011-08-30 18:13:44 +000021 return true;
22 }
23 return this->INHERITED::onQuery(evt);
24 }
25
26
27 virtual void onDrawContent(SkCanvas* canvas) {
28 SkPaint paint;
29 paint.setAntiAlias(true);
30 paint.setStyle(SkPaint::kStroke_Style);
egdaniel@google.coma3088832013-07-17 19:30:41 +000031 paint.setStrokeWidth(0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000032 canvas->save();
33 canvas->scale(1000 * SK_Scalar1, 1000 * SK_Scalar1);
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000034 SkRandom rand;
35 SkRandom randW;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000036 SkPath curves;
37 SkPath hulls;
38 SkPath ctrlPts;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000039 for (int i = 0; i < 100; ++i) {
40 SkScalar pts[] = {
41 rand.nextUScalar1(), rand.nextUScalar1(),
42 rand.nextUScalar1(), rand.nextUScalar1(),
43 rand.nextUScalar1(), rand.nextUScalar1(),
44 rand.nextUScalar1(), rand.nextUScalar1()
45 };
46 curves.moveTo(pts[0], pts[1]);
47 curves.cubicTo(pts[2], pts[3],
48 pts[4], pts[5],
49 pts[6], pts[7]);
50
51 hulls.moveTo(pts[0], pts[1]);
52 hulls.lineTo(pts[2], pts[3]);
53 hulls.lineTo(pts[4], pts[5]);
54 hulls.lineTo(pts[6], pts[7]);
55
56 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
57 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
58 ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
59 ctrlPts.addCircle(pts[6], pts[7], SK_Scalar1 / 200);
60 }
61 for (int i = 0; i < 100; ++i) {
62 SkScalar pts[] = {
63 rand.nextUScalar1(), rand.nextUScalar1(),
64 rand.nextUScalar1(), rand.nextUScalar1(),
65 rand.nextUScalar1(), rand.nextUScalar1(),
66 };
67 curves.moveTo(pts[0], pts[1]);
68 curves.quadTo(pts[2], pts[3],
69 pts[4], pts[5]);
70
71 hulls.moveTo(pts[0], pts[1]);
72 hulls.lineTo(pts[2], pts[3]);
73 hulls.lineTo(pts[4], pts[5]);
74
75 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
76 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
77 ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
78 }
79 for (int i = 0; i < 100; ++i) {
80 SkScalar pts[] = {
81 rand.nextUScalar1(), rand.nextUScalar1(),
82 rand.nextUScalar1(), rand.nextUScalar1(),
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +000083 rand.nextUScalar1(), rand.nextUScalar1(),
84 };
85 SkScalar weight = randW.nextUScalar1() * 2.0f;
86
87 curves.moveTo(pts[0], pts[1]);
88 curves.conicTo(pts[2], pts[3],
89 pts[4], pts[5],
90 weight);
91
92 hulls.moveTo(pts[0], pts[1]);
93 hulls.lineTo(pts[2], pts[3]);
94 hulls.lineTo(pts[4], pts[5]);
95
96 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
97 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
98 ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
99 }
100 for (int i = 0; i < 100; ++i) {
101 SkScalar pts[] = {
102 rand.nextUScalar1(), rand.nextUScalar1(),
103 rand.nextUScalar1(), rand.nextUScalar1(),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000104 };
105 curves.moveTo(pts[0], pts[1]);
106 curves.lineTo(pts[2], pts[3]);
107
108 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
109 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
110 }
111
112 paint.setColor(SK_ColorBLACK);
113 canvas->drawPath(curves, paint);
114 paint.setColor(SK_ColorRED);
115 //canvas->drawPath(hulls, paint);
116 paint.setStyle(SkPaint::kFill_Style);
117 paint.setColor(SK_ColorBLUE);
118 //canvas->drawPath(ctrlPts, paint);
119
120 canvas->restore();
121 }
122
123private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400124 typedef Sample INHERITED;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000125};
126
127//////////////////////////////////////////////////////////////////////////////
128
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400129DEF_SAMPLE( return new HairCurvesView(); )