Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend numberOfEdges to accept a callback #52

Open
brandonmcconnell opened this issue Nov 2, 2023 · 0 comments
Open

Extend numberOfEdges to accept a callback #52

brandonmcconnell opened this issue Nov 2, 2023 · 0 comments

Comments

@brandonmcconnell
Copy link

brandonmcconnell commented Nov 2, 2023

It would be very useful if the numberOfEdges option could accept a callback which would calculate the number of edges based on the size/radius/circumference of the circle.

The new usage might look something like this:

const circleToPolygon = require("circle-to-polygon");

const coordinates = [-27.4575887, -58.99029];
const radius = 100;
const numberOfEdges = ({ c }) => c / 4;

let polygon = circleToPolygon(coordinates, radius, { numberOfEdges });

And the definition for getNumberOfEdges could be adjusted to this:

const DEFAULT_NUMBER_OF_EDGES = 32;

function getNumberOfEdges(radius, options) {
  if (!options) return DEFAULT_NUMBER_OF_EDGES;
  if (!isObjectNotArray(options)) return options;

  if (typeof options.numberOfEdges === "function") {
    const circumference = 2 * Math.PI * radius;
    const numberOfEdges = options.numberOfEdges({ c: circumference, r: radius });
    if (Number.isFinite(numberOfEdges)) return Math.round(numberOfEdges);
    return DEFAULT_NUMBER_OF_EDGES;
  }

  return options.numberOfEdges ?? DEFAULT_NUMBER_OF_EDGES;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant