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 */ 7 #include "include/core/SkCanvas.h" 8 #include "include/core/SkPath.h" 9 #include "include/utils/SkRandom.h" 10 #include "samplecode/Sample.h" 11 12 class HairCurvesView : public Sample { 13 public: HairCurvesView()14 HairCurvesView() { 15 } 16 17 protected: name()18 SkString name() override { return SkString("HairCurves"); } 19 onDrawContent(SkCanvas * canvas)20 void onDrawContent(SkCanvas* canvas) override { 21 SkPaint paint; 22 paint.setAntiAlias(true); 23 paint.setStyle(SkPaint::kStroke_Style); 24 paint.setStrokeWidth(0); 25 canvas->save(); 26 canvas->scale(1000 * SK_Scalar1, 1000 * SK_Scalar1); 27 SkRandom rand; 28 SkRandom randW; 29 SkPath curves; 30 SkPath hulls; 31 SkPath ctrlPts; 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(), 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(), 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: 117 typedef Sample INHERITED; 118 }; 119 120 ////////////////////////////////////////////////////////////////////////////// 121 122 DEF_SAMPLE( return new HairCurvesView(); ) 123