GeometricUtil.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * Returns the length of a vector
  3. *
  4. * @param {Vector}
  5. * @return {Float}
  6. */
  7. export function vectorLength(v) {
  8. return Math.sqrt(Math.pow(v.x, 2) + Math.pow(v.y, 2));
  9. }
  10. /**
  11. * Calculates the angle between a line a the yAxis
  12. *
  13. * @param {Array}
  14. * @return {Float}
  15. */
  16. export function getAngle(line) {
  17. // return value is between 0, 180 and -180, -0
  18. // @janstuemmel: maybe replace return a/b with b/a
  19. return Math.atan((line[1].y - line[0].y) / (line[1].x - line[0].x));
  20. }
  21. /**
  22. * Rotates a vector by a given angle
  23. *
  24. * @param {Vector}
  25. * @param {Float} Angle in radians
  26. * @return {Vector}
  27. */
  28. export function rotateVector(vector, angle) {
  29. return (!angle) ? vector : {
  30. x: Math.cos(angle) * vector.x - Math.sin(angle) * vector.y,
  31. y: Math.sin(angle) * vector.x + Math.cos(angle) * vector.y
  32. };
  33. }
  34. /**
  35. * Solves a 2D equation system
  36. * a + r*b = c, where a,b,c are 2D vectors
  37. *
  38. * @param {Vector}
  39. * @param {Vector}
  40. * @param {Vector}
  41. * @return {Float}
  42. */
  43. function solveLambaSystem(a, b, c) {
  44. // the 2d system
  45. var system = [
  46. { n: a[0] - c[0], lambda: b[0] },
  47. { n: a[1] - c[1], lambda: b[1] }
  48. ];
  49. // solve
  50. var n = system[0].n * b[0] + system[1].n * b[1],
  51. l = system[0].lambda * b[0] + system[1].lambda * b[1];
  52. return -n/l;
  53. }
  54. /**
  55. * Position of perpendicular foot
  56. *
  57. * @param {Point}
  58. * @param [ {Point}, {Point} ] line defined through two points
  59. * @return {Point} the perpendicular foot position
  60. */
  61. export function perpendicularFoot(point, line) {
  62. var a = line[0], b = line[1];
  63. // relative position of b from a
  64. var bd = { x: b.x - a.x, y: b.y - a.y };
  65. // solve equation system to the parametrized vectors param real value
  66. var r = solveLambaSystem([ a.x, a.y ], [ bd.x, bd.y ], [ point.x, point.y ]);
  67. return { x: a.x + r*bd.x, y: a.y + r*bd.y };
  68. }
  69. /**
  70. * Calculates the distance between a point and a line
  71. *
  72. * @param {Point}
  73. * @param [ {Point}, {Point} ] line defined through two points
  74. * @return {Float} distance
  75. */
  76. export function getDistancePointLine(point, line) {
  77. var pfPoint = perpendicularFoot(point, line);
  78. // distance vector
  79. var connectionVector = {
  80. x: pfPoint.x - point.x,
  81. y: pfPoint.y - point.y
  82. };
  83. return vectorLength(connectionVector);
  84. }
  85. /**
  86. * Calculates the distance between two points
  87. *
  88. * @param {Point}
  89. * @param {Point}
  90. * @return {Float} distance
  91. */
  92. export function getDistancePointPoint(point1, point2) {
  93. return vectorLength({
  94. x: point1.x - point2.x,
  95. y: point1.y - point2.y
  96. });
  97. }