epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +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 | */ |
| 7 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 8 | #include "Sample.h" |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 9 | #include "SkAnimTimer.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 10 | #include "SkBitmap.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkCanvas.h" |
| 12 | #include "SkGradientShader.h" |
| 13 | #include "SkGraphics.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkPath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkRegion.h" |
| 16 | #include "SkShader.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 17 | #include "SkUTF.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 18 | #include "SkColorPriv.h" |
| 19 | #include "SkColorFilter.h" |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 20 | #include "SkParsePath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | #include "SkTime.h" |
| 22 | #include "SkTypeface.h" |
| 23 | |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 24 | #include "SkGeometry.h" |
| 25 | |
bungeman | 60e0fee | 2015-08-26 05:15:46 -0700 | [diff] [blame] | 26 | #include <stdlib.h> |
| 27 | |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 28 | // http://code.google.com/p/skia/issues/detail?id=32 |
| 29 | static void test_cubic() { |
| 30 | SkPoint src[4] = { |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 31 | { 556.25000f, 523.03003f }, |
| 32 | { 556.23999f, 522.96002f }, |
| 33 | { 556.21997f, 522.89001f }, |
| 34 | { 556.21997f, 522.82001f } |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 35 | }; |
| 36 | SkPoint dst[11]; |
| 37 | dst[10].set(42, -42); // one past the end, that we don't clobber these |
| 38 | SkScalar tval[] = { 0.33333334f, 0.99999994f }; |
| 39 | |
| 40 | SkChopCubicAt(src, dst, tval, 2); |
| 41 | |
| 42 | #if 0 |
| 43 | for (int i = 0; i < 11; i++) { |
| 44 | SkDebugf("--- %d [%g %g]\n", i, dst[i].fX, dst[i].fY); |
| 45 | } |
| 46 | #endif |
| 47 | } |
| 48 | |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 49 | static void test_cubic2() { |
| 50 | const char* str = "M2242 -590088L-377758 9.94099e+07L-377758 9.94099e+07L2242 -590088Z"; |
| 51 | SkPath path; |
| 52 | SkParsePath::FromSVGString(str, &path); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 54 | { |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 55 | SkRect r = path.getBounds(); |
| 56 | SkIRect ir; |
| 57 | r.round(&ir); |
bungeman@google.com | fab44db | 2013-10-11 18:50:45 +0000 | [diff] [blame] | 58 | SkDebugf("[%g %g %g %g] [%x %x %x %x]\n", |
| 59 | SkScalarToDouble(r.fLeft), SkScalarToDouble(r.fTop), |
| 60 | SkScalarToDouble(r.fRight), SkScalarToDouble(r.fBottom), |
| 61 | ir.fLeft, ir.fTop, ir.fRight, ir.fBottom); |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 62 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 63 | |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 64 | SkBitmap bitmap; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 65 | bitmap.allocN32Pixels(300, 200); |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 66 | |
| 67 | SkCanvas canvas(bitmap); |
| 68 | SkPaint paint; |
| 69 | paint.setAntiAlias(true); |
| 70 | canvas.drawPath(path, paint); |
| 71 | } |
| 72 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 73 | class PathView : public Sample { |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 74 | SkScalar fPrevSecs; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | public: |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 76 | SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | SkPath fPath[6]; |
| 78 | bool fShowHairline; |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 79 | bool fOnce; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 80 | |
| 81 | PathView() { |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 82 | fPrevSecs = 0; |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 83 | fOnce = false; |
| 84 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 85 | |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 86 | void init() { |
| 87 | if (fOnce) { |
| 88 | return; |
| 89 | } |
| 90 | fOnce = true; |
| 91 | |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 92 | test_cubic(); |
reed@android.com | d0d0e65 | 2009-10-13 13:31:27 +0000 | [diff] [blame] | 93 | test_cubic2(); |
reed@android.com | a964028 | 2009-08-28 20:06:54 +0000 | [diff] [blame] | 94 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | fShowHairline = false; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | fDStroke = 1; |
| 98 | fStroke = 10; |
| 99 | fMinStroke = 10; |
| 100 | fMaxStroke = 180; |
| 101 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 102 | const SkScalar V = 85; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 103 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 104 | fPath[0].moveTo(40, 70); |
| 105 | fPath[0].lineTo(70, 70 + SK_ScalarHalf); |
| 106 | fPath[0].lineTo(110, 70); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 107 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 108 | fPath[1].moveTo(40, 70); |
| 109 | fPath[1].lineTo(70, 70 - SK_ScalarHalf); |
| 110 | fPath[1].lineTo(110, 70); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 111 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 112 | fPath[2].moveTo(V, V); |
| 113 | fPath[2].lineTo(50, V); |
| 114 | fPath[2].lineTo(50, 50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 115 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 116 | fPath[3].moveTo(50, 50); |
| 117 | fPath[3].lineTo(50, V); |
| 118 | fPath[3].lineTo(V, V); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 119 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 120 | fPath[4].moveTo(50, 50); |
| 121 | fPath[4].lineTo(50, V); |
| 122 | fPath[4].lineTo(52, 50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 123 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 124 | fPath[5].moveTo(52, 50); |
| 125 | fPath[5].lineTo(50, V); |
| 126 | fPath[5].lineTo(50, 50); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 127 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 128 | this->setBGColor(0xFFDDDDDD); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 129 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 130 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 131 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 132 | bool onQuery(Sample::Event* evt) override { |
| 133 | if (Sample::TitleQ(*evt)) { |
| 134 | Sample::TitleR(evt, "Paths"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | return true; |
| 136 | } |
| 137 | return this->INHERITED::onQuery(evt); |
| 138 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 139 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 140 | void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | SkPaint paint; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 142 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | paint.setAntiAlias(true); |
| 144 | paint.setStyle(SkPaint::kStroke_Style); |
| 145 | paint.setStrokeJoin(j); |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 146 | paint.setStrokeWidth(fStroke); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 148 | if (fShowHairline) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | SkPath fill; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 150 | |
| 151 | paint.getFillPath(path, &fill); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | paint.setStrokeWidth(0); |
| 153 | canvas->drawPath(fill, paint); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 154 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | canvas->drawPath(path, paint); |
reed@google.com | 0faac1e | 2011-05-11 05:58:58 +0000 | [diff] [blame] | 156 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 157 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | paint.setColor(SK_ColorRED); |
| 159 | paint.setStrokeWidth(0); |
| 160 | canvas->drawPath(path, paint); |
| 161 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 162 | |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 163 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | c9fa63c | 2012-03-12 21:14:09 +0000 | [diff] [blame] | 164 | this->init(); |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 165 | canvas->translate(50, 50); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 166 | |
| 167 | static const SkPaint::Join gJoins[] = { |
| 168 | SkPaint::kBevel_Join, |
| 169 | SkPaint::kMiter_Join, |
| 170 | SkPaint::kRound_Join |
| 171 | }; |
| 172 | |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 173 | for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | canvas->save(); |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 175 | for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | this->drawPath(canvas, fPath[j], gJoins[i]); |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 177 | canvas->translate(200, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | } |
| 179 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 180 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 181 | canvas->translate(0, 200); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | } |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 183 | } |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 184 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 185 | bool onAnimate(const SkAnimTimer& timer) override { |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 186 | SkScalar currSecs = timer.scaled(100); |
| 187 | SkScalar delta = currSecs - fPrevSecs; |
| 188 | fPrevSecs = currSecs; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 189 | |
reed | 339cdbf | 2015-02-05 22:02:37 -0800 | [diff] [blame] | 190 | fStroke += fDStroke * delta; |
| 191 | if (fStroke > fMaxStroke || fStroke < fMinStroke) { |
| 192 | fDStroke = -fDStroke; |
| 193 | } |
| 194 | return true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 196 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 197 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 198 | fShowHairline = !fShowHairline; |
reed@google.com | 4d5c26d | 2013-01-08 16:17:50 +0000 | [diff] [blame] | 199 | return this->INHERITED::onFindClickHandler(x, y, modi); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 200 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 201 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 202 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 203 | typedef Sample INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | }; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 205 | DEF_SAMPLE( return new PathView; ) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 206 | |
| 207 | ////////////////////////////////////////////////////////////////////////////// |
| 208 | |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 209 | #include "SkCornerPathEffect.h" |
| 210 | #include "SkRandom.h" |
| 211 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 212 | class ArcToView : public Sample { |
Mike Reed | 40e7e65 | 2017-07-22 22:12:59 -0400 | [diff] [blame] | 213 | bool fDoFrame, fDoCorner, fDoConic; |
| 214 | SkPaint fPtsPaint, fSkeletonPaint, fCornerPaint; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 215 | public: |
| 216 | enum { |
| 217 | N = 4 |
| 218 | }; |
| 219 | SkPoint fPts[N]; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 220 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 221 | ArcToView() |
Mike Reed | 40e7e65 | 2017-07-22 22:12:59 -0400 | [diff] [blame] | 222 | : fDoFrame(false), fDoCorner(false), fDoConic(false) |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 223 | { |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 224 | SkRandom rand; |
| 225 | for (int i = 0; i < N; ++i) { |
| 226 | fPts[i].fX = 20 + rand.nextUScalar1() * 640; |
| 227 | fPts[i].fY = 20 + rand.nextUScalar1() * 480; |
| 228 | } |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 229 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 230 | const SkScalar rad = 50; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 231 | |
| 232 | fPtsPaint.setAntiAlias(true); |
| 233 | fPtsPaint.setStrokeWidth(15); |
| 234 | fPtsPaint.setStrokeCap(SkPaint::kRound_Cap); |
| 235 | |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 236 | fCornerPaint.setAntiAlias(true); |
| 237 | fCornerPaint.setStyle(SkPaint::kStroke_Style); |
| 238 | fCornerPaint.setStrokeWidth(13); |
| 239 | fCornerPaint.setColor(SK_ColorGREEN); |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 240 | fCornerPaint.setPathEffect(SkCornerPathEffect::Make(rad*2)); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 241 | |
| 242 | fSkeletonPaint.setAntiAlias(true); |
| 243 | fSkeletonPaint.setStyle(SkPaint::kStroke_Style); |
| 244 | fSkeletonPaint.setColor(SK_ColorRED); |
| 245 | } |
| 246 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 247 | void toggle(bool& value) { |
| 248 | value = !value; |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 249 | } |
| 250 | |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 251 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 252 | bool onQuery(Sample::Event* evt) override { |
| 253 | if (Sample::TitleQ(*evt)) { |
| 254 | Sample::TitleR(evt, "ArcTo"); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 255 | return true; |
| 256 | } |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 257 | SkUnichar uni; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 258 | if (Sample::CharQ(*evt, &uni)) { |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 259 | switch (uni) { |
| 260 | case '1': this->toggle(fDoFrame); return true; |
Mike Reed | 40e7e65 | 2017-07-22 22:12:59 -0400 | [diff] [blame] | 261 | case '2': this->toggle(fDoCorner); return true; |
| 262 | case '3': this->toggle(fDoConic); return true; |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 263 | default: break; |
| 264 | } |
| 265 | } |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 266 | return this->INHERITED::onQuery(evt); |
| 267 | } |
mtklein | f059900 | 2015-07-13 06:18:39 -0700 | [diff] [blame] | 268 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 269 | void makePath(SkPath* path) { |
| 270 | path->moveTo(fPts[0]); |
| 271 | for (int i = 1; i < N; ++i) { |
| 272 | path->lineTo(fPts[i]); |
| 273 | } |
| 274 | if (!fDoFrame) { |
| 275 | path->close(); |
| 276 | } |
| 277 | } |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 278 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 279 | void onDrawContent(SkCanvas* canvas) override { |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 280 | canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint); |
| 281 | |
| 282 | SkPath path; |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 283 | this->makePath(&path); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 284 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 285 | if (fDoCorner) { |
| 286 | canvas->drawPath(path, fCornerPaint); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 287 | } |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 288 | |
reed | 8b57524 | 2014-12-17 01:47:32 -0800 | [diff] [blame] | 289 | canvas->drawPath(path, fSkeletonPaint); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 290 | } |
| 291 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 292 | bool onClick(Click* click) override { |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 293 | int32_t index; |
| 294 | if (click->fMeta.findS32("index", &index)) { |
| 295 | SkASSERT((unsigned)index < N); |
| 296 | fPts[index] = click->fCurr; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 297 | return true; |
| 298 | } |
| 299 | return false; |
| 300 | } |
| 301 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 302 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 303 | const SkScalar tol = 4; |
| 304 | const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); |
| 305 | for (int i = 0; i < N; ++i) { |
| 306 | if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { |
| 307 | Click* click = new Click(this); |
| 308 | click->fMeta.setS32("index", i); |
| 309 | return click; |
| 310 | } |
| 311 | } |
| 312 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 313 | } |
| 314 | |
| 315 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 316 | typedef Sample INHERITED; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 317 | }; |
| 318 | DEF_SAMPLE( return new ArcToView; ) |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 319 | |
| 320 | ///////////// |
| 321 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 322 | class FatStroke : public Sample { |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 323 | bool fClosed, fShowStroke, fShowHidden, fShowSkeleton; |
| 324 | int fJoinType, fCapType; |
Mike Reed | 1fba702 | 2016-12-15 10:23:42 -0500 | [diff] [blame] | 325 | float fWidth = 30; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 326 | SkPaint fPtsPaint, fHiddenPaint, fSkeletonPaint, fStrokePaint; |
| 327 | public: |
| 328 | enum { |
| 329 | N = 4 |
| 330 | }; |
| 331 | SkPoint fPts[N]; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 332 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 333 | FatStroke() : fClosed(false), fShowStroke(true), fShowHidden(false), fShowSkeleton(true), |
| 334 | fJoinType(0), fCapType(0) |
| 335 | { |
| 336 | SkRandom rand; |
| 337 | for (int i = 0; i < N; ++i) { |
| 338 | fPts[i].fX = 20 + rand.nextUScalar1() * 640; |
| 339 | fPts[i].fY = 20 + rand.nextUScalar1() * 480; |
| 340 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 341 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 342 | fPtsPaint.setAntiAlias(true); |
| 343 | fPtsPaint.setStrokeWidth(10); |
| 344 | fPtsPaint.setStrokeCap(SkPaint::kRound_Cap); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 345 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 346 | fHiddenPaint.setAntiAlias(true); |
| 347 | fHiddenPaint.setStyle(SkPaint::kStroke_Style); |
| 348 | fHiddenPaint.setColor(0xFF0000FF); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 349 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 350 | fStrokePaint.setAntiAlias(true); |
| 351 | fStrokePaint.setStyle(SkPaint::kStroke_Style); |
| 352 | fStrokePaint.setStrokeWidth(50); |
| 353 | fStrokePaint.setColor(0x8000FF00); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 354 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 355 | fSkeletonPaint.setAntiAlias(true); |
| 356 | fSkeletonPaint.setStyle(SkPaint::kStroke_Style); |
| 357 | fSkeletonPaint.setColor(SK_ColorRED); |
| 358 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 359 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 360 | void toggle(bool& value) { |
| 361 | value = !value; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 362 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 363 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 364 | void toggle3(int& value) { |
| 365 | value = (value + 1) % 3; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 366 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 367 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 368 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 369 | bool onQuery(Sample::Event* evt) override { |
| 370 | if (Sample::TitleQ(*evt)) { |
| 371 | Sample::TitleR(evt, "FatStroke"); |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 372 | return true; |
| 373 | } |
| 374 | SkUnichar uni; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 375 | if (Sample::CharQ(*evt, &uni)) { |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 376 | switch (uni) { |
| 377 | case '1': this->toggle(fShowSkeleton); return true; |
| 378 | case '2': this->toggle(fShowStroke); return true; |
| 379 | case '3': this->toggle(fShowHidden); return true; |
| 380 | case '4': this->toggle3(fJoinType); return true; |
| 381 | case '5': this->toggle3(fCapType); return true; |
| 382 | case '6': this->toggle(fClosed); return true; |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 383 | case '-': fWidth -= 5; return true; |
| 384 | case '=': fWidth += 5; return true; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 385 | default: break; |
| 386 | } |
| 387 | } |
| 388 | return this->INHERITED::onQuery(evt); |
| 389 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 390 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 391 | void makePath(SkPath* path) { |
| 392 | path->moveTo(fPts[0]); |
| 393 | for (int i = 1; i < N; ++i) { |
| 394 | path->lineTo(fPts[i]); |
| 395 | } |
| 396 | if (fClosed) { |
| 397 | path->close(); |
| 398 | } |
| 399 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 400 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 401 | void onDrawContent(SkCanvas* canvas) override { |
| 402 | canvas->drawColor(0xFFEEEEEE); |
| 403 | |
| 404 | SkPath path; |
| 405 | this->makePath(&path); |
| 406 | |
Mike Reed | 1fba702 | 2016-12-15 10:23:42 -0500 | [diff] [blame] | 407 | fStrokePaint.setStrokeWidth(fWidth); |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 408 | fStrokePaint.setStrokeJoin((SkPaint::Join)fJoinType); |
| 409 | fStrokePaint.setStrokeCap((SkPaint::Cap)fCapType); |
| 410 | |
| 411 | if (fShowStroke) { |
| 412 | canvas->drawPath(path, fStrokePaint); |
| 413 | } |
| 414 | if (fShowHidden) { |
| 415 | SkPath hidden; |
| 416 | fStrokePaint.getFillPath(path, &hidden); |
| 417 | canvas->drawPath(hidden, fHiddenPaint); |
| 418 | } |
| 419 | if (fShowSkeleton) { |
| 420 | canvas->drawPath(path, fSkeletonPaint); |
| 421 | } |
| 422 | canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint); |
| 423 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 424 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 425 | bool onClick(Click* click) override { |
| 426 | int32_t index; |
| 427 | if (click->fMeta.findS32("index", &index)) { |
| 428 | SkASSERT((unsigned)index < N); |
| 429 | fPts[index] = click->fCurr; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 430 | return true; |
| 431 | } |
| 432 | return false; |
| 433 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 434 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 435 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 436 | const SkScalar tol = 4; |
| 437 | const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); |
| 438 | for (int i = 0; i < N; ++i) { |
| 439 | if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { |
| 440 | Click* click = new Click(this); |
| 441 | click->fMeta.setS32("index", i); |
| 442 | return click; |
| 443 | } |
| 444 | } |
| 445 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 446 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 447 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 448 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 449 | typedef Sample INHERITED; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 450 | }; |
| 451 | DEF_SAMPLE( return new FatStroke; ) |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 452 | |
| 453 | static int compute_parallel_to_base(const SkPoint pts[4], SkScalar t[2]) { |
| 454 | // F = At^3 + Bt^2 + Ct + D |
| 455 | SkVector A = pts[3] - pts[0] + (pts[1] - pts[2]) * 3.0f; |
| 456 | SkVector B = (pts[0] - pts[1] - pts[1] + pts[2]) * 3.0f; |
| 457 | SkVector C = (pts[1] - pts[0]) * 3.0f; |
| 458 | SkVector DA = pts[3] - pts[0]; |
| 459 | |
| 460 | // F' = 3At^2 + 2Bt + C |
| 461 | SkScalar a = 3 * A.cross(DA); |
| 462 | SkScalar b = 2 * B.cross(DA); |
| 463 | SkScalar c = C.cross(DA); |
| 464 | |
| 465 | int n = SkFindUnitQuadRoots(a, b, c, t); |
| 466 | SkString str; |
| 467 | for (int i = 0; i < n; ++i) { |
| 468 | str.appendf(" %g", t[i]); |
| 469 | } |
| 470 | SkDebugf("roots %s\n", str.c_str()); |
| 471 | return n; |
| 472 | } |
| 473 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 474 | class CubicCurve : public Sample { |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 475 | public: |
| 476 | enum { |
| 477 | N = 4 |
| 478 | }; |
| 479 | SkPoint fPts[N]; |
| 480 | |
| 481 | CubicCurve() { |
| 482 | SkRandom rand; |
| 483 | for (int i = 0; i < N; ++i) { |
| 484 | fPts[i].fX = 20 + rand.nextUScalar1() * 640; |
| 485 | fPts[i].fY = 20 + rand.nextUScalar1() * 480; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 490 | bool onQuery(Sample::Event* evt) override { |
| 491 | if (Sample::TitleQ(*evt)) { |
| 492 | Sample::TitleR(evt, "CubicCurve"); |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 493 | return true; |
| 494 | } |
| 495 | return this->INHERITED::onQuery(evt); |
| 496 | } |
| 497 | |
| 498 | void onDrawContent(SkCanvas* canvas) override { |
| 499 | SkPaint paint; |
| 500 | paint.setAntiAlias(true); |
| 501 | |
| 502 | { |
| 503 | SkPath path; |
| 504 | path.moveTo(fPts[0]); |
| 505 | path.cubicTo(fPts[1], fPts[2], fPts[3]); |
| 506 | paint.setStyle(SkPaint::kStroke_Style); |
| 507 | canvas->drawPath(path, paint); |
| 508 | } |
| 509 | |
| 510 | { |
| 511 | paint.setColor(SK_ColorRED); |
| 512 | SkScalar t[2]; |
| 513 | int n = compute_parallel_to_base(fPts, t); |
| 514 | SkPoint loc; |
| 515 | SkVector tan; |
| 516 | for (int i = 0; i < n; ++i) { |
| 517 | SkEvalCubicAt(fPts, t[i], &loc, &tan, nullptr); |
| 518 | tan.setLength(30); |
| 519 | canvas->drawLine(loc - tan, loc + tan, paint); |
| 520 | } |
| 521 | paint.setStrokeWidth(0.5f); |
| 522 | canvas->drawLine(fPts[0], fPts[3], paint); |
| 523 | |
| 524 | paint.setColor(SK_ColorBLUE); |
| 525 | paint.setStrokeWidth(6); |
| 526 | SkEvalCubicAt(fPts, 0.5f, &loc, nullptr, nullptr); |
| 527 | canvas->drawPoint(loc, paint); |
| 528 | |
| 529 | paint.setColor(0xFF008800); |
| 530 | SkEvalCubicAt(fPts, 1.0f/3, &loc, nullptr, nullptr); |
| 531 | canvas->drawPoint(loc, paint); |
| 532 | SkEvalCubicAt(fPts, 2.0f/3, &loc, nullptr, nullptr); |
| 533 | canvas->drawPoint(loc, paint); |
| 534 | |
| 535 | // n = SkFindCubicInflections(fPts, t); |
| 536 | // printf("inflections %d %g %g\n", n, t[0], t[1]); |
| 537 | } |
| 538 | |
| 539 | { |
| 540 | paint.setStyle(SkPaint::kFill_Style); |
| 541 | paint.setColor(SK_ColorRED); |
| 542 | for (SkPoint p : fPts) { |
| 543 | canvas->drawCircle(p.fX, p.fY, 8, paint); |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | bool onClick(Click* click) override { |
| 549 | int32_t index; |
| 550 | if (click->fMeta.findS32("index", &index)) { |
| 551 | SkASSERT((unsigned)index < N); |
| 552 | fPts[index] = click->fCurr; |
| 553 | return true; |
| 554 | } |
| 555 | return false; |
| 556 | } |
| 557 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 558 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 559 | const SkScalar tol = 8; |
| 560 | const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); |
| 561 | for (int i = 0; i < N; ++i) { |
| 562 | if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { |
| 563 | Click* click = new Click(this); |
| 564 | click->fMeta.setS32("index", i); |
| 565 | return click; |
| 566 | } |
| 567 | } |
| 568 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 569 | } |
| 570 | |
| 571 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 572 | typedef Sample INHERITED; |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 573 | }; |
| 574 | DEF_SAMPLE( return new CubicCurve; ) |
| 575 | |
| 576 | static SkPoint lerp(SkPoint a, SkPoint b, float t) { |
| 577 | return a * (1 - t) + b * t; |
| 578 | } |
| 579 | |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 580 | static int find_max_deviation_cubic(const SkPoint src[4], SkScalar ts[2]) { |
| 581 | // deviation = F' x (d - a) == 0, solve for t(s) |
| 582 | // F = At^3 + Bt^2 + Ct + D |
| 583 | // F' = 3At^2 + 2Bt + C |
| 584 | // Z = d - a |
| 585 | // F' x Z = 3(A x Z)t^2 + 2(B x Z)t + (C x Z) |
| 586 | // |
| 587 | SkVector A = src[3] + (src[1] - src[2]) * 3 - src[0]; |
| 588 | SkVector B = (src[2] - src[1] - src[1] + src[0]) * 3; |
| 589 | SkVector C = (src[1] - src[0]) * 3; |
| 590 | SkVector Z = src[3] - src[0]; |
| 591 | // now forumlate the quadratic coefficients we need to solve for t : F' x Z |
| 592 | return SkFindUnitQuadRoots(3 * A.cross(Z), 2 * B.cross(Z), C.cross(Z), ts); |
| 593 | } |
| 594 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 595 | class CubicCurve2 : public Sample { |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 596 | public: |
| 597 | enum { |
| 598 | N = 7 |
| 599 | }; |
| 600 | SkPoint fPts[N]; |
| 601 | SkPoint* fQuad = fPts + 4; |
| 602 | SkScalar fT = 0.5f; |
| 603 | bool fShowSub = false; |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 604 | bool fShowFlatness = false; |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 605 | SkScalar fScale = 0.75; |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 606 | |
| 607 | CubicCurve2() { |
| 608 | fPts[0] = { 90, 300 }; |
| 609 | fPts[1] = { 30, 60 }; |
| 610 | fPts[2] = { 250, 30 }; |
| 611 | fPts[3] = { 350, 200 }; |
| 612 | |
| 613 | fQuad[0] = fPts[0] + SkVector{ 300, 0}; |
| 614 | fQuad[1] = fPts[1] + SkVector{ 300, 0}; |
| 615 | fQuad[2] = fPts[2] + SkVector{ 300, 0}; |
| 616 | } |
| 617 | |
| 618 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 619 | bool onQuery(Sample::Event* evt) override { |
| 620 | if (Sample::TitleQ(*evt)) { |
| 621 | Sample::TitleR(evt, "CubicCurve2"); |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 622 | return true; |
| 623 | } |
| 624 | SkUnichar uni; |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 625 | if (Sample::CharQ(*evt, &uni)) { |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 626 | switch (uni) { |
| 627 | case 's': fShowSub = !fShowSub; break; |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 628 | case 'f': fShowFlatness = !fShowFlatness; break; |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 629 | case '-': fT -= 1.0f / 32; break; |
| 630 | case '=': fT += 1.0f / 32; break; |
| 631 | default: goto DONE; |
| 632 | } |
| 633 | fT = std::min(1.0f, std::max(0.0f, fT)); |
| 634 | return true; |
| 635 | } |
| 636 | DONE: |
| 637 | return this->INHERITED::onQuery(evt); |
| 638 | } |
| 639 | |
| 640 | void showFrame(SkCanvas* canvas, const SkPoint pts[], int count, const SkPaint& p) { |
| 641 | SkPaint paint(p); |
| 642 | SkPoint storage[3 + 2 + 1]; |
| 643 | SkPoint* tmp = storage; |
| 644 | const SkPoint* prev = pts; |
| 645 | int n = count; |
| 646 | for (int n = count; n > 0; --n) { |
| 647 | for (int i = 0; i < n; ++i) { |
| 648 | canvas->drawLine(prev[i], prev[i+1], paint); |
| 649 | tmp[i] = lerp(prev[i], prev[i+1], fT); |
| 650 | } |
| 651 | prev = tmp; |
| 652 | tmp += n; |
| 653 | } |
| 654 | |
| 655 | paint.setColor(SK_ColorBLUE); |
| 656 | paint.setStyle(SkPaint::kFill_Style); |
| 657 | n = tmp - storage; |
| 658 | for (int i = 0; i < n; ++i) { |
| 659 | canvas->drawCircle(storage[i].fX, storage[i].fY, 4, paint); |
| 660 | } |
| 661 | } |
Hal Canary | d499817 | 2018-07-11 11:31:34 -0400 | [diff] [blame] | 662 | |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 663 | void showFlattness(SkCanvas* canvas) { |
| 664 | SkPaint paint; |
| 665 | paint.setStyle(SkPaint::kStroke_Style); |
| 666 | paint.setAntiAlias(true); |
| 667 | |
Mike Reed | dccfa35 | 2018-04-17 17:10:51 -0400 | [diff] [blame] | 668 | SkPaint paint2(paint); |
| 669 | paint2.setColor(0xFF008800); |
| 670 | |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 671 | paint.setColor(0xFF888888); |
| 672 | canvas->drawLine(fPts[0], fPts[3], paint); |
| 673 | canvas->drawLine(fQuad[0], fQuad[2], paint); |
| 674 | |
| 675 | paint.setColor(0xFF0000FF); |
| 676 | SkPoint pts[2]; |
| 677 | pts[0] = (fQuad[0] + fQuad[1] + fQuad[1] + fQuad[2])*0.25; |
| 678 | pts[1] = (fQuad[0] + fQuad[2]) * 0.5; |
| 679 | canvas->drawLine(pts[0], pts[1], paint); |
| 680 | |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 681 | // cubic |
| 682 | |
| 683 | SkVector v0 = (fPts[0] - fPts[1] - fPts[1] + fPts[2]) * fScale; |
| 684 | SkVector v1 = (fPts[1] - fPts[2] - fPts[2] + fPts[3]) * fScale; |
Mike Reed | dccfa35 | 2018-04-17 17:10:51 -0400 | [diff] [blame] | 685 | SkVector v = (v0 + v1) * 0.5f; |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 686 | |
| 687 | SkPoint anchor; |
| 688 | SkScalar ts[2]; |
| 689 | int n = find_max_deviation_cubic(fPts, ts); |
| 690 | if (n > 0) { |
| 691 | SkEvalCubicAt(fPts, ts[0], &anchor, nullptr, nullptr); |
Mike Reed | dccfa35 | 2018-04-17 17:10:51 -0400 | [diff] [blame] | 692 | canvas->drawLine(anchor, anchor + v, paint2); |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 693 | canvas->drawLine(anchor, anchor + v0, paint); |
| 694 | if (n == 2) { |
| 695 | SkEvalCubicAt(fPts, ts[1], &anchor, nullptr, nullptr); |
Mike Reed | dccfa35 | 2018-04-17 17:10:51 -0400 | [diff] [blame] | 696 | canvas->drawLine(anchor, anchor + v, paint2); |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 697 | } |
| 698 | canvas->drawLine(anchor, anchor + v1, paint); |
| 699 | } |
| 700 | // not sure we can get here |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 701 | } |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 702 | |
| 703 | void onDrawContent(SkCanvas* canvas) override { |
| 704 | SkPaint paint; |
| 705 | paint.setAntiAlias(true); |
| 706 | |
| 707 | { |
| 708 | paint.setStyle(SkPaint::kStroke_Style); |
| 709 | SkPath path; |
| 710 | path.moveTo(fPts[0]); |
| 711 | path.cubicTo(fPts[1], fPts[2], fPts[3]); |
| 712 | path.moveTo(fQuad[0]); |
| 713 | path.quadTo(fQuad[1], fQuad[2]); |
| 714 | canvas->drawPath(path, paint); |
| 715 | } |
| 716 | |
| 717 | if (fShowSub) { |
| 718 | paint.setColor(SK_ColorRED); |
| 719 | paint.setStrokeWidth(1.7f); |
| 720 | this->showFrame(canvas, fPts, 3, paint); |
| 721 | this->showFrame(canvas, fQuad, 2, paint); |
| 722 | |
| 723 | SkString str; |
| 724 | str.printf("t = %g", fT); |
| 725 | paint.setColor(SK_ColorBLACK); |
| 726 | paint.setStyle(SkPaint::kFill_Style); |
| 727 | paint.setTextSize(20); |
| 728 | canvas->drawText(str.c_str(), str.size(), 20, 20, paint); |
| 729 | } |
| 730 | |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 731 | if (fShowFlatness) { |
| 732 | this->showFlattness(canvas); |
| 733 | } |
| 734 | |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 735 | paint.setStyle(SkPaint::kFill_Style); |
| 736 | paint.setColor(SK_ColorRED); |
| 737 | for (SkPoint p : fPts) { |
| 738 | canvas->drawCircle(p.fX, p.fY, 7, paint); |
| 739 | } |
Mike Reed | fb65db1 | 2018-04-19 19:18:35 -0400 | [diff] [blame] | 740 | |
| 741 | { |
| 742 | SkScalar ts[2]; |
| 743 | int n = SkFindCubicInflections(fPts, ts); |
| 744 | for (int i = 0; i < n; ++i) { |
| 745 | SkPoint p; |
| 746 | SkEvalCubicAt(fPts, ts[i], &p, nullptr, nullptr); |
| 747 | canvas->drawCircle(p.fX, p.fY, 3, paint); |
| 748 | } |
| 749 | } |
| 750 | |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | bool onClick(Click* click) override { |
| 754 | int32_t index; |
| 755 | if (click->fMeta.findS32("index", &index)) { |
| 756 | SkASSERT((unsigned)index < N); |
| 757 | fPts[index] = click->fCurr; |
| 758 | return true; |
| 759 | } |
| 760 | return false; |
| 761 | } |
| 762 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 763 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) override { |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 764 | const SkScalar tol = 8; |
| 765 | const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); |
| 766 | for (int i = 0; i < N; ++i) { |
| 767 | if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { |
| 768 | Click* click = new Click(this); |
| 769 | click->fMeta.setS32("index", i); |
| 770 | return click; |
| 771 | } |
| 772 | } |
| 773 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 774 | } |
| 775 | |
| 776 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 777 | typedef Sample INHERITED; |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 778 | }; |
| 779 | DEF_SAMPLE( return new CubicCurve2; ) |
| 780 | |