Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
| 2 | // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 3 | #include "tools/fiddle/examples.h" |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 4 | // HASH=5de2de0f00354e59074a9bb1a42d5a63 |
Hal Canary | a7181e7c | 2019-03-18 16:06:34 -0400 | [diff] [blame] | 5 | REG_FIDDLE(Miter_Limit, 384, 170, false, 0) { |
Hal Canary | 8751512 | 2019-03-15 14:22:51 -0400 | [diff] [blame] | 6 | void draw(SkCanvas* canvas) { |
| 7 | SkPoint pts[] = {{ 10, 50 }, { 110, 80 }, { 10, 110 }}; |
| 8 | SkVector v[] = { pts[0] - pts[1], pts[2] - pts[1] }; |
| 9 | SkScalar angle1 = SkScalarATan2(v[0].fY, v[0].fX); |
| 10 | SkScalar angle2 = SkScalarATan2(v[1].fY, v[1].fX); |
| 11 | const SkScalar strokeWidth = 20; |
| 12 | SkScalar miterLimit = 1 / SkScalarSin((angle2 - angle1) / 2); |
| 13 | SkScalar miterLength = strokeWidth * miterLimit; |
| 14 | SkPath path; |
| 15 | path.moveTo(pts[0]); |
| 16 | path.lineTo(pts[1]); |
| 17 | path.lineTo(pts[2]); |
| 18 | SkPaint paint; // set to default kMiter_Join |
| 19 | paint.setAntiAlias(true); |
| 20 | paint.setStyle(SkPaint::kStroke_Style); |
| 21 | paint.setStrokeMiter(miterLimit); |
| 22 | paint.setStrokeWidth(strokeWidth); |
| 23 | canvas->drawPath(path, paint); |
| 24 | paint.setStrokeWidth(1); |
| 25 | canvas->drawLine(pts[1].fX - miterLength / 2, pts[1].fY + 50, |
| 26 | pts[1].fX + miterLength / 2, pts[1].fY + 50, paint); |
| 27 | canvas->translate(200, 0); |
| 28 | miterLimit *= 0.99f; |
| 29 | paint.setStrokeMiter(miterLimit); |
| 30 | paint.setStrokeWidth(strokeWidth); |
| 31 | canvas->drawPath(path, paint); |
| 32 | paint.setStrokeWidth(1); |
| 33 | canvas->drawLine(pts[1].fX - miterLength / 2, pts[1].fY + 50, |
| 34 | pts[1].fX + miterLength / 2, pts[1].fY + 50, paint); |
| 35 | } |
| 36 | } // END FIDDLE |