xref: /aosp_15_r20/external/skia/docs/examples/Octopus_Generator.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
paintOctopus(int x,int y,int size_base,SkColor color,SkCanvas * canvas)8 void paintOctopus(int x, int y, int size_base, SkColor color, SkCanvas* canvas) {
9     SkPaint paint;
10     paint.setAntiAlias(true);
11     paint.setColor(color);
12     int radius = 3 * size_base;
13     canvas->drawCircle(x, y, radius, paint);
14     for (int leg = 0; leg < 8; ++leg) {
15         canvas->drawCircle(x - radius + (2 * radius / 7.5 * leg),
16                            y + radius - pow(abs(4 - leg), 2), size_base / 2 + 2, paint);
17     }
18     paint.setColor(SkColorSetRGB(std::min(255u, SkColorGetR(color) + 20),
19                                  std::min(255u, SkColorGetG(color) + 20),
20                                  std::min(255u, SkColorGetB(color) + 20)));
21     canvas->drawCircle(x - size_base, y + size_base, size_base / 2, paint);
22     canvas->drawCircle(x + size_base, y + size_base, size_base / 2, paint);
23 }
24 
draw(SkCanvas * canvas)25 void draw(SkCanvas* canvas) {
26     std::default_random_engine rng;
27     const auto randScalar = [&rng](SkScalar min, SkScalar max) -> SkScalar {
28         return std::uniform_real_distribution<SkScalar>(min, max)(rng);
29     };
30     const auto randOpaqueColor = [&rng]() -> SkColor {
31         return std::uniform_int_distribution<uint32_t>(0, 0xFFFFFF)(rng) | 0xFF000000;
32     };
33 
34     for (int i = 0; i < 400; ++i) {
35         paintOctopus(randScalar(0, 256), randScalar(0, 256),
36                      randScalar(6, 12), randOpaqueColor(), canvas);
37     }
38 }
39 }  // END FIDDLE
40