1.4.1-beta • Published 3 years ago

ocdots v1.4.1-beta

Weekly downloads
1
License
GPL-3.0-or-later
Repository
github
Last release
3 years ago

OCDots

OCDots is a javascript library for creating evenly distributed points inside a polygon

codecov CodeFactor npm version License: GPL v3

ocdots

Check the demo!

Quick Start

OCDots uses physics to maximize the distance between each point and also the distance to the walls of a polygon. It is possible to follow each step of the process calling movePoints, or call relaxPoints to run several iterations and see the final state.

Import the library, then call movePoints on a set of points to run one iteration

const polygon = [
  [0, 0],
  [0, 500],
  [500, 500],
  [500, 0],
  [0, 0],
];
const points = [
  [10, 10],
  [20, 20],
  [30, 30],
  [40, 40],
  [50, 50],
  [60, 60],
  [70, 70],
  [80, 80],
  [90, 91],
];
let momentum = points.map(() => [0, 0]);
const newPoints = ocdots.movePoints({
  points,
  momentum,
  polygon,
}); // Points closer to the relaxed position
// newPoints:
// [[13.535794548720887, 13.535273243928348],
// [23.27147728369301, 23.266677836465515],
// [31.254830565917008, 31.24699231498995],
// [41.28615235332842, 41.272681142242014],
// [51.958871850605114, 51.93302052874113],
// [63.120940102952105, 63.061513386276076],
// [73.60106973613009, 73.4687601178992],
// [83.7612857566466, 83.29434810802492],
// [93.40654447613721, 94.65998015460454]]

Or call relaxPoints to run several iterations:

const polygon = [
  [0, 0],
  [0, 500],
  [500, 500],
  [500, 0],
  [0, 0],
];
const points = [
  [10, 10],
  [20, 20],
  [30, 30],
  [40, 40],
  [50, 50],
  [60, 60],
  [70, 70],
];
const iterations = 600;
const newPoints = ocdots.relaxPoints({
  points,
  polygon,
  iterations,
}); // Points closer to the relaxed position, 600 iterations
// newPoints:
// [[102.97786539754973, 102.93913654344668],
// [249.88751973067804, 95.97842402445758],
// [95.85157380539883, 249.581118685883],
// [249.83589254388568, 250.64858636270534],
// [103.38882973892018, 396.3498058144843],
// [396.8795797129724, 103.0543405820668],
// [249.77618266302204, 404.4383179086723],
// [404.0862954198404, 249.7401659115181],
// [396.995879400969, 396.84327821075937]]

Install

Install with npm:

npm install ocdots

To use with node just import the module:

const ocdots = require("ocdots");
ocdots.movePoints(...);

In the browser import the script as a module:

<script type="module">
  import * as ocdots from 'node_modules/ocdots/dist.browser/ocdots.js';
  ocdots.movePoints(...);
</script>

You can also download ocdots.js and alongside with simplify.js and import as you prefer.

Usage

ocdots provides several functions to iterate a set of points in a polygon. All functions must be called with named parameters

movePoints(config) ⇒ Array.<Array>

Moves points according to the applied forces into it. The forces are: 1) between points, 2) between the point and walls of the polygon.

The points moves according to it's momentum up to maxMomentum. Drag reduces the momentum with the square of the momentum. Viscosity lowers the momentum of points with high forces.

Runs one iteration

Kind: global function
Returns: Array.<Array> - points, momentum - Updated points and momentum arrays

ParamTypeDescription
configObjectconfiguration object
congig.pointsArrayThe points to move
config.momentumArrayAccumulated momentum for each point. 0, 0 when the points are stopped.
config.polygonArraySet of points that describes the polygon that contains the points. This polygon should be ordered (clockwise or anticlockwise) and closed i.e. first points equals the last point.
config.massArray.<Number> | NumberMass or masses of the points.
config.chargeArray.<Number> | NumberCharge or charges of the points.
config.baseForceNumberThe force constant
config.dragNumberThe drag coeficient
config.viscosityNumberThe viscosity coeficient
config.maxMomentumNumberMaximum momentum for each point
config.parallelForcesBooleanSum line segmen parallel forces as well.
config.wallForcesNumberWalls forces constant
config.simplifyPolygonNumberSimplify polygon tolerance (0 disabled)

randomInPolygon(N, polygon) ⇒ Array

Creates N points inside the polygon

Kind: global function
Returns: Array - points N points inside the polygon

ParamTypeDescription
NNumberNumber of points
polygonArraySet of points that describes the polygon. This polygon should be ordered (clockwise or anticlockwise) and closed i.e. first points equals the last point.

randomInGeoPolygon(N, geoPolygon) ⇒ Array

Creates N points inside a geo polygon,

Kind: global function
Returns: Array - points N points inside the geo polygon

ParamTypeDescription
NNumberNumber of points
geoPolygonArrayPolygon of geo coordinates {lat, lng}

relaxPoints(config) ⇒ Array

Runs several iterations of movePoints(). The drag increases in every iteration attenuating the movement.

Kind: global function
Returns: Array - points Last iteration points positions

