How to use the d3-polygon.polygonCentroid function in d3-polygon

To help you get started, we’ve selected a few d3-polygon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / d3-polygon / d3-polygon-tests.ts View on Github external
// -----------------------------------------------------------------------------

let num: number;
let containsFlag: boolean;
let point: [number, number] = [15, 15];
let polygon: Array<[number, number]> = [[10, 10], [20, 20], [10, 30]];
let pointArray: Array<[number, number]> = [[10, 10], [20, 20], [10, 30], [15, 15]];
let hull: Array<[number, number]>;

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

num = d3Polygon.polygonArea(polygon);

point = d3Polygon.polygonCentroid(polygon);

hull = d3Polygon.polygonHull(pointArray);

containsFlag = d3Polygon.polygonContains(polygon, point);

num = d3Polygon.polygonLength(polygon);
github Kcnarf / d3-voronoi-map / src / d3-voronoi-map.js View on Github external
function adaptPositions(polygons, flickeringMitigationRatio) {
    var newMapPoints = [],
      flickeringInfluence = 0.5;
    var flickeringMitigation, d, polygon, mapPoint, centroid, dx, dy;

    flickeringMitigation = flickeringInfluence * flickeringMitigationRatio;
    d = 1 - flickeringMitigation; // in [0.5, 1]
    for (var i = 0; i < siteCount; i++) {
      polygon = polygons[i];
      mapPoint = polygon.site.originalObject;
      centroid = d3PolygonCentroid(polygon);

      dx = centroid[0] - mapPoint.x;
      dy = centroid[1] - mapPoint.y;

      //begin: handle excessive change;
      dx *= d;
      dy *= d;
      //end: handle excessive change;

      mapPoint.x += dx;
      mapPoint.y += dy;

      newMapPoints.push(mapPoint);
    }

    handleOverweighted(newMapPoints);
github elcontraption / illustrator-point-exporter / src / exporter.js View on Github external
var path = {};
    var i;

    for (i = 0; i < pathCount; i++) {
        path = layer.pathItems[i];

        // Skip if locked or hidden
        if (path.locked || path.hidden) continue;

        var points = this.readPathPoints(path);

        paths.push({
            name: path.name || path.typename,
            area: path.area,
            points: points,
            centroid: polygon.polygonCentroid(points)
        });
    }

    return paths;
};
github ideditor / id-sdk / packages / math / geom / built / geom.js View on Github external
function geomGetSmallestSurroundingRectangle(points) {
  var hull = d3_polygon_1.polygonHull(points);
  var centroid = d3_polygon_1.polygonCentroid(hull);
  var minArea = Infinity;
  var ssrExtent = new extent_1.Extent();
  var ssrAngle = 0;
  var c1 = hull[0];
  for (var i = 0; i <= hull.length - 1; i++) {
    var c2 = i === hull.length - 1 ? hull[0] : hull[i + 1];
    var angle = Math.atan2(c2[1] - c1[1], c2[0] - c1[0]);
    var poly = geomRotatePoints(hull, -angle, centroid);
    var extent = poly.reduce(function(acc, point) {
      // update Extent min/max in-place for speed
      acc.min[0] = Math.min(acc.min[0], point[0]);
      acc.min[1] = Math.min(acc.min[1], point[1]);
      acc.max[0] = Math.max(acc.max[0], point[0]);
      acc.max[1] = Math.max(acc.max[1], point[1]);
      return acc;
    }, new extent_1.Extent());
github openstreetmap / iD / modules / actions / circularize.js View on Github external
var way = graph.entity(wayId),
            origNodes = {};

        graph.childNodes(way).forEach(function(node) {
            if (!origNodes[node.id]) origNodes[node.id] = node;
        });

        if (!way.isConvex(graph)) {
            graph = action.makeConvex(graph);
        }

        var nodes = _uniq(graph.childNodes(way)),
            keyNodes = nodes.filter(function(n) { return graph.parentWays(n).length !== 1; }),
            points = nodes.map(function(n) { return projection(n.loc); }),
            keyPoints = keyNodes.map(function(n) { return projection(n.loc); }),
            centroid = (points.length === 2) ? geoVecInterp(points[0], points[1], 0.5) : d3_polygonCentroid(points),
            radius = d3_median(points, function(p) { return geoVecLength(centroid, p); }),
            sign = d3_polygonArea(points) > 0 ? 1 : -1,
            ids;

        // we need atleast two key nodes for the algorithm to work
        if (!keyNodes.length) {
            keyNodes = [nodes[0]];
            keyPoints = [points[0]];
        }

        if (keyNodes.length === 1) {
            var index = nodes.indexOf(keyNodes[0]),
                oppositeIndex = Math.floor((index + nodes.length / 2) % nodes.length);

            keyNodes.push(nodes[oppositeIndex]);
            keyPoints.push(points[oppositeIndex]);
github alexjlockwood / ShapeShifter / src / app / scripts / algorithms / Order.ts View on Github external
function squaredDistance(p1: Ring, p2: Ring) {
  const d = distance(polygonCentroid(p1), polygonCentroid(p2));
  return d * d;
}
github Kcnarf / d3-voronoi-map / src / initial-position-policies / pie.js View on Github external
function updateInternals() {
    clippingPolygonCentroid = d3PolygonCentroid(clippingPolygon);
    halfIncircleRadius = computeMinDistFromEdges(clippingPolygonCentroid, clippingPolygon) / 2;
    dataArrayLength = dataArray.length;
    angleBetweenData = 2 * Math.PI / dataArrayLength;
  };
github ideditor / id-sdk / packages / math / geom / lib / geom.js View on Github external
export function geomGetSmallestSurroundingRectangle(points) {
  var hull = d3_polygonHull(points);
  var centroid = d3_polygonCentroid(hull);
  var minArea = Infinity;
  var ssrExtent = [];
  var ssrAngle = 0;
  var c1 = hull[0];

  for (var i = 0; i <= hull.length - 1; i++) {
    var c2 = i === hull.length - 1 ? hull[0] : hull[i + 1];
    var angle = Math.atan2(c2[1] - c1[1], c2[0] - c1[0]);
    var poly = geomRotatePoints(hull, -angle, centroid);
    var extent = poly.reduce(function(extent, point) {
      return extent.extend(Extent(point));
    }, Extent());

    var area = extent.area();
    if (area < minArea) {
      minArea = area;

d3-polygon

Operations for two-dimensional polygons.

ISC
Latest version published 3 years ago

Package Health Score

71 / 100
Full package analysis