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/trivis

v2.0.0

Published

Compute visibility polygons by Triangular Expansion

Downloads

292

Readme

TriVis

Compute visibility polygons by Triangular Expansion.

Visbility polygon example

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
	edges = [[5, 8]],
	// Triangulate
	del = Delaunator.from(points),
	// Constrain the triangulation
	con = new Constrainautor(del);
con.constrainAll(edges);

// Query point
const qx = 162, qy = 262,
	// Obstruction callback: use constrained edges as obstructions
	obstructs = (edg) => con.isConstrained(edg),
	// Left & right end-points of the initial viewing cone (optional)
	ilx = 45, ily = 144, irx = 280, iry = 145,
	// Compute visibility polygon
	poly = triangularExpansion(del, qx, qy, obstructs, ilx, ily, irx, iry);

for(const [lx, ly, rx, ry] of poly){
	drawTriangle(lx, ly, qx, qy, rx, ry);
}

Visibility polygon

Install

Install from NPM:

npm install @kninnug/trivis

Use in Node.js:

const triangularExpansion = require('@kninnug/trivis');

or as an ECMAScript/ES6 module:

import triangularExpansion from '@kninnug/trivis';

or in the browser:

<script src="node_modules/@kninnug/trivis/lib/TriVis.js"></script>

or minified:

<script src="node_modules/@kninnug/trivis/lib/TriVis.min.js"></script>

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

Usage

poly = triangularExpansion(del, qx, qy, obstructs, ilx = NaN, ily = NaN, irx = NaN, iry = NaN)

Parameters:

  • del: The triangulation in the format that Delaunator outputs.
  • qx, qy: The coordinates of the query point.
  • obstructs: A callback that receives an edge id of the triangulation and must indicate whether it obstructs the view. Edges on the hull of the triangulation are always considered to be obstructing.
  • ilx, ily, irx, iry: If given, i.e. not NaN, the coordinates of the left and right points restricting the viewing cone. The angles between these points and the query point should not be greater than 180°. If these arguments are not given, the visibility polygon is computed in all directions.

Return value:

An array of 4-element arrays, [lx, ly, rx, ry] with the coordinates of the left- and right-hand side end-points of the segments that make up the visibility polygon. Each triplet (lx, ly) (qx, qy) (rx, ry) forms a counter-clockwise triangle that is entirely visible from the query point. The segments are also ordered counter-clockwise around (qx, qy).

Changes

2.0.0

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

1.0.1

  • Update dependencies.
  • Move test files to separate repository.

1.0.0

  • Initial version.

Attributions