blob: 2cf0b3c90083a8981e2e36b5efffd414a28a66b4 [file] [log] [blame]
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkPath.h"
12#include "SkRandom.h"
13
14class HairCurvesView : public SampleView {
15public:
16 HairCurvesView() {
17 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000018
bsalomon@google.comaeb21602011-08-30 18:13:44 +000019protected:
20 // overrides from SkEventSink
21 virtual bool onQuery(SkEvent* evt) {
22 if (SampleCode::TitleQ(*evt)) {
23 SampleCode::TitleR(evt, "HairCurves");
24 return true;
25 }
26 return this->INHERITED::onQuery(evt);
27 }
28
29
30 virtual void onDrawContent(SkCanvas* canvas) {
31 SkPaint paint;
32 paint.setAntiAlias(true);
33 paint.setStyle(SkPaint::kStroke_Style);
egdaniel@google.coma3088832013-07-17 19:30:41 +000034 paint.setStrokeWidth(0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000035 canvas->save();
36 canvas->scale(1000 * SK_Scalar1, 1000 * SK_Scalar1);
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000037 SkRandom rand;
38 SkRandom randW;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000039 SkPath curves;
40 SkPath hulls;
41 SkPath ctrlPts;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000042 for (int i = 0; i < 100; ++i) {
43 SkScalar pts[] = {
44 rand.nextUScalar1(), rand.nextUScalar1(),
45 rand.nextUScalar1(), rand.nextUScalar1(),
46 rand.nextUScalar1(), rand.nextUScalar1(),
47 rand.nextUScalar1(), rand.nextUScalar1()
48 };
49 curves.moveTo(pts[0], pts[1]);
50 curves.cubicTo(pts[2], pts[3],
51 pts[4], pts[5],
52 pts[6], pts[7]);
53
54 hulls.moveTo(pts[0], pts[1]);
55 hulls.lineTo(pts[2], pts[3]);
56 hulls.lineTo(pts[4], pts[5]);
57 hulls.lineTo(pts[6], pts[7]);
58
59 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
60 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
61 ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
62 ctrlPts.addCircle(pts[6], pts[7], SK_Scalar1 / 200);
63 }
64 for (int i = 0; i < 100; ++i) {
65 SkScalar pts[] = {
66 rand.nextUScalar1(), rand.nextUScalar1(),
67 rand.nextUScalar1(), rand.nextUScalar1(),
68 rand.nextUScalar1(), rand.nextUScalar1(),
69 };
70 curves.moveTo(pts[0], pts[1]);
71 curves.quadTo(pts[2], pts[3],
72 pts[4], pts[5]);
73
74 hulls.moveTo(pts[0], pts[1]);
75 hulls.lineTo(pts[2], pts[3]);
76 hulls.lineTo(pts[4], pts[5]);
77
78 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
79 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
80 ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
81 }
82 for (int i = 0; i < 100; ++i) {
83 SkScalar pts[] = {
84 rand.nextUScalar1(), rand.nextUScalar1(),
85 rand.nextUScalar1(), rand.nextUScalar1(),
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +000086 rand.nextUScalar1(), rand.nextUScalar1(),
87 };
88 SkScalar weight = randW.nextUScalar1() * 2.0f;
89
90 curves.moveTo(pts[0], pts[1]);
91 curves.conicTo(pts[2], pts[3],
92 pts[4], pts[5],
93 weight);
94
95 hulls.moveTo(pts[0], pts[1]);
96 hulls.lineTo(pts[2], pts[3]);
97 hulls.lineTo(pts[4], pts[5]);
98
99 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
100 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
101 ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
102 }
103 for (int i = 0; i < 100; ++i) {
104 SkScalar pts[] = {
105 rand.nextUScalar1(), rand.nextUScalar1(),
106 rand.nextUScalar1(), rand.nextUScalar1(),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000107 };
108 curves.moveTo(pts[0], pts[1]);
109 curves.lineTo(pts[2], pts[3]);
110
111 ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
112 ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
113 }
114
115 paint.setColor(SK_ColorBLACK);
116 canvas->drawPath(curves, paint);
117 paint.setColor(SK_ColorRED);
118 //canvas->drawPath(hulls, paint);
119 paint.setStyle(SkPaint::kFill_Style);
120 paint.setColor(SK_ColorBLUE);
121 //canvas->drawPath(ctrlPts, paint);
122
123 canvas->restore();
124 }
125
126private:
127 typedef SampleView INHERITED;
128};
129
130//////////////////////////////////////////////////////////////////////////////
131
132static SkView* MyFactory() { return new HairCurvesView; }
133static SkViewRegister reg(MyFactory);