1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "include/core/SkCanvas.h"
9#include "include/core/SkPaint.h"
10#include "include/core/SkPath.h"
11#include "samplecode/Sample.h"
12
13// ensure that we don't accidentally screw up the bounds when the oval is
14// fractional, and the impl computes the center and radii, and uses them to
15// reconstruct the edges of the circle.
16// see bug# 1504910
17static void test_circlebounds(SkCanvas*) {
18    SkRect r = { 1.39999998f, 1, 21.3999996f, 21 };
19    SkPath p;
20    p.addOval(r);
21    SkASSERT(r == p.getBounds());
22}
23
24class CircleView : public Sample {
25    SkString name() override { return SkString("Circles"); }
26
27    void circle(SkCanvas* canvas, int width, bool aa) {
28        SkPaint paint;
29
30        paint.setAntiAlias(aa);
31        if (width < 0) {
32            paint.setStyle(SkPaint::kFill_Style);
33        } else {
34            paint.setStyle(SkPaint::kStroke_Style);
35            paint.setStrokeWidth(SkIntToScalar(width));
36        }
37        canvas->drawCircle(0, 0, 9.0f, paint);
38        if (false) { // avoid bit rot, suppress warning
39            test_circlebounds(canvas);
40        }
41    }
42
43    void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
44        for (int width = -1; width <= 1; width++) {
45            canvas->save();
46            circle(canvas, width, false);
47            canvas->translate(0, dy);
48            circle(canvas, width, true);
49            canvas->restore();
50            canvas->translate(dx, 0);
51        }
52    }
53
54    static void make_poly(SkPath* path, int n) {
55        if (n <= 0) {
56            return;
57        }
58        path->incReserve(n + 1);
59        path->moveTo(SK_Scalar1, 0);
60        SkScalar step = SK_ScalarPI * 2 / n;
61        SkScalar angle = 0;
62        for (int i = 1; i < n; i++) {
63            angle += step;
64            path->lineTo(SkScalarCos(angle), SkScalarSin(angle));
65        }
66        path->close();
67    }
68
69    void onDrawContent(SkCanvas* canvas) override {
70        SkPaint paint;
71        paint.setAntiAlias(true);
72        paint.setStyle(SkPaint::kStroke_Style);
73        SkMatrix matrix;
74        matrix.setScale(SkIntToScalar(100), SkIntToScalar(100));
75        matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200));
76        canvas->concat(matrix);
77        for (int n = 3; n < 20; n++) {
78            SkPath path;
79            make_poly(&path, n);
80            SkAutoCanvasRestore acr(canvas, true);
81            canvas->rotate(SkIntToScalar(10) * (n - 3));
82            canvas->translate(-SK_Scalar1, 0);
83            canvas->drawPath(path, paint);
84        }
85    }
86};
87
88DEF_SAMPLE( return new CircleView(); )
89