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
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"
23 #include "tools/timer/TimeUtils.h"
24
25 #include "src/core/SkGeometry.h"
26
27 #include <stdlib.h>
28
29 // http://code.google.com/p/skia/issues/detail?id=32
test_cubic()30 static void test_cubic() {
31 SkPoint src[4] = {
32 { 556.25000f, 523.03003f },
33 { 556.23999f, 522.96002f },
34 { 556.21997f, 522.89001f },
35 { 556.21997f, 522.82001f }
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
test_cubic2()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);
54
55 {
56 SkRect r = path.getBounds();
57 SkIRect ir;
58 r.round(&ir);
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);
63 }
64
65 SkBitmap bitmap;
66 bitmap.allocN32Pixels(300, 200);
67
68 SkCanvas canvas(bitmap);
69 SkPaint paint;
70 paint.setAntiAlias(true);
71 canvas.drawPath(path, paint);
72 }
73
74 class PathView : public Sample {
75 SkScalar fPrevSecs;
76 public:
77 SkScalar fDStroke, fStroke, fMinStroke, fMaxStroke;
78 SkPath fPath[6];
79 bool fShowHairline;
80 bool fOnce;
81
PathView()82 PathView() {
83 fPrevSecs = 0;
84 fOnce = false;
85 }
86
init()87 void init() {
88 if (fOnce) {
89 return;
90 }
91 fOnce = true;
92
93 test_cubic();
94 test_cubic2();
95
96 fShowHairline = false;
97
98 fDStroke = 1;
99 fStroke = 10;
100 fMinStroke = 10;
101 fMaxStroke = 180;
102
103 const SkScalar V = 85;
104
105 fPath[0].moveTo(40, 70);
106 fPath[0].lineTo(70, 70 + SK_ScalarHalf);
107 fPath[0].lineTo(110, 70);
108
109 fPath[1].moveTo(40, 70);
110 fPath[1].lineTo(70, 70 - SK_ScalarHalf);
111 fPath[1].lineTo(110, 70);
112
113 fPath[2].moveTo(V, V);
114 fPath[2].lineTo(50, V);
115 fPath[2].lineTo(50, 50);
116
117 fPath[3].moveTo(50, 50);
118 fPath[3].lineTo(50, V);
119 fPath[3].lineTo(V, V);
120
121 fPath[4].moveTo(50, 50);
122 fPath[4].lineTo(50, V);
123 fPath[4].lineTo(52, 50);
124
125 fPath[5].moveTo(52, 50);
126 fPath[5].lineTo(50, V);
127 fPath[5].lineTo(50, 50);
128
129 this->setBGColor(0xFFDDDDDD);
130 }
131
132 protected:
name()133 SkString name() override { return SkString("Paths"); }
134
drawPath(SkCanvas * canvas,const SkPath & path,SkPaint::Join j)135 void drawPath(SkCanvas* canvas, const SkPath& path, SkPaint::Join j) {
136 SkPaint paint;
137
138 paint.setAntiAlias(true);
139 paint.setStyle(SkPaint::kStroke_Style);
140 paint.setStrokeJoin(j);
141 paint.setStrokeWidth(fStroke);
142
143 if (fShowHairline) {
144 SkPath fill;
145
146 paint.getFillPath(path, &fill);
147 paint.setStrokeWidth(0);
148 canvas->drawPath(fill, paint);
149 } else {
150 canvas->drawPath(path, paint);
151 }
152
153 paint.setColor(SK_ColorRED);
154 paint.setStrokeWidth(0);
155 canvas->drawPath(path, paint);
156 }
157
onDrawContent(SkCanvas * canvas)158 void onDrawContent(SkCanvas* canvas) override {
159 this->init();
160 canvas->translate(50, 50);
161
162 static const SkPaint::Join gJoins[] = {
163 SkPaint::kBevel_Join,
164 SkPaint::kMiter_Join,
165 SkPaint::kRound_Join
166 };
167
168 for (size_t i = 0; i < SK_ARRAY_COUNT(gJoins); i++) {
169 canvas->save();
170 for (size_t j = 0; j < SK_ARRAY_COUNT(fPath); j++) {
171 this->drawPath(canvas, fPath[j], gJoins[i]);
172 canvas->translate(200, 0);
173 }
174 canvas->restore();
175
176 canvas->translate(0, 200);
177 }
178 }
179
onAnimate(double nanos)180 bool onAnimate(double nanos) override {
181 SkScalar currSecs = TimeUtils::Scaled(1e-9 * nanos, 100);
182 SkScalar delta = currSecs - fPrevSecs;
183 fPrevSecs = currSecs;
184
185 fStroke += fDStroke * delta;
186 if (fStroke > fMaxStroke || fStroke < fMinStroke) {
187 fDStroke = -fDStroke;
188 }
189 return true;
190 }
191
onFindClickHandler(SkScalar x,SkScalar y,ModifierKey modi)192 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
193 fShowHairline = !fShowHairline;
194 return nullptr;
195 }
196
197 private:
198 typedef Sample INHERITED;
199 };
200 DEF_SAMPLE( return new PathView; )
201
202 //////////////////////////////////////////////////////////////////////////////
203
204 #include "include/effects/SkCornerPathEffect.h"
205 #include "include/utils/SkRandom.h"
206
207 class ArcToView : public Sample {
208 bool fDoFrame, fDoCorner, fDoConic;
209 SkPaint fPtsPaint, fSkeletonPaint, fCornerPaint;
210 public:
211 enum {
212 N = 4
213 };
214 SkPoint fPts[N];
215
ArcToView()216 ArcToView()
217 : fDoFrame(false), fDoCorner(false), fDoConic(false)
218 {
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 }
224
225 const SkScalar rad = 50;
226
227 fPtsPaint.setAntiAlias(true);
228 fPtsPaint.setStrokeWidth(15);
229 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
230
231 fCornerPaint.setAntiAlias(true);
232 fCornerPaint.setStyle(SkPaint::kStroke_Style);
233 fCornerPaint.setStrokeWidth(13);
234 fCornerPaint.setColor(SK_ColorGREEN);
235 fCornerPaint.setPathEffect(SkCornerPathEffect::Make(rad*2));
236
237 fSkeletonPaint.setAntiAlias(true);
238 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
239 fSkeletonPaint.setColor(SK_ColorRED);
240 }
241
toggle(bool & value)242 void toggle(bool& value) {
243 value = !value;
244 }
245
246 protected:
name()247 SkString name() override { return SkString("ArcTo"); }
248
onChar(SkUnichar uni)249 bool onChar(SkUnichar uni) override {
250 switch (uni) {
251 case '1': this->toggle(fDoFrame); return true;
252 case '2': this->toggle(fDoCorner); return true;
253 case '3': this->toggle(fDoConic); return true;
254 default: break;
255 }
256 return false;
257 }
258
makePath(SkPath * path)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 }
268
onDrawContent(SkCanvas * canvas)269 void onDrawContent(SkCanvas* canvas) override {
270 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, fPts, fPtsPaint);
271
272 SkPath path;
273 this->makePath(&path);
274
275 if (fDoCorner) {
276 canvas->drawPath(path, fCornerPaint);
277 }
278
279 canvas->drawPath(path, fSkeletonPaint);
280 }
281
onClick(Click * click)282 bool onClick(Click* click) override {
283 int32_t index;
284 if (click->fMeta.findS32("index", &index)) {
285 SkASSERT((unsigned)index < N);
286 fPts[index] = click->fCurr;
287 return true;
288 }
289 return false;
290 }
291
onFindClickHandler(SkScalar x,SkScalar y,ModifierKey modi)292 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
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))) {
297 Click* click = new Click();
298 click->fMeta.setS32("index", i);
299 return click;
300 }
301 }
302 return nullptr;
303 }
304
305 private:
306 typedef Sample INHERITED;
307 };
308 DEF_SAMPLE( return new ArcToView; )
309
310 /////////////
311
312 class FatStroke : public Sample {
313 bool fClosed, fShowStroke, fShowHidden, fShowSkeleton;
314 int fJoinType, fCapType;
315 float fWidth = 30;
316 SkPaint fPtsPaint, fHiddenPaint, fSkeletonPaint, fStrokePaint;
317 public:
318 enum {
319 N = 4
320 };
321 SkPoint fPts[N];
322
FatStroke()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 }
331
332 fPtsPaint.setAntiAlias(true);
333 fPtsPaint.setStrokeWidth(10);
334 fPtsPaint.setStrokeCap(SkPaint::kRound_Cap);
335
336 fHiddenPaint.setAntiAlias(true);
337 fHiddenPaint.setStyle(SkPaint::kStroke_Style);
338 fHiddenPaint.setColor(0xFF0000FF);
339
340 fStrokePaint.setAntiAlias(true);
341 fStrokePaint.setStyle(SkPaint::kStroke_Style);
342 fStrokePaint.setStrokeWidth(50);
343 fStrokePaint.setColor(0x8000FF00);
344
345 fSkeletonPaint.setAntiAlias(true);
346 fSkeletonPaint.setStyle(SkPaint::kStroke_Style);
347 fSkeletonPaint.setColor(SK_ColorRED);
348 }
349
toggle(bool & value)350 void toggle(bool& value) {
351 value = !value;
352 }
353
toggle3(int & value)354 void toggle3(int& value) {
355 value = (value + 1) % 3;
356 }
357
358 protected:
name()359 SkString name() override { return SkString("FatStroke"); }
360
onChar(SkUnichar uni)361 bool onChar(SkUnichar uni) override {
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;
369 case '-': fWidth -= 5; return true;
370 case '=': fWidth += 5; return true;
371 default: break;
372 }
373 return false;
374 }
375
makePath(SkPath * path)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 }
385
onDrawContent(SkCanvas * canvas)386 void onDrawContent(SkCanvas* canvas) override {
387 canvas->drawColor(0xFFEEEEEE);
388
389 SkPath path;
390 this->makePath(&path);
391
392 fStrokePaint.setStrokeWidth(fWidth);
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 }
409
onClick(Click * click)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;
415 return true;
416 }
417 return false;
418 }
419
onFindClickHandler(SkScalar x,SkScalar y,ModifierKey modi)420 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
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))) {
425 Click* click = new Click();
426 click->fMeta.setS32("index", i);
427 return click;
428 }
429 }
430 return nullptr;
431 }
432
433 private:
434 typedef Sample INHERITED;
435 };
DEF_SAMPLE(return new FatStroke;)436 DEF_SAMPLE( return new FatStroke; )
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
459 class CubicCurve : public Sample {
460 public:
461 enum {
462 N = 4
463 };
464 SkPoint fPts[N];
465
CubicCurve()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:
name()475 SkString name() override { return SkString("CubicCurve"); }
476
onDrawContent(SkCanvas * canvas)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
onClick(Click * click)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
onFindClickHandler(SkScalar x,SkScalar y,ModifierKey modi)537 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
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))) {
542 Click* click = new Click();
543 click->fMeta.setS32("index", i);
544 return click;
545 }
546 }
547 return this->INHERITED::onFindClickHandler(x, y, modi);
548 }
549
550 private:
551 typedef Sample INHERITED;
552 };
DEF_SAMPLE(return new CubicCurve;)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
find_max_deviation_cubic(const SkPoint src[4],SkScalar ts[2])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
574 class CubicCurve2 : public Sample {
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;
583 bool fShowFlatness = false;
584 SkScalar fScale = 0.75;
585
CubicCurve2()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:
name()598 SkString name() override { return SkString("CubicCurve2"); }
599
onChar(SkUnichar uni)600 bool onChar(SkUnichar uni) override {
601 switch (uni) {
602 case 's': fShowSub = !fShowSub; break;
603 case 'f': fShowFlatness = !fShowFlatness; break;
604 case '-': fT -= 1.0f / 32; break;
605 case '=': fT += 1.0f / 32; break;
606 default: return false;
607 }
608 fT = std::min(1.0f, std::max(0.0f, fT));
609 return true;
610 }
611
showFrame(SkCanvas * canvas,const SkPoint pts[],int count,const SkPaint & p)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 }
634
showFlattness(SkCanvas * canvas)635 void showFlattness(SkCanvas* canvas) {
636 SkPaint paint;
637 paint.setStyle(SkPaint::kStroke_Style);
638 paint.setAntiAlias(true);
639
640 SkPaint paint2(paint);
641 paint2.setColor(0xFF008800);
642
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
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;
657 SkVector v = (v0 + v1) * 0.5f;
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);
664 canvas->drawLine(anchor, anchor + v, paint2);
665 canvas->drawLine(anchor, anchor + v0, paint);
666 if (n == 2) {
667 SkEvalCubicAt(fPts, ts[1], &anchor, nullptr, nullptr);
668 canvas->drawLine(anchor, anchor + v, paint2);
669 }
670 canvas->drawLine(anchor, anchor + v1, paint);
671 }
672 // not sure we can get here
673 }
674
onDrawContent(SkCanvas * canvas)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
695 paint.setColor(SK_ColorBLACK);
696 paint.setStyle(SkPaint::kFill_Style);
697 SkFont font(nullptr, 20);
698 canvas->drawString(SkStringPrintf("t = %g", fT), 20, 20, font, paint);
699 }
700
701 if (fShowFlatness) {
702 this->showFlattness(canvas);
703 }
704
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 }
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
721 }
722
onClick(Click * click)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
onFindClickHandler(SkScalar x,SkScalar y,ModifierKey modi)733 Sample::Click* onFindClickHandler(SkScalar x, SkScalar y, ModifierKey modi) override {
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))) {
738 Click* click = new Click();
739 click->fMeta.setS32("index", i);
740 return click;
741 }
742 }
743 return this->INHERITED::onFindClickHandler(x, y, modi);
744 }
745
746 private:
747 typedef Sample INHERITED;
748 };
749 DEF_SAMPLE( return new CubicCurve2; )
750
751