blob: b71f9747181e5aee175a2efe5c9553d81f2fd8ab [file] [log] [blame]
Hal Canary87515122019-03-15 14:22:51 -04001// 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 Kleinc0bd9f92019-04-23 12:05:21 -05003#include "tools/fiddle/examples.h"
Hal Canary87515122019-03-15 14:22:51 -04004// HASH=5de2de0f00354e59074a9bb1a42d5a63
Hal Canarya7181e7c2019-03-18 16:06:34 -04005REG_FIDDLE(Miter_Limit, 384, 170, false, 0) {
Hal Canary87515122019-03-15 14:22:51 -04006void 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