Security News
Supply Chain Attack Detected in Solana's web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
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. You can also load directly from d3js.org, either as a standalone library or as part of D3 4.0. AMD, CommonJS, and vanilla environments are supported. In vanilla, a d3
global is exported:
<script src="https://d3js.org/d3-polygon.v1.min.js"></script>
<script>
var hull = d3.polygonHull(points);
</script>
Try d3-polygon in your browser.
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.
# d3.polygonCentroid(polygon) <>
Returns the centroid of the specified polygon.
Returns the convex hull of the specified points using Andrew’s monotone chain algorithm. The returned hull is represented as an array containing a subset of the input points arranged in counterclockwise order. Returns null if points has fewer than three elements.
# d3.polygonContains(polygon, point) <>
Returns true if and only if the specified point is inside the specified polygon.
# d3.polygonLength(polygon) <>
Returns the length of the perimeter of the specified polygon.
FAQs
Operations for two-dimensional polygons.
The npm package d3-polygon receives a total of 2,241,920 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
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.