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