{"size": 961, "ext": "js", "max_stars_repo_path": "packages/modeling/src/geometry/geom2/fromPoints.js", "max_stars_repo_name": "minatani/OpenJSCAD.org", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/modeling/src/geometry/geom2/fromPoints.js", "max_issues_repo_name": "minatani/OpenJSCAD.org", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "packages/modeling/src/geometry/geom2/fromPoints.js", "max_forks_repo_name": "minatani/OpenJSCAD.org", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 31.0, "max_line_length": 96, "alphanum_fraction": 0.6930280957}
const vec2 = require('../../math/vec2')

const create = require('./create')

/**
 * Create a new 2D geometry from the given points.
 * The direction (rotation) of the points is not relevant,
 * as the points can define a convex or a concave polygon.
 * The geometry must not self intersect, i.e. the sides cannot cross.
 * @param {Array} points - list of points in 2D space where each point is an array of two values
 * @returns {geom2} a new geometry
 */
const fromPoints = function (points) {
  if (!Array.isArray(points)) {
    throw new Error('the given points must be an array')
  }
  if (points.length < 3) {
    throw new Error('the given points must define a closed geometry with three or more points')
  }

  let sides = []
  let prevpoint = points[points.length - 1]
  points.forEach(function (point) {
    sides.push([vec2.fromArray(prevpoint), vec2.fromArray(point)])
    prevpoint = point
  })
  return create(sides)
}

module.exports = fromPoints
