Options
These globals are now defined:
double duration; // The requested duration of the animation. double frame; // A value in [0, 1] of where we are in the animation.
This global is now defined:
GrBackendRenderTarget backEndRenderTarget;
GrBackendTexture backEndTextureRenderTarget;
Optional source image
These globals are now defined:
SkBitmap source; sk_sp<SkImage> image; GrBackendTexture backEndTexture; // GPU Only.
Note:
Adding comments with SK_FOLD_START and SK_FOLD_END creates foldable code
blocks.
These blocks will be folded by default and are useful for highlighting specific lines of code.
You can also use the keyboard shortcuts Ctrl+S and Ctrl+E in the code editor to set them.
These blocks will be folded by default and are useful for highlighting specific lines of code.
You can also use the keyboard shortcuts Ctrl+S and Ctrl+E in the code editor to set them.
xxxxxxxxxx
70
// L-System
// https://en.wikipedia.org/wiki/L-system#Example_7:_Fractal_plant
struct rules_t {
char c;
std::string s;
};
rules_t rules[6] = {
{'X', "F-[[X]+X]+F[+FX]-X"},
{'F', "FF"},
{'+', "+"},
{'-', "-"},
{'[', "["},
{']', "]"},
};
std::string E(std::string s) {
if (s.size() == 0) {
return "";
}
for (int i=0; i<6; i++) {
if (rules[i].c == s[0]) {
return rules[i].s + E(s.substr(1));
}
}
return "";
}
struct Pt {
SkScalar x;
SkScalar y;
SkScalar a;
};
void draw(SkCanvas* canvas) {
canvas->drawColor(SK_ColorLTGRAY);
SkPaint p;
p.setColor(0xFFA6761D);
p.setAntiAlias(true);
p.setStyle(SkPaint::kStroke_Style);
p.setStrokeWidth(1);
std::vector<struct Pt> ptstack;
std::string plant = E(E(E(E(E("X")))));
const double len = 2.5;
struct Pt pt = {128, 256, 3.14};
SkPath path;
path.moveTo(pt.x, pt.y);
for (std::string::iterator it=plant.begin(); it!=plant.end(); ++it) {
if (*it == 'F') {
pt.x += len*sin(pt.a);
pt.y += len*cos(pt.a);
path.lineTo(pt.x, pt.y);
} else if (*it == '+') {
pt.a += (0.15 + sin(frame*2.0*3.14159)*0.05);
} else if (*it == '-') {
pt.a += (-0.15 + sin(frame*2.0*3.14159)*0.05);
} else if (*it == '[') {
ptstack.push_back(pt);
} else if (*it == ']') {
pt = ptstack.back();
ptstack.pop_back();
path.moveTo(pt.x, pt.y);
}
}
canvas->drawPath(path, p);
}