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" |
Mike Reed | 65dc579 | 2020-10-23 16:25:41 -0400 | [diff] [blame] | 14 | #include "include/core/SkPathBuilder.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 198 | using INHERITED = Sample; |
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 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 282 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 283 | const SkScalar tol = 4; |
| 284 | const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); |
| 285 | for (int i = 0; i < N; ++i) { |
| 286 | if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { |
Mike Reed | 0ce3e88 | 2020-04-22 11:09:30 -0400 | [diff] [blame] | 287 | return new Click([this, i](Click* c) { |
| 288 | fPts[i] = c->fCurr; |
| 289 | return true; |
| 290 | }); |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 293 | return nullptr; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 297 | using INHERITED = Sample; |
reed | a7a8b10 | 2014-12-16 08:07:43 -0800 | [diff] [blame] | 298 | }; |
| 299 | DEF_SAMPLE( return new ArcToView; ) |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 300 | |
| 301 | ///////////// |
| 302 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 303 | class FatStroke : public Sample { |
Mike Reed | 439fe60 | 2020-06-26 14:33:13 -0400 | [diff] [blame] | 304 | bool fClosed, fShowStroke, fShowHidden, fShowSkeleton, fAsCurves = false; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 305 | int fJoinType, fCapType; |
Mike Reed | 1fba702 | 2016-12-15 10:23:42 -0500 | [diff] [blame] | 306 | float fWidth = 30; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 307 | SkPaint fPtsPaint, fHiddenPaint, fSkeletonPaint, fStrokePaint; |
| 308 | public: |
| 309 | enum { |
| 310 | N = 4 |
| 311 | }; |
| 312 | SkPoint fPts[N]; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 313 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 314 | FatStroke() : fClosed(false), fShowStroke(true), fShowHidden(false), fShowSkeleton(true), |
| 315 | fJoinType(0), fCapType(0) |
| 316 | { |
| 317 | SkRandom rand; |
| 318 | for (int i = 0; i < N; ++i) { |
| 319 | fPts[i].fX = 20 + rand.nextUScalar1() * 640; |
| 320 | fPts[i].fY = 20 + rand.nextUScalar1() * 480; |
| 321 | } |
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 | fPtsPaint.setAntiAlias(true); |
| 324 | fPtsPaint.setStrokeWidth(10); |
| 325 | fPtsPaint.setStrokeCap(SkPaint::kRound_Cap); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 326 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 327 | fHiddenPaint.setAntiAlias(true); |
| 328 | fHiddenPaint.setStyle(SkPaint::kStroke_Style); |
| 329 | fHiddenPaint.setColor(0xFF0000FF); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 330 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 331 | fStrokePaint.setAntiAlias(true); |
| 332 | fStrokePaint.setStyle(SkPaint::kStroke_Style); |
| 333 | fStrokePaint.setStrokeWidth(50); |
| 334 | fStrokePaint.setColor(0x8000FF00); |
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 | fSkeletonPaint.setAntiAlias(true); |
| 337 | fSkeletonPaint.setStyle(SkPaint::kStroke_Style); |
| 338 | fSkeletonPaint.setColor(SK_ColorRED); |
| 339 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 340 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 341 | void toggle(bool& value) { |
| 342 | value = !value; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 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 | void toggle3(int& value) { |
| 346 | value = (value + 1) % 3; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 347 | } |
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 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 350 | SkString name() override { return SkString("FatStroke"); } |
| 351 | |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 352 | bool onChar(SkUnichar uni) override { |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 353 | switch (uni) { |
| 354 | case '1': this->toggle(fShowSkeleton); return true; |
| 355 | case '2': this->toggle(fShowStroke); return true; |
| 356 | case '3': this->toggle(fShowHidden); return true; |
| 357 | case '4': this->toggle3(fJoinType); return true; |
| 358 | case '5': this->toggle3(fCapType); return true; |
| 359 | case '6': this->toggle(fClosed); return true; |
Mike Reed | 439fe60 | 2020-06-26 14:33:13 -0400 | [diff] [blame] | 360 | case 'c': this->toggle(fAsCurves); return true; |
Brian Osman | ede860e | 2017-11-22 16:36:07 -0500 | [diff] [blame] | 361 | case '-': fWidth -= 5; return true; |
| 362 | case '=': fWidth += 5; return true; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 363 | default: break; |
| 364 | } |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 365 | return false; |
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 | void makePath(SkPath* path) { |
| 369 | path->moveTo(fPts[0]); |
Mike Reed | 439fe60 | 2020-06-26 14:33:13 -0400 | [diff] [blame] | 370 | if (fAsCurves) { |
| 371 | for (int i = 1; i < N-2; ++i) { |
| 372 | path->quadTo(fPts[i], (fPts[i+1] + fPts[i]) * 0.5f); |
| 373 | } |
| 374 | path->quadTo(fPts[N-2], fPts[N-1]); |
| 375 | } else { |
| 376 | for (int i = 1; i < N; ++i) { |
| 377 | path->lineTo(fPts[i]); |
| 378 | } |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 379 | } |
| 380 | if (fClosed) { |
| 381 | path->close(); |
| 382 | } |
| 383 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 384 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 385 | void onDrawContent(SkCanvas* canvas) override { |
| 386 | canvas->drawColor(0xFFEEEEEE); |
| 387 | |
| 388 | SkPath path; |
| 389 | this->makePath(&path); |
| 390 | |
Mike Reed | 1fba702 | 2016-12-15 10:23:42 -0500 | [diff] [blame] | 391 | fStrokePaint.setStrokeWidth(fWidth); |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 392 | fStrokePaint.setStrokeJoin((SkPaint::Join)fJoinType); |
| 393 | fStrokePaint.setStrokeCap((SkPaint::Cap)fCapType); |
| 394 | |
| 395 | if (fShowStroke) { |
| 396 | canvas->drawPath(path, fStrokePaint); |
| 397 | } |
| 398 | if (fShowHidden) { |
| 399 | SkPath hidden; |
| 400 | fStrokePaint.getFillPath(path, &hidden); |
| 401 | canvas->drawPath(hidden, fHiddenPaint); |
| 402 | } |
| 403 | if (fShowSkeleton) { |
| 404 | canvas->drawPath(path, fSkeletonPaint); |
| 405 | } |
| 406 | canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint); |
| 407 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 408 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 409 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 410 | const SkScalar tol = 4; |
| 411 | const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); |
| 412 | for (int i = 0; i < N; ++i) { |
| 413 | if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { |
Mike Reed | 0ce3e88 | 2020-04-22 11:09:30 -0400 | [diff] [blame] | 414 | return new Click([this, i](Click* c) { |
| 415 | fPts[i] = c->fCurr; |
| 416 | return true; |
| 417 | }); |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 418 | } |
| 419 | } |
Hal Canary | fcf6359 | 2019-07-12 11:32:43 -0400 | [diff] [blame] | 420 | return nullptr; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 421 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 422 | |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 423 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 424 | using INHERITED = Sample; |
Mike Reed | a964a29 | 2016-11-02 22:09:25 -0400 | [diff] [blame] | 425 | }; |
| 426 | DEF_SAMPLE( return new FatStroke; ) |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 427 | |
| 428 | static int compute_parallel_to_base(const SkPoint pts[4], SkScalar t[2]) { |
| 429 | // F = At^3 + Bt^2 + Ct + D |
| 430 | SkVector A = pts[3] - pts[0] + (pts[1] - pts[2]) * 3.0f; |
| 431 | SkVector B = (pts[0] - pts[1] - pts[1] + pts[2]) * 3.0f; |
| 432 | SkVector C = (pts[1] - pts[0]) * 3.0f; |
| 433 | SkVector DA = pts[3] - pts[0]; |
| 434 | |
| 435 | // F' = 3At^2 + 2Bt + C |
| 436 | SkScalar a = 3 * A.cross(DA); |
| 437 | SkScalar b = 2 * B.cross(DA); |
| 438 | SkScalar c = C.cross(DA); |
| 439 | |
| 440 | int n = SkFindUnitQuadRoots(a, b, c, t); |
| 441 | SkString str; |
| 442 | for (int i = 0; i < n; ++i) { |
| 443 | str.appendf(" %g", t[i]); |
| 444 | } |
| 445 | SkDebugf("roots %s\n", str.c_str()); |
| 446 | return n; |
| 447 | } |
| 448 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 449 | class CubicCurve : public Sample { |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 450 | public: |
| 451 | enum { |
| 452 | N = 4 |
| 453 | }; |
| 454 | SkPoint fPts[N]; |
| 455 | |
| 456 | CubicCurve() { |
| 457 | SkRandom rand; |
| 458 | for (int i = 0; i < N; ++i) { |
| 459 | fPts[i].fX = 20 + rand.nextUScalar1() * 640; |
| 460 | fPts[i].fY = 20 + rand.nextUScalar1() * 480; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 465 | SkString name() override { return SkString("CubicCurve"); } |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 466 | |
| 467 | void onDrawContent(SkCanvas* canvas) override { |
| 468 | SkPaint paint; |
| 469 | paint.setAntiAlias(true); |
| 470 | |
| 471 | { |
| 472 | SkPath path; |
| 473 | path.moveTo(fPts[0]); |
| 474 | path.cubicTo(fPts[1], fPts[2], fPts[3]); |
| 475 | paint.setStyle(SkPaint::kStroke_Style); |
| 476 | canvas->drawPath(path, paint); |
| 477 | } |
| 478 | |
| 479 | { |
| 480 | paint.setColor(SK_ColorRED); |
| 481 | SkScalar t[2]; |
| 482 | int n = compute_parallel_to_base(fPts, t); |
| 483 | SkPoint loc; |
| 484 | SkVector tan; |
| 485 | for (int i = 0; i < n; ++i) { |
| 486 | SkEvalCubicAt(fPts, t[i], &loc, &tan, nullptr); |
| 487 | tan.setLength(30); |
| 488 | canvas->drawLine(loc - tan, loc + tan, paint); |
| 489 | } |
| 490 | paint.setStrokeWidth(0.5f); |
| 491 | canvas->drawLine(fPts[0], fPts[3], paint); |
| 492 | |
| 493 | paint.setColor(SK_ColorBLUE); |
| 494 | paint.setStrokeWidth(6); |
| 495 | SkEvalCubicAt(fPts, 0.5f, &loc, nullptr, nullptr); |
| 496 | canvas->drawPoint(loc, paint); |
| 497 | |
| 498 | paint.setColor(0xFF008800); |
| 499 | SkEvalCubicAt(fPts, 1.0f/3, &loc, nullptr, nullptr); |
| 500 | canvas->drawPoint(loc, paint); |
| 501 | SkEvalCubicAt(fPts, 2.0f/3, &loc, nullptr, nullptr); |
| 502 | canvas->drawPoint(loc, paint); |
| 503 | |
| 504 | // n = SkFindCubicInflections(fPts, t); |
| 505 | // printf("inflections %d %g %g\n", n, t[0], t[1]); |
| 506 | } |
| 507 | |
| 508 | { |
| 509 | paint.setStyle(SkPaint::kFill_Style); |
| 510 | paint.setColor(SK_ColorRED); |
| 511 | for (SkPoint p : fPts) { |
| 512 | canvas->drawCircle(p.fX, p.fY, 8, paint); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 517 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 518 | const SkScalar tol = 8; |
| 519 | const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); |
| 520 | for (int i = 0; i < N; ++i) { |
| 521 | if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { |
Mike Reed | 0ce3e88 | 2020-04-22 11:09:30 -0400 | [diff] [blame] | 522 | return new Click([this, i](Click* c) { |
| 523 | fPts[i] = c->fCurr; |
| 524 | return true; |
| 525 | }); |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 529 | } |
| 530 | |
| 531 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 532 | using INHERITED = Sample; |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 533 | }; |
| 534 | DEF_SAMPLE( return new CubicCurve; ) |
| 535 | |
| 536 | static SkPoint lerp(SkPoint a, SkPoint b, float t) { |
| 537 | return a * (1 - t) + b * t; |
| 538 | } |
| 539 | |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 540 | static int find_max_deviation_cubic(const SkPoint src[4], SkScalar ts[2]) { |
| 541 | // deviation = F' x (d - a) == 0, solve for t(s) |
| 542 | // F = At^3 + Bt^2 + Ct + D |
| 543 | // F' = 3At^2 + 2Bt + C |
| 544 | // Z = d - a |
| 545 | // F' x Z = 3(A x Z)t^2 + 2(B x Z)t + (C x Z) |
| 546 | // |
| 547 | SkVector A = src[3] + (src[1] - src[2]) * 3 - src[0]; |
| 548 | SkVector B = (src[2] - src[1] - src[1] + src[0]) * 3; |
| 549 | SkVector C = (src[1] - src[0]) * 3; |
| 550 | SkVector Z = src[3] - src[0]; |
| 551 | // now forumlate the quadratic coefficients we need to solve for t : F' x Z |
| 552 | return SkFindUnitQuadRoots(3 * A.cross(Z), 2 * B.cross(Z), C.cross(Z), ts); |
| 553 | } |
| 554 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 555 | class CubicCurve2 : public Sample { |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 556 | public: |
| 557 | enum { |
| 558 | N = 7 |
| 559 | }; |
| 560 | SkPoint fPts[N]; |
| 561 | SkPoint* fQuad = fPts + 4; |
| 562 | SkScalar fT = 0.5f; |
| 563 | bool fShowSub = false; |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 564 | bool fShowFlatness = false; |
Mike Reed | 65dc579 | 2020-10-23 16:25:41 -0400 | [diff] [blame] | 565 | bool fShowInnerQuads = false; |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 566 | SkScalar fScale = 0.75; |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 567 | |
| 568 | CubicCurve2() { |
| 569 | fPts[0] = { 90, 300 }; |
| 570 | fPts[1] = { 30, 60 }; |
| 571 | fPts[2] = { 250, 30 }; |
| 572 | fPts[3] = { 350, 200 }; |
| 573 | |
| 574 | fQuad[0] = fPts[0] + SkVector{ 300, 0}; |
| 575 | fQuad[1] = fPts[1] + SkVector{ 300, 0}; |
| 576 | fQuad[2] = fPts[2] + SkVector{ 300, 0}; |
| 577 | } |
| 578 | |
| 579 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 580 | SkString name() override { return SkString("CubicCurve2"); } |
| 581 | |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 582 | bool onChar(SkUnichar uni) override { |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 583 | switch (uni) { |
| 584 | case 's': fShowSub = !fShowSub; break; |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 585 | case 'f': fShowFlatness = !fShowFlatness; break; |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 586 | case '-': fT -= 1.0f / 32; break; |
| 587 | case '=': fT += 1.0f / 32; break; |
Mike Reed | 65dc579 | 2020-10-23 16:25:41 -0400 | [diff] [blame] | 588 | case 'q': fShowInnerQuads = !fShowInnerQuads; break; |
Hal Canary | 6cc65e1 | 2019-07-03 15:53:04 -0400 | [diff] [blame] | 589 | default: return false; |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 590 | } |
| 591 | fT = std::min(1.0f, std::max(0.0f, fT)); |
| 592 | return true; |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 593 | } |
| 594 | |
Mike Reed | 65dc579 | 2020-10-23 16:25:41 -0400 | [diff] [blame] | 595 | static void Dot(SkCanvas* canvas, SkPoint p, SkScalar radius, SkColor c) { |
| 596 | SkPaint paint; |
| 597 | paint.setAntiAlias(true); |
| 598 | paint.setColor(c); |
| 599 | canvas->drawCircle(p.fX, p.fY, radius, paint); |
| 600 | } |
| 601 | |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 602 | void showFrame(SkCanvas* canvas, const SkPoint pts[], int count, const SkPaint& p) { |
| 603 | SkPaint paint(p); |
| 604 | SkPoint storage[3 + 2 + 1]; |
| 605 | SkPoint* tmp = storage; |
| 606 | const SkPoint* prev = pts; |
| 607 | int n = count; |
| 608 | for (int n = count; n > 0; --n) { |
| 609 | for (int i = 0; i < n; ++i) { |
| 610 | canvas->drawLine(prev[i], prev[i+1], paint); |
| 611 | tmp[i] = lerp(prev[i], prev[i+1], fT); |
| 612 | } |
| 613 | prev = tmp; |
| 614 | tmp += n; |
| 615 | } |
| 616 | |
| 617 | paint.setColor(SK_ColorBLUE); |
| 618 | paint.setStyle(SkPaint::kFill_Style); |
| 619 | n = tmp - storage; |
| 620 | for (int i = 0; i < n; ++i) { |
Mike Reed | 65dc579 | 2020-10-23 16:25:41 -0400 | [diff] [blame] | 621 | Dot(canvas, storage[i], 4, SK_ColorBLUE); |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 622 | } |
| 623 | } |
Hal Canary | d499817 | 2018-07-11 11:31:34 -0400 | [diff] [blame] | 624 | |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 625 | void showFlattness(SkCanvas* canvas) { |
| 626 | SkPaint paint; |
| 627 | paint.setStyle(SkPaint::kStroke_Style); |
| 628 | paint.setAntiAlias(true); |
| 629 | |
Mike Reed | dccfa35 | 2018-04-17 17:10:51 -0400 | [diff] [blame] | 630 | SkPaint paint2(paint); |
| 631 | paint2.setColor(0xFF008800); |
| 632 | |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 633 | paint.setColor(0xFF888888); |
| 634 | canvas->drawLine(fPts[0], fPts[3], paint); |
| 635 | canvas->drawLine(fQuad[0], fQuad[2], paint); |
| 636 | |
| 637 | paint.setColor(0xFF0000FF); |
| 638 | SkPoint pts[2]; |
| 639 | pts[0] = (fQuad[0] + fQuad[1] + fQuad[1] + fQuad[2])*0.25; |
| 640 | pts[1] = (fQuad[0] + fQuad[2]) * 0.5; |
| 641 | canvas->drawLine(pts[0], pts[1], paint); |
| 642 | |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 643 | // cubic |
| 644 | |
| 645 | SkVector v0 = (fPts[0] - fPts[1] - fPts[1] + fPts[2]) * fScale; |
| 646 | SkVector v1 = (fPts[1] - fPts[2] - fPts[2] + fPts[3]) * fScale; |
Mike Reed | dccfa35 | 2018-04-17 17:10:51 -0400 | [diff] [blame] | 647 | SkVector v = (v0 + v1) * 0.5f; |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 648 | |
| 649 | SkPoint anchor; |
| 650 | SkScalar ts[2]; |
| 651 | int n = find_max_deviation_cubic(fPts, ts); |
| 652 | if (n > 0) { |
| 653 | SkEvalCubicAt(fPts, ts[0], &anchor, nullptr, nullptr); |
Mike Reed | dccfa35 | 2018-04-17 17:10:51 -0400 | [diff] [blame] | 654 | canvas->drawLine(anchor, anchor + v, paint2); |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 655 | canvas->drawLine(anchor, anchor + v0, paint); |
| 656 | if (n == 2) { |
| 657 | SkEvalCubicAt(fPts, ts[1], &anchor, nullptr, nullptr); |
Mike Reed | dccfa35 | 2018-04-17 17:10:51 -0400 | [diff] [blame] | 658 | canvas->drawLine(anchor, anchor + v, paint2); |
Mike Reed | 83dc473 | 2018-04-16 17:07:07 -0400 | [diff] [blame] | 659 | } |
| 660 | canvas->drawLine(anchor, anchor + v1, paint); |
| 661 | } |
| 662 | // not sure we can get here |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 663 | } |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 664 | |
Mike Reed | 65dc579 | 2020-10-23 16:25:41 -0400 | [diff] [blame] | 665 | void showInnerQuads(SkCanvas* canvas) { |
| 666 | auto draw_quad = [canvas](SkPoint a, SkPoint b, SkPoint c, SkColor color) { |
| 667 | SkPaint paint; |
| 668 | paint.setAntiAlias(true); |
| 669 | paint.setStroke(true); |
| 670 | paint.setColor(color); |
| 671 | |
| 672 | canvas->drawPath(SkPathBuilder().moveTo(a).quadTo(b, c).detach(), paint); |
| 673 | }; |
| 674 | |
| 675 | SkPoint p0 = SkEvalQuadAt(&fPts[0], fT), |
| 676 | p1 = SkEvalQuadAt(&fPts[1], fT), |
| 677 | p2 = lerp(p0, p1, fT); |
| 678 | |
| 679 | draw_quad(fPts[0], fPts[1], fPts[2], SK_ColorRED); |
| 680 | Dot(canvas, p0, 4, SK_ColorRED); |
| 681 | |
| 682 | draw_quad(fPts[1], fPts[2], fPts[3], SK_ColorBLUE); |
| 683 | Dot(canvas, p1, 4, SK_ColorBLUE); |
| 684 | |
| 685 | SkPaint paint; |
| 686 | paint.setAntiAlias(true); |
| 687 | paint.setColor(0xFF008800); |
| 688 | canvas->drawLine(p0, p1, paint); |
| 689 | Dot(canvas, p2, 4, 0xFF00AA00); |
| 690 | } |
| 691 | |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 692 | void onDrawContent(SkCanvas* canvas) override { |
| 693 | SkPaint paint; |
| 694 | paint.setAntiAlias(true); |
| 695 | |
| 696 | { |
| 697 | paint.setStyle(SkPaint::kStroke_Style); |
| 698 | SkPath path; |
| 699 | path.moveTo(fPts[0]); |
| 700 | path.cubicTo(fPts[1], fPts[2], fPts[3]); |
| 701 | path.moveTo(fQuad[0]); |
| 702 | path.quadTo(fQuad[1], fQuad[2]); |
| 703 | canvas->drawPath(path, paint); |
| 704 | } |
| 705 | |
| 706 | if (fShowSub) { |
| 707 | paint.setColor(SK_ColorRED); |
| 708 | paint.setStrokeWidth(1.7f); |
| 709 | this->showFrame(canvas, fPts, 3, paint); |
| 710 | this->showFrame(canvas, fQuad, 2, paint); |
| 711 | |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 712 | paint.setColor(SK_ColorBLACK); |
| 713 | paint.setStyle(SkPaint::kFill_Style); |
Hal Canary | 4484b8f | 2019-01-08 14:00:08 -0500 | [diff] [blame] | 714 | SkFont font(nullptr, 20); |
| 715 | canvas->drawString(SkStringPrintf("t = %g", fT), 20, 20, font, paint); |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 716 | } |
| 717 | |
Mike Reed | 48bf6ab | 2018-04-15 20:04:45 -0400 | [diff] [blame] | 718 | if (fShowFlatness) { |
| 719 | this->showFlattness(canvas); |
| 720 | } |
| 721 | |
Mike Reed | 65dc579 | 2020-10-23 16:25:41 -0400 | [diff] [blame] | 722 | if (fShowInnerQuads) { |
| 723 | this->showInnerQuads(canvas); |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 724 | } |
Mike Reed | fb65db1 | 2018-04-19 19:18:35 -0400 | [diff] [blame] | 725 | |
Mike Reed | 65dc579 | 2020-10-23 16:25:41 -0400 | [diff] [blame] | 726 | paint.setColor(SK_ColorGRAY); |
| 727 | paint.setStroke(true); |
| 728 | canvas->drawPath(SkPathBuilder().addPolygon(fPts, 4, false).detach(), paint); |
| 729 | canvas->drawPath(SkPathBuilder().addPolygon(fQuad, 3, false).detach(), paint); |
| 730 | |
| 731 | for (SkPoint p : fPts) { |
| 732 | Dot(canvas, p, 7, SK_ColorBLACK); |
| 733 | } |
| 734 | |
| 735 | if (false) { |
Mike Reed | fb65db1 | 2018-04-19 19:18:35 -0400 | [diff] [blame] | 736 | SkScalar ts[2]; |
| 737 | int n = SkFindCubicInflections(fPts, ts); |
| 738 | for (int i = 0; i < n; ++i) { |
| 739 | SkPoint p; |
| 740 | SkEvalCubicAt(fPts, ts[i], &p, nullptr, nullptr); |
| 741 | canvas->drawCircle(p.fX, p.fY, 3, paint); |
| 742 | } |
| 743 | } |
| 744 | |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 745 | } |
| 746 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 747 | Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey modi) override { |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 748 | const SkScalar tol = 8; |
| 749 | const SkRect r = SkRect::MakeXYWH(x - tol, y - tol, tol * 2, tol * 2); |
| 750 | for (int i = 0; i < N; ++i) { |
| 751 | if (r.intersects(SkRect::MakeXYWH(fPts[i].fX, fPts[i].fY, 1, 1))) { |
Mike Reed | 0ce3e88 | 2020-04-22 11:09:30 -0400 | [diff] [blame] | 752 | return new Click([this, i](Click* c) { |
| 753 | fPts[i] = c->fCurr; |
| 754 | return true; |
| 755 | }); |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | return this->INHERITED::onFindClickHandler(x, y, modi); |
| 759 | } |
| 760 | |
| 761 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 762 | using INHERITED = Sample; |
Mike Reed | 66fa456 | 2018-04-06 19:56:50 -0400 | [diff] [blame] | 763 | }; |
| 764 | DEF_SAMPLE( return new CubicCurve2; ) |
| 765 | |