beziers.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2014 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 "gm/gm.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkPaint.h"
  10. #include "include/core/SkPath.h"
  11. #include "include/core/SkScalar.h"
  12. #include "include/core/SkSize.h"
  13. #include "include/core/SkString.h"
  14. #include "include/utils/SkRandom.h"
  15. #define W 400
  16. #define H 400
  17. #define N 10
  18. constexpr SkScalar SH = SkIntToScalar(H);
  19. static void rnd_quad(SkPath* p, SkPaint* paint, SkRandom& rand) {
  20. auto a = rand.nextRangeScalar(0,W),
  21. b = rand.nextRangeScalar(0,H);
  22. p->moveTo(a,b);
  23. for (int x = 0; x < 2; ++x) {
  24. auto c = rand.nextRangeScalar(W/4, W),
  25. d = rand.nextRangeScalar( 0, H),
  26. e = rand.nextRangeScalar( 0, W),
  27. f = rand.nextRangeScalar(H/4, H);
  28. p->quadTo(c,d,e,f);
  29. }
  30. paint->setColor(rand.nextU());
  31. SkScalar width = rand.nextRangeScalar(1, 5);
  32. width *= width;
  33. paint->setStrokeWidth(width);
  34. paint->setAlphaf(1.0f);
  35. }
  36. static void rnd_cubic(SkPath* p, SkPaint* paint, SkRandom& rand) {
  37. auto a = rand.nextRangeScalar(0,W),
  38. b = rand.nextRangeScalar(0,H);
  39. p->moveTo(a,b);
  40. for (int x = 0; x < 2; ++x) {
  41. auto c = rand.nextRangeScalar(W/4, W),
  42. d = rand.nextRangeScalar( 0, H),
  43. e = rand.nextRangeScalar( 0, W),
  44. f = rand.nextRangeScalar(H/4, H),
  45. g = rand.nextRangeScalar(W/4, W),
  46. h = rand.nextRangeScalar(H/4, H);
  47. p->cubicTo(c,d,e,f,g,h);
  48. }
  49. paint->setColor(rand.nextU());
  50. SkScalar width = rand.nextRangeScalar(1, 5);
  51. width *= width;
  52. paint->setStrokeWidth(width);
  53. paint->setAlphaf(1.0f);
  54. }
  55. class BeziersGM : public skiagm::GM {
  56. public:
  57. BeziersGM() {}
  58. protected:
  59. SkString onShortName() override {
  60. return SkString("beziers");
  61. }
  62. SkISize onISize() override {
  63. return SkISize::Make(W, H*2);
  64. }
  65. void onDraw(SkCanvas* canvas) override {
  66. SkPaint paint;
  67. paint.setStyle(SkPaint::kStroke_Style);
  68. paint.setStrokeWidth(SkIntToScalar(9)/2);
  69. paint.setAntiAlias(true);
  70. SkRandom rand;
  71. for (int i = 0; i < N; i++) {
  72. SkPath p;
  73. rnd_quad(&p, &paint, rand);
  74. canvas->drawPath(p, paint);
  75. }
  76. canvas->translate(0, SH);
  77. for (int i = 0; i < N; i++) {
  78. SkPath p;
  79. rnd_cubic(&p, &paint, rand);
  80. canvas->drawPath(p, paint);
  81. }
  82. }
  83. private:
  84. typedef skiagm::GM INHERITED;
  85. };
  86. DEF_GM( return new BeziersGM; )