blob: 967c4f25cb7f569bf398d8703ce69cad5cf89c80 [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 */
7#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11#include "SkRandom.h"
12
13class HairCurvesView : public SampleView {
14public:
15 HairCurvesView() {
16 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000017
bsalomon@google.comaeb21602011-08-30 18:13:44 +000018protected:
19 // overrides from SkEventSink
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
29 virtual void onDrawContent(SkCanvas* canvas) {
30 SkPaint paint;
31 paint.setAntiAlias(true);
32 paint.setStyle(SkPaint::kStroke_Style);
egdaniel@google.coma3088832013-07-17 19:30:41 +000033 paint.setStrokeWidth(0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000034 canvas->save();
35 canvas->scale(1000 * SK_Scalar1, 1000 * SK_Scalar1);
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000036 SkRandom rand;
37 SkRandom randW;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000038 SkPath curves;
39 SkPath hulls;
40 SkPath ctrlPts;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000041 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(),
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +000085 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(),
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000106 };
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
125private:
126 typedef SampleView INHERITED;
127};
128
129//////////////////////////////////////////////////////////////////////////////
130
131static SkView* MyFactory() { return new HairCurvesView; }
132static SkViewRegister reg(MyFactory);