arc.ts 1.1 KB

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