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