arc.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var _util = require("./util");
  2. var normalizeRadian = _util.normalizeRadian;
  3. var PI2 = Math.PI * 2;
  4. /**
  5. * 圆弧描边包含判断
  6. * @param {number} cx
  7. * @param {number} cy
  8. * @param {number} r
  9. * @param {number} startAngle
  10. * @param {number} endAngle
  11. * @param {boolean} anticlockwise
  12. * @param {number} lineWidth
  13. * @param {number} x
  14. * @param {number} y
  15. * @return {Boolean}
  16. */
  17. function containStroke(cx, cy, r, startAngle, endAngle, anticlockwise, lineWidth, x, y) {
  18. if (lineWidth === 0) {
  19. return false;
  20. }
  21. var _l = lineWidth;
  22. x -= cx;
  23. y -= cy;
  24. var d = Math.sqrt(x * x + y * y);
  25. if (d - _l > r || d + _l < r) {
  26. return false;
  27. }
  28. if (Math.abs(startAngle - endAngle) % PI2 < 1e-4) {
  29. // Is a circle
  30. return true;
  31. }
  32. if (anticlockwise) {
  33. var tmp = startAngle;
  34. startAngle = normalizeRadian(endAngle);
  35. endAngle = normalizeRadian(tmp);
  36. } else {
  37. startAngle = normalizeRadian(startAngle);
  38. endAngle = normalizeRadian(endAngle);
  39. }
  40. if (startAngle > endAngle) {
  41. endAngle += PI2;
  42. }
  43. var angle = Math.atan2(y, x);
  44. if (angle < 0) {
  45. angle += PI2;
  46. }
  47. return angle >= startAngle && angle <= endAngle || angle + PI2 >= startAngle && angle + PI2 <= endAngle;
  48. }
  49. exports.containStroke = containStroke;