index.cjs 2.8 KB

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