npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kninnug/containing-triangle

v2.0.0

Published

Find the triangle of a Delaunator triangulation that contains a given point

Downloads

389

Readme

Containing Triangle

Find the triangle of a Delaunator triangulation that contains a given point.

Located triangle highlighted

Example

// Points to be triangulated
const points = [[53,98],[5,201],[194,288],[280,195],[392,148],[413,43],[278,5],[169,71],[146,171]],
	// Edges to be constrained (optional)
	edges = [[5, 8]],
	// Triangulate
	del = Delaunator.from(points),
	// (Optional: constrain the triangulation)
	con = new Constrainautor(del).constrainAll(edges),
	// Find the triangle that contains the point (178, 190)
	tri = containingTriangle(del, 178, 190);

// tri has triangle id: 3

Install

Install from NPM:

npm install @kninnug/containing-triangle

Use in Node.js:

const containingTriangle = require('@kninnug/containing-triangle'),
	isInTriangulation = containingTriangle.isInTriangulation;

or as an ECMAScript/ES6 module:

import containingTriangle, {isInTriangulation} from '@kninnug/containing-triangle';

or in the browser:

<script src="node_modules/@kninnug/containing-triangle/lib/containing-triangle.js"></script>

or minified:

<script src="node_modules/@kninnug/containing-triangle/lib/containing-triangle.min.js"></script>

The containing-triangle library does not depend on Delaunator itself, but the input is expected to be in the format that Delaunator outputs. The ES module variant (lib/containing-triangle.mjs) depends on robust-predicates, but the CommonJS, browser, and minified versions (lib/containing-triangle.cjs, lib/containing-triangle.js, and lib/containing-triangle.min.js) come with this dependency compiled in, and can be used standalone. The (source) TypeScript version is in Constrainautor.ts.

Usage

containingTriangle(del, x, y)

Given a triangulation from Delaunator: del, and the coordinates of a point (x, y), finds the triangle that contains that point. Returns the triangle id, or -1 if the point is outside the hull of the triangulation, i.e. not in any of its triangles.

isInTriangulation(del, x, y)

Whether a point (x, y) is within the hull of the given triangulation. Should generally be faster if you only want to know if the point is in the triangulation and don't care what triangle it's in.

Changes

2.0.0

  • Convert to TypeScript.
  • Move built files to lib/.

1.1.1

  • Update dependencies.

1.1.0

  • Fix stability issue.
  • Add isInTriangulation function.
  • Move test files to separate repository (to share with other libraries).

1.0.0

  • Initial version.

Attributions