index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // index.ts
  2. import {
  3. degreesToRadians,
  4. polygon,
  5. isObject,
  6. isNumber
  7. } from "@turf/helpers";
  8. import { rhumbDestination } from "@turf/rhumb-destination";
  9. import { transformRotate } from "@turf/transform-rotate";
  10. import { getCoord } from "@turf/invariant";
  11. function ellipse(center, xSemiAxis, ySemiAxis, options) {
  12. options = options || {};
  13. const steps = options.steps || 64;
  14. const units = options.units || "kilometers";
  15. const angle = options.angle || 0;
  16. const pivot = options.pivot || center;
  17. const properties = options.properties || {};
  18. if (!center) throw new Error("center is required");
  19. if (!xSemiAxis) throw new Error("xSemiAxis is required");
  20. if (!ySemiAxis) throw new Error("ySemiAxis is required");
  21. if (!isObject(options)) throw new Error("options must be an object");
  22. if (!isNumber(steps)) throw new Error("steps must be a number");
  23. if (!isNumber(angle)) throw new Error("angle must be a number");
  24. const centerCoords = getCoord(center);
  25. if (units !== "degrees") {
  26. const xDest = rhumbDestination(center, xSemiAxis, 90, { units });
  27. const yDest = rhumbDestination(center, ySemiAxis, 0, { units });
  28. xSemiAxis = getCoord(xDest)[0] - centerCoords[0];
  29. ySemiAxis = getCoord(yDest)[1] - centerCoords[1];
  30. }
  31. const coordinates = [];
  32. for (let i = 0; i < steps; i += 1) {
  33. const stepAngle = i * -360 / steps;
  34. let x = xSemiAxis * ySemiAxis / Math.sqrt(
  35. Math.pow(ySemiAxis, 2) + Math.pow(xSemiAxis, 2) * Math.pow(getTanDeg(stepAngle), 2)
  36. );
  37. let y = xSemiAxis * ySemiAxis / Math.sqrt(
  38. Math.pow(xSemiAxis, 2) + Math.pow(ySemiAxis, 2) / Math.pow(getTanDeg(stepAngle), 2)
  39. );
  40. if (stepAngle < -90 && stepAngle >= -270) x = -x;
  41. if (stepAngle < -180 && stepAngle >= -360) y = -y;
  42. if (units === "degrees") {
  43. const angleRad = degreesToRadians(angle);
  44. const newx = x * Math.cos(angleRad) + y * Math.sin(angleRad);
  45. const newy = y * Math.cos(angleRad) - x * Math.sin(angleRad);
  46. x = newx;
  47. y = newy;
  48. }
  49. coordinates.push([x + centerCoords[0], y + centerCoords[1]]);
  50. }
  51. coordinates.push(coordinates[0]);
  52. if (units === "degrees") {
  53. return polygon([coordinates], properties);
  54. } else {
  55. return transformRotate(polygon([coordinates], properties), angle, {
  56. pivot
  57. });
  58. }
  59. }
  60. function getTanDeg(deg) {
  61. const rad = deg * Math.PI / 180;
  62. return Math.tan(rad);
  63. }
  64. var turf_ellipse_default = ellipse;
  65. export {
  66. turf_ellipse_default as default,
  67. ellipse
  68. };
  69. //# sourceMappingURL=index.js.map