ParamTypeDescription
configObjectconfiguration object
config.pointsArrayThe points to move
config.momentumArrayInitial momentum. 0, 0 for all points if ommited
config.polygonArraySet of points that describes the polygon that contains the points. This polygon should be ordered (clockwise or anticlockwise) and closed i.e. first points equals the last point.
config.iterationsNumberNumber of iterations to run
config.callbackfunctionCallback function to run at every iteration (optional). Callback args: points, momentum, polygon, baseForce, currentDrag, viscosity, maxMomentum
config.massArray.<Number> | NumberMass or masses of the points.
config.chargeArray.<Number> | NumberCharge or charges of the points.
config.baseForceNumberThe force constant
config.dragNumberThe drag coeficient
config.viscosityNumberThe viscosity coeficient
config.maxMomentumNumberMaximum momentum for each point
config.parallelForcesBooleanSum line segmen parallel forces as well.
config.wallForcesNumberWalls forces constant
config.simplifyPolygonNumberSimplify polygon tolerance (0 disabled)
config.attenuationNumberRate of attenuation

relaxNPoints(config) ⇒ Array

Calls relaxPoints for N random points placed inside the polygon.

Kind: global function
Returns: Array - points Last iteration points positions

ParamTypeDescription
configObjectconfiguration object
config.NNumberNumber of points
config.polygonArraySet of points that describes the polygon that contains the points. This polygon should be ordered (clockwise or anticlockwise) and closed i.e. first points equals the last point.
config.iterationsNumberNumber of iterations to run
config.callbackfunctionCallback function to run at every iteration (optional). Callback args: points, momentum, polygon, baseForce, currentDrag, viscosity, maxMomentum
config.massArray.<Number> | NumberMass or masses of the points.
config.chargeArray.<Number> | NumberCharge or charges of the points.
config.baseForceNumberThe force constant
config.dragNumberThe drag coeficient
config.viscosityNumberThe viscosity coeficient
config.maxMomentumNumberMaximum momentum for each point
config.parallelForcesBooleanSum line segmen parallel forces as well.
config.wallForcesNumberWalls forces constant
config.simplifyPolygonNumberSimplify polygon tolerance (0 disabled)
config.attenuationNumberRate of attenuation

relaxGeoPoints(config) ⇒ Object

Converts geoPoints and geoPolygon to a points and polygon, then calls relaxPoints, returning the last position of the points.

Kind: global function
Returns: Object - { polygon, points, geoPoints } Last iteration geo points positions

ParamTypeDescription
configObjectconfiguration object
config.geoPointsArrayPoints in geo coordinates {lat, lng}
config.geoPolygonArrayPolygon of geo coordinates {lat, lng}
config.widthNumberWidth of the polygon
config.iterationsNumberNumber of iterations to run
confi.callbackfunctionCallback function to run at every iteration. Callback args: points, momentum, polygon, baseForce, currentDrag, viscosity, maxMomentum
config.massArray.<Number> | NumberMass or masses of the points.
config.chargeArray.<Number> | NumberCharge or charges of the points.
config.baseForceNumberThe force constant
config.dragNumberThe drag coeficient
config.viscosityNumberThe viscosity coeficient
config.maxMomentumNumberMaximum momentum for each point
config.parallelForcesBooleanSum line segmen parallel forces as well.
config.wallForcesNumberWalls forces constant
config.simplifyPolygonNumberSimplify polygon tolerance (0 disabled)
config.attenuationNumberRate of attenuation

relaxNGeoPoints(config) ⇒ Object

Calls relaxGeoPoints for N random points placed inside the polygon.

Kind: global function
Returns: Object - { polygon, points, geoPoints } Last iteration geo points positions

ParamTypeDescription
configObjectconfiguration object
config.NNumberNumber of points
config.geoPolygonArrayPolygon of geo coordinates {lat, lng}
config.widthNumberWidth of the polygon
config.iterationsNumberjNumber of iterations to run
config.callbackfunctionCallback function to run at every iteration. Callback args: points, momentum, polygon, baseForce, currentDrag, viscosity, maxMomentum
config.massArray.<Number> | NumberMass or masses of the points.
config.chargeArray.<Number> | NumberCharge or charges of the points.
config.baseForceNumberThe force constant
config.dragNumberThe drag coeficient
config.viscosityNumberThe viscosity coeficient
config.maxMomentumNumberMaximum momentum for each point
config.parallelForcesBooleanSum line segmen parallel forces as well.
config.wallForcesNumberWalls forces constant
config.simplifyPolygonNumberSimplify polygon tolerance (0 disabled)
config.attenuationNumberRate of attenuation

buildPolygon(geoPolygon, width) ⇒ Object

Transforms a set of coordinates into a polygon with a known width

Kind: global function
Returns: Object - { polygon, minLat, minLng, delta }

ParamTypeDescription
geoPolygonArrayPolygon of geo coordinates {lat, lng}
widthNumberWidth of the polygon

License

OCDots Copyright (C) 2020 Luiz Eduardo Amaral luizamaral306@gmail.com

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License

1.4.1-beta

3 years ago

1.4.0-beta

3 years ago

1.3.0-beta

3 years ago

1.2.4-beta

3 years ago

1.2.3-beta

4 years ago

1.2.2-beta

4 years ago

1.2.1-beta

4 years ago

1.2.0-beta

4 years ago

1.1.0-beta

4 years ago

1.0.1-beta

4 years ago

1.0.0-beta

4 years ago