1*c8dee2aaSAndroid Build Coastguard Worker // Copyright 2020 Google LLC.
2*c8dee2aaSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3*c8dee2aaSAndroid Build Coastguard Worker #include "tools/fiddle/examples.h"
4*c8dee2aaSAndroid Build Coastguard Worker REG_FIDDLE_ANIMATED(l_system_plant, 256, 256, false, 0, 3) {
5*c8dee2aaSAndroid Build Coastguard Worker // L-System
6*c8dee2aaSAndroid Build Coastguard Worker // https://en.wikipedia.org/wiki/L-system#Example_7:_Fractal_plant
7*c8dee2aaSAndroid Build Coastguard Worker
8*c8dee2aaSAndroid Build Coastguard Worker struct rules_t {
9*c8dee2aaSAndroid Build Coastguard Worker char c;
10*c8dee2aaSAndroid Build Coastguard Worker std::string s;
11*c8dee2aaSAndroid Build Coastguard Worker };
12*c8dee2aaSAndroid Build Coastguard Worker
13*c8dee2aaSAndroid Build Coastguard Worker rules_t rules[6] = {
14*c8dee2aaSAndroid Build Coastguard Worker {'X', "F-[[X]+X]+F[+FX]-X"},
15*c8dee2aaSAndroid Build Coastguard Worker {'F', "FF"},
16*c8dee2aaSAndroid Build Coastguard Worker {'+', "+"},
17*c8dee2aaSAndroid Build Coastguard Worker {'-', "-"},
18*c8dee2aaSAndroid Build Coastguard Worker {'[', "["},
19*c8dee2aaSAndroid Build Coastguard Worker {']', "]"},
20*c8dee2aaSAndroid Build Coastguard Worker };
21*c8dee2aaSAndroid Build Coastguard Worker
E(std::string s)22*c8dee2aaSAndroid Build Coastguard Worker std::string E(std::string s) {
23*c8dee2aaSAndroid Build Coastguard Worker if (s.size() == 0) {
24*c8dee2aaSAndroid Build Coastguard Worker return "";
25*c8dee2aaSAndroid Build Coastguard Worker }
26*c8dee2aaSAndroid Build Coastguard Worker for (int i=0; i<6; i++) {
27*c8dee2aaSAndroid Build Coastguard Worker if (rules[i].c == s[0]) {
28*c8dee2aaSAndroid Build Coastguard Worker return rules[i].s + E(s.substr(1));
29*c8dee2aaSAndroid Build Coastguard Worker }
30*c8dee2aaSAndroid Build Coastguard Worker }
31*c8dee2aaSAndroid Build Coastguard Worker return "";
32*c8dee2aaSAndroid Build Coastguard Worker }
33*c8dee2aaSAndroid Build Coastguard Worker
34*c8dee2aaSAndroid Build Coastguard Worker struct Pt {
35*c8dee2aaSAndroid Build Coastguard Worker SkScalar x;
36*c8dee2aaSAndroid Build Coastguard Worker SkScalar y;
37*c8dee2aaSAndroid Build Coastguard Worker SkScalar a;
38*c8dee2aaSAndroid Build Coastguard Worker };
39*c8dee2aaSAndroid Build Coastguard Worker
draw(SkCanvas * canvas)40*c8dee2aaSAndroid Build Coastguard Worker void draw(SkCanvas* canvas) {
41*c8dee2aaSAndroid Build Coastguard Worker canvas->drawColor(SK_ColorLTGRAY);
42*c8dee2aaSAndroid Build Coastguard Worker
43*c8dee2aaSAndroid Build Coastguard Worker SkPaint p;
44*c8dee2aaSAndroid Build Coastguard Worker p.setColor(0xFFA6761D);
45*c8dee2aaSAndroid Build Coastguard Worker p.setAntiAlias(true);
46*c8dee2aaSAndroid Build Coastguard Worker p.setStyle(SkPaint::kStroke_Style);
47*c8dee2aaSAndroid Build Coastguard Worker p.setStrokeWidth(1);
48*c8dee2aaSAndroid Build Coastguard Worker
49*c8dee2aaSAndroid Build Coastguard Worker std::vector<struct Pt> ptstack;
50*c8dee2aaSAndroid Build Coastguard Worker std::string plant = E(E(E(E(E("X")))));
51*c8dee2aaSAndroid Build Coastguard Worker const double len = 2.5;
52*c8dee2aaSAndroid Build Coastguard Worker struct Pt pt = {128, 256, 3.14};
53*c8dee2aaSAndroid Build Coastguard Worker SkPath path;
54*c8dee2aaSAndroid Build Coastguard Worker path.moveTo(pt.x, pt.y);
55*c8dee2aaSAndroid Build Coastguard Worker
56*c8dee2aaSAndroid Build Coastguard Worker for (std::string::iterator it=plant.begin(); it!=plant.end(); ++it) {
57*c8dee2aaSAndroid Build Coastguard Worker if (*it == 'F') {
58*c8dee2aaSAndroid Build Coastguard Worker pt.x += len*sin(pt.a);
59*c8dee2aaSAndroid Build Coastguard Worker pt.y += len*cos(pt.a);
60*c8dee2aaSAndroid Build Coastguard Worker path.lineTo(pt.x, pt.y);
61*c8dee2aaSAndroid Build Coastguard Worker } else if (*it == '+') {
62*c8dee2aaSAndroid Build Coastguard Worker pt.a += (0.15 + sin(frame*2.0*3.14159)*0.05);
63*c8dee2aaSAndroid Build Coastguard Worker } else if (*it == '-') {
64*c8dee2aaSAndroid Build Coastguard Worker pt.a += (-0.15 + sin(frame*2.0*3.14159)*0.05);
65*c8dee2aaSAndroid Build Coastguard Worker } else if (*it == '[') {
66*c8dee2aaSAndroid Build Coastguard Worker ptstack.push_back(pt);
67*c8dee2aaSAndroid Build Coastguard Worker } else if (*it == ']') {
68*c8dee2aaSAndroid Build Coastguard Worker pt = ptstack.back();
69*c8dee2aaSAndroid Build Coastguard Worker ptstack.pop_back();
70*c8dee2aaSAndroid Build Coastguard Worker path.moveTo(pt.x, pt.y);
71*c8dee2aaSAndroid Build Coastguard Worker }
72*c8dee2aaSAndroid Build Coastguard Worker }
73*c8dee2aaSAndroid Build Coastguard Worker canvas->drawPath(path, p);
74*c8dee2aaSAndroid Build Coastguard Worker }
75*c8dee2aaSAndroid Build Coastguard Worker } // END FIDDLE
76