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