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