epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 7 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkPaint.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 |
sugoi@google.com | 93c7ee3 | 2013-03-12 14:36:57 +0000 | [diff] [blame] | 17 | static void test_circlebounds(SkCanvas*) { |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 18 | SkRect r = { 1.39999998f, 1, 21.3999996f, 21 }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | SkPath p; |
| 20 | p.addOval(r); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 21 | SkASSERT(r == p.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 24 | class CircleView : public Sample { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | public: |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 26 | static const SkScalar ANIM_DX; |
| 27 | static const SkScalar ANIM_DY; |
| 28 | static const SkScalar ANIM_RAD; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | SkScalar fDX, fDY, fRAD; |
| 30 | |
| 31 | CircleView() { |
| 32 | fDX = fDY = fRAD = 0; |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 33 | fN = 3; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 35 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 37 | virtual bool onQuery(Sample::Event* evt) { |
| 38 | if (Sample::TitleQ(*evt)) { |
| 39 | Sample::TitleR(evt, "Circles"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 40 | return true; |
| 41 | } |
| 42 | return this->INHERITED::onQuery(evt); |
| 43 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 44 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | void circle(SkCanvas* canvas, int width, bool aa) { |
| 46 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 47 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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); |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 56 | if (false) { // avoid bit rot, suppress warning |
| 57 | test_circlebounds(canvas); |
| 58 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 60 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 71 | |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 87 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 88 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 89 | SkPaint paint; |
| 90 | paint.setAntiAlias(true); |
| 91 | paint.setStyle(SkPaint::kStroke_Style); |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 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 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | private: |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 107 | int fN; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 108 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 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); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | |
| 115 | ////////////////////////////////////////////////////////////////////////////// |
| 116 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 117 | DEF_SAMPLE( return new CircleView(); ) |