Miter_Limit.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  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. // HASH=5de2de0f00354e59074a9bb1a42d5a63
  5. REG_FIDDLE(Miter_Limit, 384, 170, false, 0) {
  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