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 "Sample.h" 8 #include "SkCanvas.h" 9 #include "SkPath.h" 10 #include "SkRandom.h" 11 12 class HairCurvesView : public Sample { 13 public: 14 HairCurvesView() { 15 } 16 17 protected: 18 virtual bool onQuery(Sample::Event* evt) { 19 if (Sample::TitleQ(*evt)) { 20 Sample::TitleR(evt, "HairCurves"); 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); 31 paint.setStrokeWidth(0); 32 canvas->save(); 33 canvas->scale(1000 * SK_Scalar1, 1000 * SK_Scalar1); 34 SkRandom rand; 35 SkRandom randW; 36 SkPath curves; 37 SkPath hulls; 38 SkPath ctrlPts; 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(), 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(), 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: 124 typedef Sample INHERITED; 125 }; 126 127 ////////////////////////////////////////////////////////////////////////////// 128 129 DEF_SAMPLE( return new HairCurvesView(); ) 130