| 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 |
|
| 5 |
REG_FIDDLE(Octopus_Generator, 256, 256, false, 0) { |
| 6 |
#include <random> |
| 7 |
|
| 8 |
void paintOctopus(float x, float y, int size_base, SkColor color, SkCanvas* canvas) { |
| 9 |
// Set up the paint to draw the head and legs |
| 10 |
SkPaint paint; |
| 11 |
paint.setAntiAlias(true); |
| 12 |
paint.setColor(color); |
| 13 |
int radius = 3 * size_base; |
| 14 |
// Draw the big head |
| 15 |
canvas->drawCircle(x, y, radius, paint); |
| 16 |
// Draw 8 small legs |
| 17 |
for (int leg = 0; leg < 8; ++leg) { |
| 18 |
canvas->drawCircle(x - radius + (2 * radius / 7.5 * leg), |
| 19 |
y + radius - pow(abs(4 - leg), 2), size_base / 2 + 2, paint); |
| 20 |
} |
| 21 |
// Make the color a bit lighter for the eyes |
| 22 |
paint.setColor(SkColorSetRGB(std::min(255u, SkColorGetR(color) + 20), |
| 23 |
std::min(255u, SkColorGetG(color) + 20), |
| 24 |
std::min(255u, SkColorGetB(color) + 20))); |
| 25 |
// Draw the left and right eye |
| 26 |
canvas->drawCircle(x - size_base, y + size_base, size_base / 2, paint); |
| 27 |
canvas->drawCircle(x + size_base, y + size_base, size_base / 2, paint); |
| 28 |
} |
| 29 |
|
| 30 |
void draw(SkCanvas* canvas) { |
| 31 |
// Some helper functions to create random coordinates, sizes, and colors |
| 32 |
std::default_random_engine rng; |
| 33 |
const auto randScalar = [&rng](SkScalar min, SkScalar max) -> SkScalar { |
| 34 |
return std::uniform_real_distribution<SkScalar>(min, max)(rng); |
| 35 |
}; |
| 36 |
const auto randOpaqueColor = [&rng]() -> SkColor { |
| 37 |
return std::uniform_int_distribution<uint32_t>(0, 0xFFFFFF)(rng) | 0xFF000000; |
| 38 |
}; |
| 39 |
|
| 40 |
constexpr int numOctopods = 400; |
| 41 |
for (int i = 0; i < numOctopods; ++i) { |
| 42 |
paintOctopus(randScalar(0, 256), randScalar(0, 256), |
| 43 |
randScalar(6, 12), randOpaqueColor(), canvas); |
| 44 |
} |
| 45 |
} |
| 46 |
} // END FIDDLE |