blob: 598ed62c5ec2b58cf65f694cdd142cde1f84d9da [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) {
mtklein477ba0c2016-09-26 08:18:43 -070019 auto a = rand.nextRangeScalar(0,W),
20 b = rand.nextRangeScalar(0,H);
21 p->moveTo(a,b);
caryclarkfeff7d22014-10-09 05:36:03 -070022 for (int x = 0; x < 2; ++x) {
mtklein477ba0c2016-09-26 08:18:43 -070023 auto c = rand.nextRangeScalar(W/4, W),
24 d = rand.nextRangeScalar( 0, H),
25 e = rand.nextRangeScalar( 0, W),
26 f = rand.nextRangeScalar(H/4, H);
27 p->quadTo(c,d,e,f);
caryclarkfeff7d22014-10-09 05:36:03 -070028 }
29 paint->setColor(rand.nextU());
30 SkScalar width = rand.nextRangeScalar(1, 5);
31 width *= width;
32 paint->setStrokeWidth(width);
33 paint->setAlpha(0xFF);
34}
35
scroggof9d61012014-12-15 12:54:51 -080036static void rnd_cubic(SkPath* p, SkPaint* paint, SkRandom& rand) {
mtklein1c127cc2016-09-26 06:29:18 -070037 auto a = rand.nextRangeScalar(0,W),
38 b = rand.nextRangeScalar(0,H);
39 p->moveTo(a,b);
caryclarkfeff7d22014-10-09 05:36:03 -070040 for (int x = 0; x < 2; ++x) {
mtklein1c127cc2016-09-26 06:29:18 -070041 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);
caryclarkfeff7d22014-10-09 05:36:03 -070048 }
49 paint->setColor(rand.nextU());
50 SkScalar width = rand.nextRangeScalar(1, 5);
51 width *= width;
52 paint->setStrokeWidth(width);
53 paint->setAlpha(0xFF);
54}
55
56class BeziersGM : public skiagm::GM {
57public:
58 BeziersGM() {}
59
60protected:
caryclarkfeff7d22014-10-09 05:36:03 -070061
mtklein36352bf2015-03-25 18:17:31 -070062 SkString onShortName() override {
caryclarkfeff7d22014-10-09 05:36:03 -070063 return SkString("beziers");
64 }
65
mtklein36352bf2015-03-25 18:17:31 -070066 SkISize onISize() override {
caryclarkfeff7d22014-10-09 05:36:03 -070067 return SkISize::Make(W, H*2);
68 }
69
mtklein36352bf2015-03-25 18:17:31 -070070 void onDraw(SkCanvas* canvas) override {
caryclarkfeff7d22014-10-09 05:36:03 -070071 SkPaint paint;
72 paint.setStyle(SkPaint::kStroke_Style);
73 paint.setStrokeWidth(SkIntToScalar(9)/2);
74 paint.setAntiAlias(true);
75
scroggof9d61012014-12-15 12:54:51 -080076 SkRandom rand;
caryclarkfeff7d22014-10-09 05:36:03 -070077 for (int i = 0; i < N; i++) {
78 SkPath p;
79 rnd_quad(&p, &paint, rand);
80 canvas->drawPath(p, paint);
81 }
82 canvas->translate(0, SH);
83 for (int i = 0; i < N; i++) {
84 SkPath p;
85 rnd_cubic(&p, &paint, rand);
86 canvas->drawPath(p, paint);
87 }
88 }
89
90private:
91 typedef skiagm::GM INHERITED;
92};
93
scroggo96f16e82015-12-10 13:31:59 -080094DEF_GM( return new BeziersGM; )