blob: b71b8186b2c2b152a82633ea5f02d3d6e7e2fdf5 [file] [log] [blame]
caryclark@google.coma5764232012-03-28 16:20:21 +00001#include "EdgeDemo.h"
2#include "EdgeWalker_Test.h"
3#include "ShapeOps.h"
4#import "SkCanvas.h"
5#import "SkPaint.h"
6
7// Three circles bounce inside a rectangle. The circles describe three, four
8// or five points which in turn describe a polygon. The polygon points
9// bounce inside the circles. The circles rotate and scale over time. The
10// polygons are combined into a single path, simplified, and stroked.
11static bool drawCircles(SkCanvas* canvas, int step)
12{
13 const int circles = 3;
14 int scales[circles];
15 int angles[circles];
16 int locs[circles * 2];
17 int pts[circles * 2 * 4];
18 int c, p;
19 for (c = 0; c < circles; ++c) {
20 scales[c] = abs(10 - (step + c * 4) % 21);
21 angles[c] = (step + c * 6) % 600;
22 locs[c * 2] = abs(130 - (step + c * 9) % 261);
23 locs[c * 2 + 1] = abs(170 - (step + c * 11) % 341);
24 for (p = 0; p < 4; ++p) {
25 pts[c * 8 + p * 2] = abs(90 - ((step + c * 121 + p * 13) % 190));
26 pts[c * 8 + p * 2 + 1] = abs(110 - ((step + c * 223 + p * 17) % 230));
27 }
28 }
29 SkPath path, out;
30 for (c = 0; c < circles; ++c) {
31 for (p = 0; p < 4; ++p) {
32 SkScalar x = pts[c * 8 + p * 2];
33 SkScalar y = pts[c * 8 + p * 2 + 1];
34 x *= 3 + scales[c] / 10.0f;
35 y *= 3 + scales[c] / 10.0f;
36 SkScalar angle = angles[c] * 3.1415f * 2 / 600;
37 SkScalar temp = x * cos(angle) - y * sin(angle);
38 y = x * sin(angle) + y * cos(angle);
39 x = temp;
40 x += locs[c * 2] * 200 / 130.0f;
41 y += locs[c * 2 + 1] * 200 / 170.0f;
42 x += 50;
43 // y += 200;
44 if (p == 0) {
45 path.moveTo(x, y);
46 } else {
47 path.lineTo(x, y);
48 }
49 }
50 path.close();
51 }
52 showPath(path, "original:");
53 simplify(path, true, out);
54 showPath(out, "simplified:");
55 SkPaint paint;
56 paint.setAntiAlias(true);
57 paint.setStyle(SkPaint::kStroke_Style);
58 paint.setStrokeWidth(3);
59 paint.setColor(0x3F007fbF);
60 canvas->drawPath(path, paint);
61 paint.setColor(0xFF60FF00);
62 paint.setStrokeWidth(1);
63 canvas->drawPath(out, paint);
64 return true;
65}
66
67static void createStar(SkPath& path, SkScalar innerRadius, SkScalar outerRadius,
68 SkScalar startAngle, int points, SkPoint center) {
69 SkScalar angle = startAngle;
70 for (int index = 0; index < points * 2; ++index) {
71 SkScalar radius = index & 1 ? outerRadius : innerRadius;
72 SkScalar x = radius * cos(angle);
73 SkScalar y = radius * sin(angle);
74 x += center.fX;
75 y += center.fY;
76 if (index == 0) {
77 path.moveTo(x, y);
78 } else {
79 path.lineTo(x, y);
80 }
81 angle += 3.1415f / points;
82 }
83 path.close();
84}
85
86static bool drawStars(SkCanvas* canvas, int step)
87{
88 SkPath path, out;
89 const int stars = 25;
90 int pts[stars];
91 static bool initialize = true;
92 int s;
93 for (s = 0; s < stars; ++s) {
94 pts[s] = 4 + (s % 7);
95 }
96 SkPoint locs[stars];
97 SkScalar angles[stars];
98 SkScalar innerRadius[stars];
99 SkScalar outerRadius[stars];
100 const int width = 640;
101 const int height = 480;
102 const int margin = 30;
103 const int minRadius = 120;
104 const int maxInner = 800;
105 const int maxOuter = 1153;
106 for (s = 0; s < stars; ++s) {
107 int starW = width - margin * 2 + (SkScalar) s * (stars - s) / stars;
108 locs[s].fX = (int) (step * (1.3f * (s + 1) / stars) + s * 121) % (starW * 2);
109 if (locs[s].fX > starW) {
110 locs[s].fX = starW * 2 - locs[s].fX;
111 }
112 locs[s].fX += margin;
113 int starH = height - margin * 2 + (SkScalar) s * s / stars;
114 locs[s].fY = (int) (step * (1.7f * (s + 1) / stars) + s * 183) % (starH * 2);
115 if (locs[s].fY > starH) {
116 locs[s].fY = starH * 2 - locs[s].fY;
117 }
118 locs[s].fY += margin;
119 angles[s] = ((step + s * 47) % (360 * 4)) * 3.1415f / 180 / 4;
120 innerRadius[s] = (step + s * 30) % (maxInner * 2);
121 if (innerRadius[s] > maxInner) {
122 innerRadius[s] = (maxInner * 2) - innerRadius[s];
123 }
124 innerRadius[s] = innerRadius[s] / 4 + minRadius;
125 outerRadius[s] = (step + s * 70) % (maxOuter * 2);
126 if (outerRadius[s] > maxOuter) {
127 outerRadius[s] = (maxOuter * 2) - outerRadius[s];
128 }
129 outerRadius[s] = outerRadius[s] / 4 + minRadius;
130 createStar(path, innerRadius[s] / 4.0f, outerRadius[s] / 4.0f,
131 angles[s], pts[s], locs[s]);
132 }
133#define SHOW_PATH 0
134#if SHOW_PATH
135 showPath(path, "original:");
136#endif
137#define TEST_SIMPLIFY 01
138#if TEST_SIMPLIFY
139 simplify(path, true, out);
140#if SHOW_PATH
141 showPath(out, "simplified:");
142#endif
143#endif
144 SkPaint paint;
145 paint.setAntiAlias(true);
146 paint.setStyle(SkPaint::kStroke_Style);
147 paint.setStrokeWidth(6);
148 paint.setColor(0x1F003f7f);
149 canvas->drawPath(path, paint);
150 paint.setColor(0xFF305F00);
151 paint.setStrokeWidth(1);
152#if TEST_SIMPLIFY
153 canvas->drawPath(out, paint);
154#endif
155 return true;
156}
157
158static bool (*drawDemos[])(SkCanvas* , int) = {
159 drawStars,
160 drawCircles
161};
162
163static size_t drawDemosCount = sizeof(drawDemos) / sizeof(drawDemos[0]);
164
165static bool (*firstTest)(SkCanvas* , int) = 0;
166
167
168bool DrawEdgeDemo(SkCanvas* canvas, int step) {
169 size_t index = 0;
170 if (firstTest) {
171 while (index < drawDemosCount && drawDemos[index] != firstTest) {
172 ++index;
173 }
174 }
175 return (*drawDemos[index])(canvas, step);
176}