line.js 641 B

123456789101112131415161718192021222324
  1. export function containStroke(x0, y0, x1, y1, lineWidth, x, y) {
  2. if (lineWidth === 0) {
  3. return false;
  4. }
  5. var _l = lineWidth;
  6. var _a = 0;
  7. var _b = x0;
  8. if ((y > y0 + _l && y > y1 + _l)
  9. || (y < y0 - _l && y < y1 - _l)
  10. || (x > x0 + _l && x > x1 + _l)
  11. || (x < x0 - _l && x < x1 - _l)) {
  12. return false;
  13. }
  14. if (x0 !== x1) {
  15. _a = (y0 - y1) / (x0 - x1);
  16. _b = (x0 * y1 - x1 * y0) / (x0 - x1);
  17. }
  18. else {
  19. return Math.abs(x - x0) <= _l / 2;
  20. }
  21. var tmp = _a * x - y + _b;
  22. var _s = tmp * tmp / (_a * _a + 1);
  23. return _s <= _l / 2 * _l / 2;
  24. }