Debian Codegraph @

docs/examples/Miter_Limit.cpp

raw
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.
3 #include "tools/fiddle/examples.h"
4 REG_FIDDLE(Miter_Limit, 384, 170, false, 0) {
5 void draw(SkCanvas* canvas) {
6 SkPoint pts[] = {{ 10, 50 }, { 110, 80 }, { 10, 110 }};
7 SkVector v[] = { pts[0] - pts[1], pts[2] - pts[1] };
8 SkScalar angle1 = SkScalarATan2(v[0].fY, v[0].fX);
9 SkScalar angle2 = SkScalarATan2(v[1].fY, v[1].fX);
10 const SkScalar strokeWidth = 20;
11 SkScalar miterLimit = 1 / SkScalarSin((angle2 - angle1) / 2);
12 SkScalar miterLength = strokeWidth * miterLimit;
13 SkPath path = SkPath::Polygon(pts, false);
14 SkPaint paint; // set to default kMiter_Join
15 paint.setAntiAlias(true);
16 paint.setStyle(SkPaint::kStroke_Style);
17 paint.setStrokeMiter(miterLimit);
18 paint.setStrokeWidth(strokeWidth);
19 canvas->drawPath(path, paint);
20 paint.setStrokeWidth(1);
21 canvas->drawLine(pts[1].fX - miterLength / 2, pts[1].fY + 50,
22 pts[1].fX + miterLength / 2, pts[1].fY + 50, paint);
23 canvas->translate(200, 0);
24 miterLimit *= 0.99f;
25 paint.setStrokeMiter(miterLimit);
26 paint.setStrokeWidth(strokeWidth);
27 canvas->drawPath(path, paint);
28 paint.setStrokeWidth(1);
29 canvas->drawLine(pts[1].fX - miterLength / 2, pts[1].fY + 50,
30 pts[1].fX + miterLength / 2, pts[1].fY + 50, paint);
31 }
32 } // END FIDDLE