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