Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
d3-polygon
Advanced tools
The d3-polygon npm package is a part of the D3.js library that provides utilities for working with polygons. It includes functions for calculating the area, centroid, and perimeter of polygons, as well as for clipping and hull generation.
Polygon Area Calculation
This feature allows you to calculate the area of a polygon. The code sample demonstrates how to calculate the area of a rectangle with vertices at (0,0), (4,0), (4,3), and (0,3).
const d3 = require('d3-polygon');
const polygon = [[0, 0], [4, 0], [4, 3], [0, 3]];
const area = d3.polygonArea(polygon);
console.log(area); // Output: 12
Polygon Centroid Calculation
This feature allows you to calculate the centroid (geometric center) of a polygon. The code sample demonstrates how to find the centroid of a rectangle.
const d3 = require('d3-polygon');
const polygon = [[0, 0], [4, 0], [4, 3], [0, 3]];
const centroid = d3.polygonCentroid(polygon);
console.log(centroid); // Output: [2, 1.5]
Polygon Hull Generation
This feature allows you to generate the convex hull of a set of points. The code sample demonstrates how to find the convex hull of a set of points that includes the vertices of a rectangle and an additional point inside the rectangle.
const d3 = require('d3-polygon');
const points = [[0, 0], [4, 0], [4, 3], [0, 3], [2, 1.5]];
const hull = d3.polygonHull(points);
console.log(hull); // Output: [[0, 0], [4, 0], [4, 3], [0, 3]]
Turf is a powerful geospatial analysis library for JavaScript. It provides a wide range of functions for working with geometries, including polygons. Compared to d3-polygon, Turf offers more comprehensive geospatial analysis capabilities, such as buffering, intersecting, and measuring distances between geometries.
Polygon Clipping is a library specifically designed for performing boolean operations on polygons, such as union, intersection, and difference. While d3-polygon provides basic polygon utilities, Polygon Clipping focuses on more advanced operations for manipulating polygon shapes.
Earcut is a fast and simple library for triangulating polygons. It is particularly useful for rendering polygons in WebGL. Compared to d3-polygon, Earcut is specialized for converting polygons into triangles, which can be useful for graphics and visualization applications.
This module provides a few basic geometric operations for two-dimensional polygons. Each polygon is represented as an array of two-element arrays [[x1, y1], [x2, y2], …], and may either be closed (wherein the first and last point are the same) or open (wherein they are not). Typically polygons are in counterclockwise order, assuming a coordinate system where the origin ⟨0,0⟩ is in the top-left corner.
If you use NPM, npm install d3-polygon
. Otherwise, download the latest release.
# area(polygon)
Returns the signed area of the specified polygon. If the vertices of the polygon are in counterclockwise order (assuming a coordinate system where the origin ⟨0,0⟩ is in the top-left corner), the returned area is positive; otherwise it is negative, or zero.
# centroid(polygon)
Returns the centroid of the specified polygon.
# perimeter(polygon)
Returns the perimeter of the specified polygon.
FAQs
Operations for two-dimensional polygons.
The npm package d3-polygon receives a total of 1,125,974 weekly downloads. As such, d3-polygon popularity was classified as popular.
We found that d3-polygon demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.