blob: 3063324128df6068354138811511bdfdba98fb81 [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04008#include "Sample.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkCanvas.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkPaint.h"
bungemand3ebb482015-08-05 13:57:49 -070011#include "SkPath.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 {
reed@android.com8a1c16f2008-12-17 15:59:43 +000025public:
reed@android.comf2b98d62010-12-20 18:26:13 +000026 static const SkScalar ANIM_DX;
27 static const SkScalar ANIM_DY;
28 static const SkScalar ANIM_RAD;
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 SkScalar fDX, fDY, fRAD;
30
31 CircleView() {
32 fDX = fDY = fRAD = 0;
reed@android.com7a99eb12009-07-16 01:13:14 +000033 fN = 3;
reed@android.com8a1c16f2008-12-17 15:59:43 +000034 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000035
reed@android.com8a1c16f2008-12-17 15:59:43 +000036protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040037 virtual bool onQuery(Sample::Event* evt) {
38 if (Sample::TitleQ(*evt)) {
39 Sample::TitleR(evt, "Circles");
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 return true;
41 }
42 return this->INHERITED::onQuery(evt);
43 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000044
reed@android.com8a1c16f2008-12-17 15:59:43 +000045 void circle(SkCanvas* canvas, int width, bool aa) {
46 SkPaint paint;
rmistry@google.comae933ce2012-08-23 18:19:56 +000047
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 paint.setAntiAlias(aa);
49 if (width < 0) {
50 paint.setStyle(SkPaint::kFill_Style);
51 } else {
52 paint.setStyle(SkPaint::kStroke_Style);
53 paint.setStrokeWidth(SkIntToScalar(width));
54 }
55 canvas->drawCircle(0, 0, SkIntToScalar(9) + fRAD, paint);
caryclark@google.com02939ce2012-06-06 12:09:51 +000056 if (false) { // avoid bit rot, suppress warning
57 test_circlebounds(canvas);
58 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000060
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
62 for (int width = -1; width <= 1; width++) {
63 canvas->save();
64 circle(canvas, width, false);
65 canvas->translate(0, dy);
66 circle(canvas, width, true);
67 canvas->restore();
68 canvas->translate(dx, 0);
69 }
70 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000071
reed@android.com7a99eb12009-07-16 01:13:14 +000072 static void make_poly(SkPath* path, int n) {
73 if (n <= 0) {
74 return;
75 }
76 path->incReserve(n + 1);
77 path->moveTo(SK_Scalar1, 0);
78 SkScalar step = SK_ScalarPI * 2 / n;
79 SkScalar angle = 0;
80 for (int i = 1; i < n; i++) {
81 angle += step;
82 SkScalar c, s = SkScalarSinCos(angle, &c);
83 path->lineTo(c, s);
84 }
85 path->close();
86 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000087
mike@reedtribe.org5fd92432011-05-05 01:59:48 +000088 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com7a99eb12009-07-16 01:13:14 +000089 SkPaint paint;
90 paint.setAntiAlias(true);
91 paint.setStyle(SkPaint::kStroke_Style);
reed@android.com7a99eb12009-07-16 01:13:14 +000092 SkMatrix matrix;
93 matrix.setScale(SkIntToScalar(100), SkIntToScalar(100));
94 matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200));
95 canvas->concat(matrix);
96 for (int n = 3; n < 20; n++) {
97 SkPath path;
98 make_poly(&path, n);
99 SkAutoCanvasRestore acr(canvas, true);
100 canvas->rotate(SkIntToScalar(10) * (n - 3));
101 canvas->translate(-SK_Scalar1, 0);
102 canvas->drawPath(path, paint);
103 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106private:
reed@android.com7a99eb12009-07-16 01:13:14 +0000107 int fN;
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400108 typedef Sample INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109};
110
reed@android.comf2b98d62010-12-20 18:26:13 +0000111const SkScalar CircleView::ANIM_DX(SK_Scalar1 / 67);
112const SkScalar CircleView::ANIM_DY(SK_Scalar1 / 29);
113const SkScalar CircleView::ANIM_RAD(SK_Scalar1 / 19);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114
115//////////////////////////////////////////////////////////////////////////////
116
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400117DEF_SAMPLE( return new CircleView(); )