• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
7 #include "SampleCode.h"
8 #include "SkView.h"
9 #include "SkCanvas.h"
10 #include "SkPath.h"
11 #include "SkRandom.h"
12 
13 class HairCurvesView : public SampleView {
14 public:
HairCurvesView()15     HairCurvesView() {
16     }
17 
18 protected:
19     // overrides from SkEventSink
onQuery(SkEvent * evt)20     virtual bool onQuery(SkEvent* evt) {
21         if (SampleCode::TitleQ(*evt)) {
22             SampleCode::TitleR(evt, "HairCurves");
23             return true;
24         }
25         return this->INHERITED::onQuery(evt);
26     }
27 
28 
onDrawContent(SkCanvas * canvas)29     virtual void onDrawContent(SkCanvas* canvas) {
30         SkPaint paint;
31         paint.setAntiAlias(true);
32         paint.setStyle(SkPaint::kStroke_Style);
33         paint.setStrokeWidth(0);
34         canvas->save();
35         canvas->scale(1000 * SK_Scalar1, 1000 * SK_Scalar1);
36         SkRandom rand;
37         SkRandom randW;
38         SkPath curves;
39         SkPath hulls;
40         SkPath ctrlPts;
41         for (int i = 0; i < 100; ++i) {
42             SkScalar pts[] = {
43                 rand.nextUScalar1(), rand.nextUScalar1(),
44                 rand.nextUScalar1(), rand.nextUScalar1(),
45                 rand.nextUScalar1(), rand.nextUScalar1(),
46                 rand.nextUScalar1(), rand.nextUScalar1()
47             };
48             curves.moveTo(pts[0], pts[1]);
49             curves.cubicTo(pts[2], pts[3],
50                          pts[4], pts[5],
51                          pts[6], pts[7]);
52 
53             hulls.moveTo(pts[0], pts[1]);
54             hulls.lineTo(pts[2], pts[3]);
55             hulls.lineTo(pts[4], pts[5]);
56             hulls.lineTo(pts[6], pts[7]);
57 
58             ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
59             ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
60             ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
61             ctrlPts.addCircle(pts[6], pts[7], SK_Scalar1 / 200);
62         }
63         for (int i = 0; i < 100; ++i) {
64             SkScalar pts[] = {
65                 rand.nextUScalar1(), rand.nextUScalar1(),
66                 rand.nextUScalar1(), rand.nextUScalar1(),
67                 rand.nextUScalar1(), rand.nextUScalar1(),
68             };
69             curves.moveTo(pts[0], pts[1]);
70             curves.quadTo(pts[2], pts[3],
71                           pts[4], pts[5]);
72 
73             hulls.moveTo(pts[0], pts[1]);
74             hulls.lineTo(pts[2], pts[3]);
75             hulls.lineTo(pts[4], pts[5]);
76 
77             ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
78             ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
79             ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
80         }
81         for (int i = 0; i < 100; ++i) {
82             SkScalar pts[] = {
83                 rand.nextUScalar1(), rand.nextUScalar1(),
84                 rand.nextUScalar1(), rand.nextUScalar1(),
85                 rand.nextUScalar1(), rand.nextUScalar1(),
86             };
87             SkScalar weight = randW.nextUScalar1() * 2.0f;
88 
89             curves.moveTo(pts[0], pts[1]);
90             curves.conicTo(pts[2], pts[3],
91                           pts[4], pts[5],
92                           weight);
93 
94             hulls.moveTo(pts[0], pts[1]);
95             hulls.lineTo(pts[2], pts[3]);
96             hulls.lineTo(pts[4], pts[5]);
97 
98             ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
99             ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
100             ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200);
101         }
102         for (int i = 0; i < 100; ++i) {
103             SkScalar pts[] = {
104                 rand.nextUScalar1(), rand.nextUScalar1(),
105                 rand.nextUScalar1(), rand.nextUScalar1(),
106             };
107             curves.moveTo(pts[0], pts[1]);
108             curves.lineTo(pts[2], pts[3]);
109 
110             ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200);
111             ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200);
112         }
113 
114         paint.setColor(SK_ColorBLACK);
115         canvas->drawPath(curves, paint);
116         paint.setColor(SK_ColorRED);
117         //canvas->drawPath(hulls, paint);
118         paint.setStyle(SkPaint::kFill_Style);
119         paint.setColor(SK_ColorBLUE);
120         //canvas->drawPath(ctrlPts, paint);
121 
122         canvas->restore();
123     }
124 
125 private:
126     typedef SampleView INHERITED;
127 };
128 
129 //////////////////////////////////////////////////////////////////////////////
130 
MyFactory()131 static SkView* MyFactory() { return new HairCurvesView; }
132 static SkViewRegister reg(MyFactory);
133