Geometry.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* *
  2. *
  3. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  4. *
  5. * */
  6. 'use strict';
  7. /**
  8. * Calculates the center between a list of points.
  9. * @private
  10. * @param {Array<Highcharts.PositionObject>} points
  11. * A list of points to calculate the center of.
  12. * @return {Highcharts.PositionObject}
  13. * Calculated center
  14. */
  15. var getCenterOfPoints = function getCenterOfPoints(points) {
  16. var sum = points.reduce(function (sum, point) {
  17. sum.x += point.x;
  18. sum.y += point.y;
  19. return sum;
  20. }, { x: 0, y: 0 });
  21. return {
  22. x: sum.x / points.length,
  23. y: sum.y / points.length
  24. };
  25. };
  26. /**
  27. * Calculates the distance between two points based on their x and y
  28. * coordinates.
  29. * @private
  30. * @param {Highcharts.PositionObject} p1
  31. * The x and y coordinates of the first point.
  32. * @param {Highcharts.PositionObject} p2
  33. * The x and y coordinates of the second point.
  34. * @return {number}
  35. * Returns the distance between the points.
  36. */
  37. var getDistanceBetweenPoints = function getDistanceBetweenPoints(p1, p2) {
  38. return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
  39. };
  40. /**
  41. * Calculates the angle between two points.
  42. * @todo add unit tests.
  43. * @private
  44. * @param {Highcharts.PositionObject} p1 The first point.
  45. * @param {Highcharts.PositionObject} p2 The second point.
  46. * @return {number} Returns the angle in radians.
  47. */
  48. var getAngleBetweenPoints = function getAngleBetweenPoints(p1, p2) {
  49. return Math.atan2(p2.x - p1.x, p2.y - p1.y);
  50. };
  51. var geometry = {
  52. getAngleBetweenPoints: getAngleBetweenPoints,
  53. getCenterOfPoints: getCenterOfPoints,
  54. getDistanceBetweenPoints: getDistanceBetweenPoints
  55. };
  56. export default geometry;