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