SampleCircle.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkPaint.h"
  9. #include "include/core/SkPath.h"
  10. #include "samplecode/Sample.h"
  11. // ensure that we don't accidentally screw up the bounds when the oval is
  12. // fractional, and the impl computes the center and radii, and uses them to
  13. // reconstruct the edges of the circle.
  14. // see bug# 1504910
  15. static void test_circlebounds(SkCanvas*) {
  16. SkRect r = { 1.39999998f, 1, 21.3999996f, 21 };
  17. SkPath p;
  18. p.addOval(r);
  19. SkASSERT(r == p.getBounds());
  20. }
  21. class CircleView : public Sample {
  22. SkString name() override { return SkString("Circles"); }
  23. void circle(SkCanvas* canvas, int width, bool aa) {
  24. SkPaint paint;
  25. paint.setAntiAlias(aa);
  26. if (width < 0) {
  27. paint.setStyle(SkPaint::kFill_Style);
  28. } else {
  29. paint.setStyle(SkPaint::kStroke_Style);
  30. paint.setStrokeWidth(SkIntToScalar(width));
  31. }
  32. canvas->drawCircle(0, 0, 9.0f, paint);
  33. if (false) { // avoid bit rot, suppress warning
  34. test_circlebounds(canvas);
  35. }
  36. }
  37. void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
  38. for (int width = -1; width <= 1; width++) {
  39. canvas->save();
  40. circle(canvas, width, false);
  41. canvas->translate(0, dy);
  42. circle(canvas, width, true);
  43. canvas->restore();
  44. canvas->translate(dx, 0);
  45. }
  46. }
  47. static void make_poly(SkPath* path, int n) {
  48. if (n <= 0) {
  49. return;
  50. }
  51. path->incReserve(n + 1);
  52. path->moveTo(SK_Scalar1, 0);
  53. SkScalar step = SK_ScalarPI * 2 / n;
  54. SkScalar angle = 0;
  55. for (int i = 1; i < n; i++) {
  56. angle += step;
  57. path->lineTo(SkScalarCos(angle), SkScalarSin(angle));
  58. }
  59. path->close();
  60. }
  61. void onDrawContent(SkCanvas* canvas) override {
  62. SkPaint paint;
  63. paint.setAntiAlias(true);
  64. paint.setStyle(SkPaint::kStroke_Style);
  65. SkMatrix matrix;
  66. matrix.setScale(SkIntToScalar(100), SkIntToScalar(100));
  67. matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200));
  68. canvas->concat(matrix);
  69. for (int n = 3; n < 20; n++) {
  70. SkPath path;
  71. make_poly(&path, n);
  72. SkAutoCanvasRestore acr(canvas, true);
  73. canvas->rotate(SkIntToScalar(10) * (n - 3));
  74. canvas->translate(-SK_Scalar1, 0);
  75. canvas->drawPath(path, paint);
  76. }
  77. }
  78. };
  79. DEF_SAMPLE( return new CircleView(); )