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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkPaint.h" |
| 10 | #include "include/core/SkPath.h" |
| 11 | #include "samplecode/Sample.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: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 37 | virtual SkString name() { return SkString("Circles"); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 38 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | void circle(SkCanvas* canvas, int width, bool aa) { |
| 40 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 41 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | paint.setAntiAlias(aa); |
| 43 | if (width < 0) { |
| 44 | paint.setStyle(SkPaint::kFill_Style); |
| 45 | } else { |
| 46 | paint.setStyle(SkPaint::kStroke_Style); |
| 47 | paint.setStrokeWidth(SkIntToScalar(width)); |
| 48 | } |
| 49 | canvas->drawCircle(0, 0, SkIntToScalar(9) + fRAD, paint); |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 50 | if (false) { // avoid bit rot, suppress warning |
| 51 | test_circlebounds(canvas); |
| 52 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 54 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 55 | void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) { |
| 56 | for (int width = -1; width <= 1; width++) { |
| 57 | canvas->save(); |
| 58 | circle(canvas, width, false); |
| 59 | canvas->translate(0, dy); |
| 60 | circle(canvas, width, true); |
| 61 | canvas->restore(); |
| 62 | canvas->translate(dx, 0); |
| 63 | } |
| 64 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 65 | |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 66 | static void make_poly(SkPath* path, int n) { |
| 67 | if (n <= 0) { |
| 68 | return; |
| 69 | } |
| 70 | path->incReserve(n + 1); |
| 71 | path->moveTo(SK_Scalar1, 0); |
| 72 | SkScalar step = SK_ScalarPI * 2 / n; |
| 73 | SkScalar angle = 0; |
| 74 | for (int i = 1; i < n; i++) { |
| 75 | angle += step; |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 76 | path->lineTo(SkScalarCos(angle), SkScalarSin(angle)); |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 77 | } |
| 78 | path->close(); |
| 79 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 80 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 81 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 82 | SkPaint paint; |
| 83 | paint.setAntiAlias(true); |
| 84 | paint.setStyle(SkPaint::kStroke_Style); |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 85 | SkMatrix matrix; |
| 86 | matrix.setScale(SkIntToScalar(100), SkIntToScalar(100)); |
| 87 | matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200)); |
| 88 | canvas->concat(matrix); |
| 89 | for (int n = 3; n < 20; n++) { |
| 90 | SkPath path; |
| 91 | make_poly(&path, n); |
| 92 | SkAutoCanvasRestore acr(canvas, true); |
| 93 | canvas->rotate(SkIntToScalar(10) * (n - 3)); |
| 94 | canvas->translate(-SK_Scalar1, 0); |
| 95 | canvas->drawPath(path, paint); |
| 96 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 98 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | private: |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 100 | int fN; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 101 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 104 | const SkScalar CircleView::ANIM_DX(SK_Scalar1 / 67); |
| 105 | const SkScalar CircleView::ANIM_DY(SK_Scalar1 / 29); |
| 106 | const SkScalar CircleView::ANIM_RAD(SK_Scalar1 / 19); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | |
| 108 | ////////////////////////////////////////////////////////////////////////////// |
| 109 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 110 | DEF_SAMPLE( return new CircleView(); ) |