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 "Sample.h"
9 #include "SkCanvas.h"
10 #include "SkPaint.h"
11 #include "SkPath.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
test_circlebounds(SkCanvas *)17 static 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
24 class CircleView : public Sample {
25 public:
26 static const SkScalar ANIM_DX;
27 static const SkScalar ANIM_DY;
28 static const SkScalar ANIM_RAD;
29 SkScalar fDX, fDY, fRAD;
30
CircleView()31 CircleView() {
32 fDX = fDY = fRAD = 0;
33 fN = 3;
34 }
35
36 protected:
onQuery(Sample::Event * evt)37 virtual bool onQuery(Sample::Event* evt) {
38 if (Sample::TitleQ(*evt)) {
39 Sample::TitleR(evt, "Circles");
40 return true;
41 }
42 return this->INHERITED::onQuery(evt);
43 }
44
circle(SkCanvas * canvas,int width,bool aa)45 void circle(SkCanvas* canvas, int width, bool aa) {
46 SkPaint paint;
47
48 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);
56 if (false) { // avoid bit rot, suppress warning
57 test_circlebounds(canvas);
58 }
59 }
60
drawSix(SkCanvas * canvas,SkScalar dx,SkScalar dy)61 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 }
71
make_poly(SkPath * path,int n)72 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 }
87
onDrawContent(SkCanvas * canvas)88 virtual void onDrawContent(SkCanvas* canvas) {
89 SkPaint paint;
90 paint.setAntiAlias(true);
91 paint.setStyle(SkPaint::kStroke_Style);
92 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 }
104 }
105
106 private:
107 int fN;
108 typedef Sample INHERITED;
109 };
110
111 const SkScalar CircleView::ANIM_DX(SK_Scalar1 / 67);
112 const SkScalar CircleView::ANIM_DY(SK_Scalar1 / 29);
113 const SkScalar CircleView::ANIM_RAD(SK_Scalar1 / 19);
114
115 //////////////////////////////////////////////////////////////////////////////
116
117 DEF_SAMPLE( return new CircleView(); )
118