blob: ffc3ba65fdcf94da9d3132b500d89a48513cf7e7 [file] [log] [blame]
caryclarkfeff7d22014-10-09 05:36:03 -07001/*
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"
bungemand3ebb482015-08-05 13:57:49 -07009#include "SkPath.h"
caryclarkfeff7d22014-10-09 05:36:03 -070010#include "SkRandom.h"
11
12#define W 400
13#define H 400
14#define N 10
15
mtkleindbfd7ab2016-09-01 11:24:54 -070016constexpr SkScalar SH = SkIntToScalar(H);
caryclarkfeff7d22014-10-09 05:36:03 -070017
scroggof9d61012014-12-15 12:54:51 -080018static void rnd_quad(SkPath* p, SkPaint* paint, SkRandom& rand) {
caryclarkfeff7d22014-10-09 05:36:03 -070019 p->moveTo(rand.nextRangeScalar(0, W), rand.nextRangeScalar(0, H));
20 for (int x = 0; x < 2; ++x) {
mtklein1c127cc2016-09-26 06:29:18 -070021 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);
caryclarkfeff7d22014-10-09 05:36:03 -070026 }
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
scroggof9d61012014-12-15 12:54:51 -080034static void rnd_cubic(SkPath* p, SkPaint* paint, SkRandom& rand) {
mtklein1c127cc2016-09-26 06:29:18 -070035 auto a = rand.nextRangeScalar(0,W),
36 b = rand.nextRangeScalar(0,H);
37 p->moveTo(a,b);
caryclarkfeff7d22014-10-09 05:36:03 -070038 for (int x = 0; x < 2; ++x) {
mtklein1c127cc2016-09-26 06:29:18 -070039 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);
caryclarkfeff7d22014-10-09 05:36:03 -070046 }
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
54class BeziersGM : public skiagm::GM {
55public:
56 BeziersGM() {}
57
58protected:
caryclarkfeff7d22014-10-09 05:36:03 -070059
mtklein36352bf2015-03-25 18:17:31 -070060 SkString onShortName() override {
caryclarkfeff7d22014-10-09 05:36:03 -070061 return SkString("beziers");
62 }
63
mtklein36352bf2015-03-25 18:17:31 -070064 SkISize onISize() override {
caryclarkfeff7d22014-10-09 05:36:03 -070065 return SkISize::Make(W, H*2);
66 }
67
mtklein36352bf2015-03-25 18:17:31 -070068 void onDraw(SkCanvas* canvas) override {
caryclarkfeff7d22014-10-09 05:36:03 -070069 SkPaint paint;
70 paint.setStyle(SkPaint::kStroke_Style);
71 paint.setStrokeWidth(SkIntToScalar(9)/2);
72 paint.setAntiAlias(true);
73
scroggof9d61012014-12-15 12:54:51 -080074 SkRandom rand;
caryclarkfeff7d22014-10-09 05:36:03 -070075 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
88private:
89 typedef skiagm::GM INHERITED;
90};
91
scroggo96f16e82015-12-10 13:31:59 -080092DEF_GM( return new BeziersGM; )