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 { |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 25 | SkString name() override { return SkString("Circles"); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 26 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | void circle(SkCanvas* canvas, int width, bool aa) { |
| 28 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 29 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | 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 Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 37 | canvas->drawCircle(0, 0, 9.0f, paint); |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 38 | if (false) { // avoid bit rot, suppress warning |
| 39 | test_circlebounds(canvas); |
| 40 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 42 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 54 | 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 Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 64 | path->lineTo(SkScalarCos(angle), SkScalarSin(angle)); |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 65 | } |
| 66 | path->close(); |
| 67 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 68 | |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 69 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 70 | SkPaint paint; |
| 71 | paint.setAntiAlias(true); |
| 72 | paint.setStyle(SkPaint::kStroke_Style); |
reed@android.com | 7a99eb1 | 2009-07-16 01:13:14 +0000 | [diff] [blame] | 73 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | }; |
| 87 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 88 | DEF_SAMPLE( return new CircleView(); ) |