xref: /aosp_15_r20/external/skia/docs/examples/Miter_Limit.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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) {
draw(SkCanvas * canvas)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;
14     path.moveTo(pts[0]);
15     path.lineTo(pts[1]);
16     path.lineTo(pts[2]);
17     SkPaint paint;  // set to default kMiter_Join
18     paint.setAntiAlias(true);
19     paint.setStyle(SkPaint::kStroke_Style);
20     paint.setStrokeMiter(miterLimit);
21     paint.setStrokeWidth(strokeWidth);
22     canvas->drawPath(path, paint);
23     paint.setStrokeWidth(1);
24     canvas->drawLine(pts[1].fX - miterLength / 2, pts[1].fY + 50,
25                      pts[1].fX + miterLength / 2, pts[1].fY + 50, paint);
26     canvas->translate(200, 0);
27     miterLimit *= 0.99f;
28     paint.setStrokeMiter(miterLimit);
29     paint.setStrokeWidth(strokeWidth);
30     canvas->drawPath(path, paint);
31     paint.setStrokeWidth(1);
32     canvas->drawLine(pts[1].fX - miterLength / 2, pts[1].fY + 50,
33                      pts[1].fX + miterLength / 2, pts[1].fY + 50, paint);
34 }
35 }  // END FIDDLE
36