Security News
RubyGems.org Adds New Maintainer Role
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.
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
. You can also download the latest release on GitHub. For vanilla HTML in modern browsers, import d3-polygon from Skypack:
<script type="module">
import {polygonHull} from "https://cdn.skypack.dev/d3-polygon@3";
const hull = polygonHull(points);
</script>
For legacy environments, you can load d3-polygon’s UMD bundle from an npm-based CDN such as jsDelivr; a d3
global is exported:
<script src="https://cdn.jsdelivr.net/npm/d3-polygon@3"></script>
<script>
const hull = d3.polygonHull(points);
</script>
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.
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
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.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.