rect.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { pointAtSegments, angleAtSegments, distanceAtSegment } from './segments';
  2. function getPoints(x, y, width, height) {
  3. return [
  4. [x, y],
  5. [x + width, y],
  6. [x + width, y + height],
  7. [x, y + height],
  8. [x, y],
  9. ];
  10. }
  11. export default {
  12. /**
  13. * 矩形包围盒计算
  14. * @param {number} x 起始点 x
  15. * @param {number} y 起始点 y
  16. * @param {number} width 宽度
  17. * @param {number} height 高度
  18. * @return {object} 包围盒
  19. */
  20. box: function (x, y, width, height) {
  21. return {
  22. x: x,
  23. y: y,
  24. width: width,
  25. height: height,
  26. };
  27. },
  28. /**
  29. * 长度,矩形不需要传入 x, y 即可计算周长,但是避免出错
  30. * @param {number} x 起始点 x
  31. * @param {number} y 起始点 y
  32. * @param {number} width 宽
  33. * @param {number} height 高
  34. */
  35. length: function (x, y, width, height) {
  36. return (width + height) * 2;
  37. },
  38. /**
  39. * 点到矩形的最小距离
  40. * @param {number} x 起始点 x
  41. * @param {number} y 起始点 y
  42. * @param {number} width 宽度
  43. * @param {number} height 高度
  44. * @param {number} x0 指定点的 x
  45. * @param {number} y0 指定点的 y
  46. * @return {number} 最短距离
  47. */
  48. pointDistance: function (x, y, width, height, x0, y0) {
  49. var points = getPoints(x, y, width, height);
  50. return distanceAtSegment(points, x0, y0);
  51. },
  52. /**
  53. * 按照比例计算对应的点
  54. * @param {number} x 起始点 x
  55. * @param {number} y 起始点 y
  56. * @param {number} width 宽度
  57. * @param {number} height 高度
  58. * @param {number} t 比例 0-1 之间的值
  59. * @return {object} 计算出来的点信息,包含 x,y
  60. */
  61. pointAt: function (x, y, width, height, t) {
  62. // 边界判断,避免获取顶点
  63. if (t > 1 || t < 0) {
  64. return null;
  65. }
  66. var points = getPoints(x, y, width, height);
  67. return pointAtSegments(points, t);
  68. },
  69. /**
  70. * 获取对应点的切线角度
  71. * @param {number} x 起始点 x
  72. * @param {number} y 起始点 y
  73. * @param {number} width 宽度
  74. * @param {number} height 高度
  75. * @param {number} t 比例 0-1 之间的值
  76. * @return {number} 切线的角度
  77. */
  78. tangentAngle: function (x, y, width, height, t) {
  79. // 边界判断,避免获取顶点
  80. if (t > 1 || t < 0) {
  81. return 0;
  82. }
  83. var points = getPoints(x, y, width, height);
  84. return angleAtSegments(points, t);
  85. },
  86. };
  87. //# sourceMappingURL=rect.js.map