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