modeling/src/primitives/circle.js

  1. const { TAU } = require('../maths/constants')
  2. const ellipse = require('./ellipse')
  3. const { isGTE } = require('./commonChecks')
  4. /**
  5. * Construct a circle in two dimensional space where all points are at the same distance from the center.
  6. * @see [ellipse]{@link module:modeling/primitives.ellipse} for more options
  7. * @param {Object} [options] - options for construction
  8. * @param {Array} [options.center=[0,0]] - center of circle
  9. * @param {Number} [options.radius=1] - radius of circle
  10. * @param {Number} [options.startAngle=0] - start angle of circle, in radians
  11. * @param {Number} [options.endAngle=TAU] - end angle of circle, in radians
  12. * @param {Number} [options.segments=32] - number of segments to create per full rotation
  13. * @returns {geom2} new 2D geometry
  14. * @alias module:modeling/primitives.circle
  15. * @example
  16. * let myshape = circle({radius: 10})
  17. */
  18. const circle = (options) => {
  19. const defaults = {
  20. center: [0, 0],
  21. radius: 1,
  22. startAngle: 0,
  23. endAngle: TAU,
  24. segments: 32
  25. }
  26. let { center, radius, startAngle, endAngle, segments } = Object.assign({}, defaults, options)
  27. if (!isGTE(radius, 0)) throw new Error('radius must be positive')
  28. radius = [radius, radius]
  29. return ellipse({ center, radius, startAngle, endAngle, segments })
  30. }
  31. module.exports = circle