Geometry.js 1.7 KB

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