bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "include/core/SkCanvas.h" |
| 8 | #include "include/core/SkPath.h" |
| 9 | #include "include/utils/SkRandom.h" |
| 10 | #include "samplecode/Sample.h" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 11 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 12 | class HairCurvesView : public Sample { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 13 | public: |
| 14 | HairCurvesView() { |
| 15 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 16 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 17 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 18 | SkString name() override { return SkString("HairCurves"); } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 19 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 20 | void onDrawContent(SkCanvas* canvas) override { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 21 | SkPaint paint; |
| 22 | paint.setAntiAlias(true); |
| 23 | paint.setStyle(SkPaint::kStroke_Style); |
egdaniel@google.com | a308883 | 2013-07-17 19:30:41 +0000 | [diff] [blame] | 24 | paint.setStrokeWidth(0); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 25 | canvas->save(); |
| 26 | canvas->scale(1000 * SK_Scalar1, 1000 * SK_Scalar1); |
commit-bot@chromium.org | e0e7cfe | 2013-09-09 20:09:12 +0000 | [diff] [blame] | 27 | SkRandom rand; |
| 28 | SkRandom randW; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 29 | SkPath curves; |
| 30 | SkPath hulls; |
| 31 | SkPath ctrlPts; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 32 | for (int i = 0; i < 100; ++i) { |
| 33 | SkScalar pts[] = { |
| 34 | rand.nextUScalar1(), rand.nextUScalar1(), |
| 35 | rand.nextUScalar1(), rand.nextUScalar1(), |
| 36 | rand.nextUScalar1(), rand.nextUScalar1(), |
| 37 | rand.nextUScalar1(), rand.nextUScalar1() |
| 38 | }; |
| 39 | curves.moveTo(pts[0], pts[1]); |
| 40 | curves.cubicTo(pts[2], pts[3], |
| 41 | pts[4], pts[5], |
| 42 | pts[6], pts[7]); |
| 43 | |
| 44 | hulls.moveTo(pts[0], pts[1]); |
| 45 | hulls.lineTo(pts[2], pts[3]); |
| 46 | hulls.lineTo(pts[4], pts[5]); |
| 47 | hulls.lineTo(pts[6], pts[7]); |
| 48 | |
| 49 | ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200); |
| 50 | ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200); |
| 51 | ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200); |
| 52 | ctrlPts.addCircle(pts[6], pts[7], SK_Scalar1 / 200); |
| 53 | } |
| 54 | for (int i = 0; i < 100; ++i) { |
| 55 | SkScalar pts[] = { |
| 56 | rand.nextUScalar1(), rand.nextUScalar1(), |
| 57 | rand.nextUScalar1(), rand.nextUScalar1(), |
| 58 | rand.nextUScalar1(), rand.nextUScalar1(), |
| 59 | }; |
| 60 | curves.moveTo(pts[0], pts[1]); |
| 61 | curves.quadTo(pts[2], pts[3], |
| 62 | pts[4], pts[5]); |
| 63 | |
| 64 | hulls.moveTo(pts[0], pts[1]); |
| 65 | hulls.lineTo(pts[2], pts[3]); |
| 66 | hulls.lineTo(pts[4], pts[5]); |
| 67 | |
| 68 | ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200); |
| 69 | ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200); |
| 70 | ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200); |
| 71 | } |
| 72 | for (int i = 0; i < 100; ++i) { |
| 73 | SkScalar pts[] = { |
| 74 | rand.nextUScalar1(), rand.nextUScalar1(), |
| 75 | rand.nextUScalar1(), rand.nextUScalar1(), |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 76 | rand.nextUScalar1(), rand.nextUScalar1(), |
| 77 | }; |
| 78 | SkScalar weight = randW.nextUScalar1() * 2.0f; |
| 79 | |
| 80 | curves.moveTo(pts[0], pts[1]); |
| 81 | curves.conicTo(pts[2], pts[3], |
| 82 | pts[4], pts[5], |
| 83 | weight); |
| 84 | |
| 85 | hulls.moveTo(pts[0], pts[1]); |
| 86 | hulls.lineTo(pts[2], pts[3]); |
| 87 | hulls.lineTo(pts[4], pts[5]); |
| 88 | |
| 89 | ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200); |
| 90 | ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200); |
| 91 | ctrlPts.addCircle(pts[4], pts[5], SK_Scalar1 / 200); |
| 92 | } |
| 93 | for (int i = 0; i < 100; ++i) { |
| 94 | SkScalar pts[] = { |
| 95 | rand.nextUScalar1(), rand.nextUScalar1(), |
| 96 | rand.nextUScalar1(), rand.nextUScalar1(), |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 97 | }; |
| 98 | curves.moveTo(pts[0], pts[1]); |
| 99 | curves.lineTo(pts[2], pts[3]); |
| 100 | |
| 101 | ctrlPts.addCircle(pts[0], pts[1], SK_Scalar1 / 200); |
| 102 | ctrlPts.addCircle(pts[2], pts[3], SK_Scalar1 / 200); |
| 103 | } |
| 104 | |
| 105 | paint.setColor(SK_ColorBLACK); |
| 106 | canvas->drawPath(curves, paint); |
| 107 | paint.setColor(SK_ColorRED); |
| 108 | //canvas->drawPath(hulls, paint); |
| 109 | paint.setStyle(SkPaint::kFill_Style); |
| 110 | paint.setColor(SK_ColorBLUE); |
| 111 | //canvas->drawPath(ctrlPts, paint); |
| 112 | |
| 113 | canvas->restore(); |
| 114 | } |
| 115 | |
| 116 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 117 | using INHERITED = Sample; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | ////////////////////////////////////////////////////////////////////////////// |
| 121 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 122 | DEF_SAMPLE( return new HairCurvesView(); ) |