Geometry.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {
  2. every
  3. } from 'min-dash';
  4. /**
  5. * Computes the distance between two points
  6. *
  7. * @param {Point} p
  8. * @param {Point} q
  9. *
  10. * @return {number} distance
  11. */
  12. export function pointDistance(a, b) {
  13. if (!a || !b) {
  14. return -1;
  15. }
  16. return Math.sqrt(
  17. Math.pow(a.x - b.x, 2) +
  18. Math.pow(a.y - b.y, 2)
  19. );
  20. }
  21. /**
  22. * Returns true if the point r is on the line between p and q
  23. *
  24. * @param {Point} p
  25. * @param {Point} q
  26. * @param {Point} r
  27. * @param {number} [accuracy=5] accuracy for points on line check (lower is better)
  28. *
  29. * @return {boolean}
  30. */
  31. export function pointsOnLine(p, q, r, accuracy) {
  32. if (typeof accuracy === 'undefined') {
  33. accuracy = 5;
  34. }
  35. if (!p || !q || !r) {
  36. return false;
  37. }
  38. var val = (q.x - p.x) * (r.y - p.y) - (q.y - p.y) * (r.x - p.x),
  39. dist = pointDistance(p, q);
  40. // @see http://stackoverflow.com/a/907491/412190
  41. return Math.abs(val / dist) <= accuracy;
  42. }
  43. var ALIGNED_THRESHOLD = 2;
  44. /**
  45. * Check whether two points are horizontally or vertically aligned.
  46. *
  47. * @param {Point[]|Point} a
  48. * @param {Point} [b]
  49. *
  50. * @return {'h'|'v'|false} axis or false
  51. */
  52. export function pointsAligned(a, b) {
  53. var points = Array.from(arguments).flat();
  54. const axisMap = {
  55. 'x': 'v',
  56. 'y': 'h'
  57. };
  58. for (const [ axis, orientation ] of Object.entries(axisMap)) {
  59. if (pointsAlignedOnAxis(axis, points)) {
  60. return orientation;
  61. }
  62. }
  63. return false;
  64. }
  65. /**
  66. * @param { 'x' | 'y' } axis
  67. * @param { Point[] } points
  68. *
  69. * @return {boolean}
  70. */
  71. export function pointsAlignedOnAxis(axis, points) {
  72. const referencePoint = points[0];
  73. return every(points, function(point) {
  74. return Math.abs(referencePoint[axis] - point[axis]) <= ALIGNED_THRESHOLD;
  75. });
  76. }
  77. /**
  78. * Returns true if the point p is inside the rectangle rect
  79. *
  80. * @param {Point} p
  81. * @param {Rect} rect
  82. * @param {number} tolerance
  83. *
  84. * @return {boolean}
  85. */
  86. export function pointInRect(p, rect, tolerance) {
  87. tolerance = tolerance || 0;
  88. return p.x > rect.x - tolerance &&
  89. p.y > rect.y - tolerance &&
  90. p.x < rect.x + rect.width + tolerance &&
  91. p.y < rect.y + rect.height + tolerance;
  92. }
  93. /**
  94. * Returns a point in the middle of points p and q
  95. *
  96. * @param {Point} p
  97. * @param {Point} q
  98. *
  99. * @return {Point} middle point
  100. */
  101. export function getMidPoint(p, q) {
  102. return {
  103. x: Math.round(p.x + ((q.x - p.x) / 2.0)),
  104. y: Math.round(p.y + ((q.y - p.y) / 2.0))
  105. };
  106. }