blob: 329f8e3b92c470b21f601395b32ece9a71d9b8c8 [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
caryclark@google.com6aea33f2012-10-09 14:11:58 +00007extern void showPath(const SkPath& path, const char* str);
8
9static bool drawPaths(SkCanvas* canvas, const SkPath& path, bool useOld)
10{
11 SkPath out;
12#define SHOW_PATH 0
13#if SHOW_PATH
14 showPath(path, "original:");
15#endif
16 if (useOld) {
17 simplify(path, true, out);
18 } else {
19 simplifyx(path, out);
20 }
21#if SHOW_PATH
22 showPath(out, "simplified:");
23#endif
24 SkPaint paint;
25 paint.setAntiAlias(true);
26 paint.setStyle(SkPaint::kStroke_Style);
27 // paint.setStrokeWidth(6);
28 // paint.setColor(0x1F003f7f);
29 // canvas->drawPath(path, paint);
30 paint.setColor(0xFF305F00);
31 paint.setStrokeWidth(1);
32 canvas->drawPath(out, paint);
33 return true;
34}
35
caryclark@google.coma5764232012-03-28 16:20:21 +000036// Three circles bounce inside a rectangle. The circles describe three, four
37// or five points which in turn describe a polygon. The polygon points
38// bounce inside the circles. The circles rotate and scale over time. The
39// polygons are combined into a single path, simplified, and stroked.
caryclark@google.com03f97062012-08-21 13:13:52 +000040static bool drawCircles(SkCanvas* canvas, int step, bool useOld)
caryclark@google.coma5764232012-03-28 16:20:21 +000041{
42 const int circles = 3;
43 int scales[circles];
44 int angles[circles];
45 int locs[circles * 2];
46 int pts[circles * 2 * 4];
47 int c, p;
48 for (c = 0; c < circles; ++c) {
49 scales[c] = abs(10 - (step + c * 4) % 21);
50 angles[c] = (step + c * 6) % 600;
51 locs[c * 2] = abs(130 - (step + c * 9) % 261);
52 locs[c * 2 + 1] = abs(170 - (step + c * 11) % 341);
53 for (p = 0; p < 4; ++p) {
54 pts[c * 8 + p * 2] = abs(90 - ((step + c * 121 + p * 13) % 190));
55 pts[c * 8 + p * 2 + 1] = abs(110 - ((step + c * 223 + p * 17) % 230));
56 }
57 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +000058 SkPath path;
caryclark@google.coma5764232012-03-28 16:20:21 +000059 for (c = 0; c < circles; ++c) {
60 for (p = 0; p < 4; ++p) {
61 SkScalar x = pts[c * 8 + p * 2];
62 SkScalar y = pts[c * 8 + p * 2 + 1];
63 x *= 3 + scales[c] / 10.0f;
64 y *= 3 + scales[c] / 10.0f;
65 SkScalar angle = angles[c] * 3.1415f * 2 / 600;
caryclark@google.com03f97062012-08-21 13:13:52 +000066 SkScalar temp = (SkScalar) (x * cos(angle) - y * sin(angle));
67 y = (SkScalar) (x * sin(angle) + y * cos(angle));
caryclark@google.coma5764232012-03-28 16:20:21 +000068 x = temp;
69 x += locs[c * 2] * 200 / 130.0f;
70 y += locs[c * 2 + 1] * 200 / 170.0f;
71 x += 50;
72 // y += 200;
73 if (p == 0) {
74 path.moveTo(x, y);
75 } else {
76 path.lineTo(x, y);
77 }
78 }
79 path.close();
80 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +000081 return drawPaths(canvas, path, useOld);
caryclark@google.coma5764232012-03-28 16:20:21 +000082}
83
rmistry@google.comd6176b02012-08-23 18:14:13 +000084static void createStar(SkPath& path, SkScalar innerRadius, SkScalar outerRadius,
caryclark@google.coma5764232012-03-28 16:20:21 +000085 SkScalar startAngle, int points, SkPoint center) {
86 SkScalar angle = startAngle;
87 for (int index = 0; index < points * 2; ++index) {
88 SkScalar radius = index & 1 ? outerRadius : innerRadius;
caryclark@google.com03f97062012-08-21 13:13:52 +000089 SkScalar x = (SkScalar) (radius * cos(angle));
90 SkScalar y = (SkScalar) (radius * sin(angle));
caryclark@google.coma5764232012-03-28 16:20:21 +000091 x += center.fX;
92 y += center.fY;
93 if (index == 0) {
94 path.moveTo(x, y);
95 } else {
96 path.lineTo(x, y);
97 }
98 angle += 3.1415f / points;
99 }
100 path.close();
101}
102
caryclark@google.com03f97062012-08-21 13:13:52 +0000103static bool drawStars(SkCanvas* canvas, int step, bool useOld)
caryclark@google.coma5764232012-03-28 16:20:21 +0000104{
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000105 SkPath path;
caryclark@google.coma5764232012-03-28 16:20:21 +0000106 const int stars = 25;
107 int pts[stars];
caryclark@google.com03f97062012-08-21 13:13:52 +0000108 // static bool initialize = true;
caryclark@google.coma5764232012-03-28 16:20:21 +0000109 int s;
110 for (s = 0; s < stars; ++s) {
111 pts[s] = 4 + (s % 7);
112 }
113 SkPoint locs[stars];
114 SkScalar angles[stars];
115 SkScalar innerRadius[stars];
116 SkScalar outerRadius[stars];
117 const int width = 640;
118 const int height = 480;
119 const int margin = 30;
120 const int minRadius = 120;
121 const int maxInner = 800;
122 const int maxOuter = 1153;
123 for (s = 0; s < stars; ++s) {
caryclark@google.com03f97062012-08-21 13:13:52 +0000124 int starW = (int) (width - margin * 2 + (SkScalar) s * (stars - s) / stars);
caryclark@google.coma5764232012-03-28 16:20:21 +0000125 locs[s].fX = (int) (step * (1.3f * (s + 1) / stars) + s * 121) % (starW * 2);
126 if (locs[s].fX > starW) {
127 locs[s].fX = starW * 2 - locs[s].fX;
128 }
129 locs[s].fX += margin;
caryclark@google.com03f97062012-08-21 13:13:52 +0000130 int starH = (int) (height - margin * 2 + (SkScalar) s * s / stars);
caryclark@google.coma5764232012-03-28 16:20:21 +0000131 locs[s].fY = (int) (step * (1.7f * (s + 1) / stars) + s * 183) % (starH * 2);
132 if (locs[s].fY > starH) {
133 locs[s].fY = starH * 2 - locs[s].fY;
134 }
135 locs[s].fY += margin;
136 angles[s] = ((step + s * 47) % (360 * 4)) * 3.1415f / 180 / 4;
137 innerRadius[s] = (step + s * 30) % (maxInner * 2);
138 if (innerRadius[s] > maxInner) {
139 innerRadius[s] = (maxInner * 2) - innerRadius[s];
140 }
141 innerRadius[s] = innerRadius[s] / 4 + minRadius;
142 outerRadius[s] = (step + s * 70) % (maxOuter * 2);
143 if (outerRadius[s] > maxOuter) {
144 outerRadius[s] = (maxOuter * 2) - outerRadius[s];
145 }
146 outerRadius[s] = outerRadius[s] / 4 + minRadius;
147 createStar(path, innerRadius[s] / 4.0f, outerRadius[s] / 4.0f,
148 angles[s], pts[s], locs[s]);
149 }
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000150 return drawPaths(canvas, path, useOld);
151}
152
153static void tryRoncoOnce(const SkPath& path, const SkRect& target, bool show) {
154 // capture everything in a desired rectangle
155 SkPath tiny;
156 bool closed = true;
157 SkPath::Iter iter(path, false);
158 SkPoint pts[4];
159 SkPath::Verb verb;
160 int count = 0;
161 SkPoint lastPt;
162 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
163 switch (verb) {
164 case SkPath::kMove_Verb:
165 count = 0;
166 break;
167 case SkPath::kLine_Verb:
168 count = 1;
169 break;
170 case SkPath::kQuad_Verb:
171 count = 2;
172 break;
173 case SkPath::kCubic_Verb:
174 count = 3;
175 break;
176 case SkPath::kClose_Verb:
177 if (!closed) {
178 tiny.close();
179 closed = true;
180 }
181 count = 0;
182 break;
183 default:
184 SkDEBUGFAIL("bad verb");
185 }
186 if (!count) {
187 continue;
188 }
189 SkRect bounds;
190 bounds.set(pts[0].fX, pts[0].fY, pts[0].fX, pts[0].fY);
191 for (int i = 1; i <= count; ++i) {
192 bounds.growToInclude(pts[i].fX + 0.1f, pts[i].fY + 0.1f);
193 }
194 if (!SkRect::Intersects(target, bounds)) {
195 continue;
196 }
197 if (closed) {
198 tiny.moveTo(pts[0].fX, pts[0].fY);
199 closed = false;
200 } else if (pts[0] != lastPt) {
201 tiny.lineTo(pts[0].fX, pts[0].fY);
202 }
203 switch (verb) {
204 case SkPath::kLine_Verb:
205 tiny.lineTo(pts[1].fX, pts[1].fY);
206 lastPt = pts[1];
207 break;
208 case SkPath::kQuad_Verb:
209 tiny.quadTo(pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
210 lastPt = pts[2];
211 break;
212 case SkPath::kCubic_Verb:
213 tiny.cubicTo(pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY, pts[3].fX, pts[3].fY);
214 lastPt = pts[3];
215 break;
216 default:
217 SkDEBUGFAIL("bad verb");
218 }
219 }
220 if (!closed) {
221 tiny.close();
222 }
223 if (show) {
224 showPath(tiny, NULL);
225 SkDebugf("simplified:\n");
226 }
227 SkPath out;
228 simplifyx(tiny, out);
229}
230
231static void tryRonco(const SkPath& path) {
232 const SkRect& overall = path.getBounds();
233 const int divs = 50;
234 SkScalar cellWidth = overall.width() / divs * 2;
skia.committer@gmail.com439cb512012-10-10 02:01:30 +0000235 SkScalar cellHeight = overall.height() / divs * 2;
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000236 SkRect target;
237 if (true) {
238 int xDiv = 21;
239 int yDiv = 9;
240 target.setXYWH(overall.fLeft + (overall.width() - cellWidth) * xDiv / divs,
241 overall.fTop + (overall.height() - cellHeight) * yDiv / divs,
242 cellWidth, cellHeight);
243 tryRoncoOnce(path, target, true);
caryclark@google.com03f97062012-08-21 13:13:52 +0000244 } else {
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000245 for (int xDiv = 0; xDiv < divs; ++xDiv) {
246 for (int yDiv = 0; yDiv < divs; ++yDiv) {
247 target.setXYWH(overall.fLeft + (overall.width() - cellWidth) * xDiv / divs,
248 overall.fTop + (overall.height() - cellHeight) * yDiv / divs,
249 cellWidth, cellHeight);
250 tryRoncoOnce(path, target, false);
251 }
252 }
253 }
254}
skia.committer@gmail.com439cb512012-10-10 02:01:30 +0000255
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000256static bool drawLetters(SkCanvas* canvas, int step, bool useOld)
257{
258 SkPath path;
259 const int width = 640;
260 const int height = 480;
261 const char testStr[] = "Merge";
262 const int testStrLen = sizeof(testStr) - 1;
263 SkPoint textPos[testStrLen];
264 SkScalar widths[testStrLen];
265 SkPaint paint;
266 paint.setTextSize(40);
267 paint.setAntiAlias(true);
268 paint.getTextWidths(testStr, testStrLen, widths, NULL);
269 SkScalar running = 0;
270 for (int x = 0; x < testStrLen; ++x) {
271 SkScalar width = widths[x];
272 widths[x] = running;
273 running += width;
274 }
275 SkScalar bias = (width - widths[testStrLen - 1]) / 2;
276 for (int x = 0; x < testStrLen; ++x) {
277 textPos[x].fX = bias + widths[x];
278 textPos[x].fY = height / 2;
279 }
280 paint.setTextSize(40 + step / 100.0f);
281#if 0
282 for (int mask = 0; mask < 1 << testStrLen; ++mask) {
283 char maskStr[testStrLen];
284 // mask = 26;
285 for (int letter = 0; letter < testStrLen; ++letter) {
286 maskStr[letter] = mask & (1 << letter) ? testStr[letter] : ' ';
287 }
288 paint.getPosTextPath(maskStr, testStrLen, textPos, &path);
289 showPath(path, NULL);
290 SkDebugf("%d simplified:\n", mask);
291 SkPath out;
caryclark@google.com03f97062012-08-21 13:13:52 +0000292 simplifyx(path, out);
293 }
294#endif
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000295 paint.getPosTextPath(testStr, testStrLen, textPos, &path);
296#if 1
297 tryRonco(path);
caryclark@google.coma5764232012-03-28 16:20:21 +0000298#endif
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000299#if 1
300 showPath(path, NULL);
301 SkDebugf("simplified:\n");
caryclark@google.coma5764232012-03-28 16:20:21 +0000302#endif
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000303 return drawPaths(canvas, path, false);
caryclark@google.coma5764232012-03-28 16:20:21 +0000304}
305
caryclark@google.com03f97062012-08-21 13:13:52 +0000306static bool (*drawDemos[])(SkCanvas* , int , bool ) = {
caryclark@google.coma5764232012-03-28 16:20:21 +0000307 drawStars,
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000308 drawCircles,
309 drawLetters,
caryclark@google.coma5764232012-03-28 16:20:21 +0000310};
311
312static size_t drawDemosCount = sizeof(drawDemos) / sizeof(drawDemos[0]);
313
caryclark@google.com6aea33f2012-10-09 14:11:58 +0000314static bool (*firstTest)(SkCanvas* , int , bool) = drawLetters;
caryclark@google.coma5764232012-03-28 16:20:21 +0000315
316
caryclark@google.com03f97062012-08-21 13:13:52 +0000317bool DrawEdgeDemo(SkCanvas* canvas, int step, bool useOld) {
caryclark@google.coma5764232012-03-28 16:20:21 +0000318 size_t index = 0;
319 if (firstTest) {
320 while (index < drawDemosCount && drawDemos[index] != firstTest) {
321 ++index;
322 }
323 }
caryclark@google.com03f97062012-08-21 13:13:52 +0000324 return (*drawDemos[index])(canvas, step, useOld);
caryclark@google.coma5764232012-03-28 16:20:21 +0000325}