blob: c4126f9a03c689b37ef02145cccad6849b47aad4 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
bungemand3ebb482015-08-05 13:57:49 -07007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkPaint.h"
10#include "include/core/SkPath.h"
11#include "samplecode/Sample.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012
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
sugoi@google.com93c7ee32013-03-12 14:36:57 +000017static void test_circlebounds(SkCanvas*) {
reed@google.com261b8e22011-04-14 17:53:24 +000018 SkRect r = { 1.39999998f, 1, 21.3999996f, 21 };
reed@android.com8a1c16f2008-12-17 15:59:43 +000019 SkPath p;
20 p.addOval(r);
reed@android.comd252db02009-04-01 18:31:44 +000021 SkASSERT(r == p.getBounds());
reed@android.com8a1c16f2008-12-17 15:59:43 +000022}
23
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040024class CircleView : public Sample {
Hal Canaryd7639af2019-07-17 09:08:11 -040025 SkString name() override { return SkString("Circles"); }
rmistry@google.comae933ce2012-08-23 18:19:56 +000026
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 void circle(SkCanvas* canvas, int width, bool aa) {
28 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000029
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 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 }
Hal Canaryd7639af2019-07-17 09:08:11 -040037 canvas->drawCircle(0, 0, 9.0f, paint);
caryclark@google.com02939ce2012-06-06 12:09:51 +000038 if (false) { // avoid bit rot, suppress warning
39 test_circlebounds(canvas);
40 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000042
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000053
reed@android.com7a99eb12009-07-16 01:13:14 +000054 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;
Brian Osman4428f2c2019-04-02 10:59:28 -040064 path->lineTo(SkScalarCos(angle), SkScalarSin(angle));
reed@android.com7a99eb12009-07-16 01:13:14 +000065 }
66 path->close();
67 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
Hal Canaryd7639af2019-07-17 09:08:11 -040069 void onDrawContent(SkCanvas* canvas) override {
reed@android.com7a99eb12009-07-16 01:13:14 +000070 SkPaint paint;
71 paint.setAntiAlias(true);
72 paint.setStyle(SkPaint::kStroke_Style);
reed@android.com7a99eb12009-07-16 01:13:14 +000073 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 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000086};
87
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040088DEF_SAMPLE( return new CircleView(); )