blob: a75c649995d0359678d44db0a96a15f2718237e4 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkPaint.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkPath.h"
Ben Wagner6a34f3a2019-05-01 10:59:30 -040012#include "include/core/SkScalar.h"
13#include "include/core/SkSize.h"
14#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "include/utils/SkRandom.h"
caryclarkfeff7d22014-10-09 05:36:03 -070016
17#define W 400
18#define H 400
19#define N 10
20
mtkleindbfd7ab2016-09-01 11:24:54 -070021constexpr SkScalar SH = SkIntToScalar(H);
caryclarkfeff7d22014-10-09 05:36:03 -070022
scroggof9d61012014-12-15 12:54:51 -080023static void rnd_quad(SkPath* p, SkPaint* paint, SkRandom& rand) {
mtklein477ba0c2016-09-26 08:18:43 -070024 auto a = rand.nextRangeScalar(0,W),
25 b = rand.nextRangeScalar(0,H);
26 p->moveTo(a,b);
caryclarkfeff7d22014-10-09 05:36:03 -070027 for (int x = 0; x < 2; ++x) {
mtklein477ba0c2016-09-26 08:18:43 -070028 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);
caryclarkfeff7d22014-10-09 05:36:03 -070033 }
34 paint->setColor(rand.nextU());
35 SkScalar width = rand.nextRangeScalar(1, 5);
36 width *= width;
37 paint->setStrokeWidth(width);
Mike Reed9407e242019-02-15 16:13:57 -050038 paint->setAlphaf(1.0f);
caryclarkfeff7d22014-10-09 05:36:03 -070039}
40
scroggof9d61012014-12-15 12:54:51 -080041static void rnd_cubic(SkPath* p, SkPaint* paint, SkRandom& rand) {
mtklein1c127cc2016-09-26 06:29:18 -070042 auto a = rand.nextRangeScalar(0,W),
43 b = rand.nextRangeScalar(0,H);
44 p->moveTo(a,b);
caryclarkfeff7d22014-10-09 05:36:03 -070045 for (int x = 0; x < 2; ++x) {
mtklein1c127cc2016-09-26 06:29:18 -070046 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);
caryclarkfeff7d22014-10-09 05:36:03 -070053 }
54 paint->setColor(rand.nextU());
55 SkScalar width = rand.nextRangeScalar(1, 5);
56 width *= width;
57 paint->setStrokeWidth(width);
Mike Reed9407e242019-02-15 16:13:57 -050058 paint->setAlphaf(1.0f);
caryclarkfeff7d22014-10-09 05:36:03 -070059}
60
61class BeziersGM : public skiagm::GM {
62public:
63 BeziersGM() {}
64
65protected:
caryclarkfeff7d22014-10-09 05:36:03 -070066
mtklein36352bf2015-03-25 18:17:31 -070067 SkString onShortName() override {
caryclarkfeff7d22014-10-09 05:36:03 -070068 return SkString("beziers");
69 }
70
mtklein36352bf2015-03-25 18:17:31 -070071 SkISize onISize() override {
caryclarkfeff7d22014-10-09 05:36:03 -070072 return SkISize::Make(W, H*2);
73 }
74
mtklein36352bf2015-03-25 18:17:31 -070075 void onDraw(SkCanvas* canvas) override {
caryclarkfeff7d22014-10-09 05:36:03 -070076 SkPaint paint;
77 paint.setStyle(SkPaint::kStroke_Style);
78 paint.setStrokeWidth(SkIntToScalar(9)/2);
79 paint.setAntiAlias(true);
80
scroggof9d61012014-12-15 12:54:51 -080081 SkRandom rand;
caryclarkfeff7d22014-10-09 05:36:03 -070082 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
95private:
96 typedef skiagm::GM INHERITED;
97};
98
scroggo96f16e82015-12-10 13:31:59 -080099DEF_GM( return new BeziersGM; )