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