line.js 837 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * 线段包含判断
  3. * @param {number} x0
  4. * @param {number} y0
  5. * @param {number} x1
  6. * @param {number} y1
  7. * @param {number} lineWidth
  8. * @param {number} x
  9. * @param {number} y
  10. * @return {boolean}
  11. */
  12. function containStroke(x0, y0, x1, y1, lineWidth, x, y) {
  13. if (lineWidth === 0) {
  14. return false;
  15. }
  16. var _l = lineWidth;
  17. var _a = 0;
  18. var _b = x0; // Quick reject
  19. if (y > y0 + _l && y > y1 + _l || y < y0 - _l && y < y1 - _l || x > x0 + _l && x > x1 + _l || x < x0 - _l && x < x1 - _l) {
  20. return false;
  21. }
  22. if (x0 !== x1) {
  23. _a = (y0 - y1) / (x0 - x1);
  24. _b = (x0 * y1 - x1 * y0) / (x0 - x1);
  25. } else {
  26. return Math.abs(x - x0) <= _l / 2;
  27. }
  28. var tmp = _a * x - y + _b;
  29. var _s = tmp * tmp / (_a * _a + 1);
  30. return _s <= _l / 2 * _l / 2;
  31. }
  32. exports.containStroke = containStroke;