1 // Copyright 2020 Google LLC.
2 // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3 #include "tools/fiddle/examples.h"
4 REG_FIDDLE_ANIMATED(Turtle, 256, 256, false, 0, 2) {
5 // Simple turtle based graphics. The turtle starts out at 128, 128, looking North, pen down.
6 // The input string is read left to right (but see 'r' below). Commands are a single character,
7 // sometimes followed by additional arguments:
11 // + : Reads integer N, rotates turtle N degrees clockwise
12 // - : Reads integer N, rotates turtle N degrees counterclockwise
13 // f : Reads integer N, moves turtle forwards N units
14 // r : Reads integer N, then separator character C. C should be some non-digit, non-command
15 // character. Repeats all commands after C until the next instance of C, N times. Can be
17 //const char* input = "r2[r3(f50+90f50+90f50+90f50(+45uf50d[";
18 //const char* input = "r360|f1+1|";
19 const char* input = "uf100+91dr180|f3+2|+89uf60+90r2$f20-90dr60|f1+6|u-90f20$-90f50-90f30d+180f60uf1000";
21 struct Turtle { float x; float y; float h; bool p; } t;
25 paint.setColor(SK_ColorBLACK);
26 paint.setAntiAlias(true);
27 paint.setStyle(SkPaint::kStroke_Style);
28 paint.setStrokeWidth(0);
32 const char* eval(SkCanvas* canvas, const char* s, char e, float& dist, float& l, bool pt) {
35 case 'u': t.p = false; break;
36 case 'd': t.p = true; break;
37 case '+': t.h += atoi(s); break;
38 case '-': t.h -= atoi(s); break;
43 float r = t.h * 0.01745329f;
44 Turtle nt = { t.x + sinf(r) * d, t.y - cosf(r) * d, t.h, t.p };
45 if (pt && t.p) canvas->drawLine(t.x, t.y, nt.x, nt.y, p());
51 while (*s >= '0' && *s <= '9') { ++s; }
53 for (int i = 0; i < c; ++i) { n = eval(canvas, s+1, *s, dist, l, pt); }
61 void draw(SkCanvas* canvas) {
62 canvas->clear(SK_ColorWHITE);
64 t = { 128, 128, 0, true };
67 eval(canvas, input, 0, totalDist, l, false);
69 l = frame * totalDist;
70 t = { 128, 128, 0, true };
71 eval(canvas, input, 0, totalDist, l, true);