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

tritra

v0.1.2

Published

Identify isosceles triangles within a set of 2d points

Downloads

11

Readme

Triangle Tracker

A simple library for identifying isosceles triangles within a set of 2d points.

Isosceles triangle

Motivation

I was working on building an interactive touchscreen table with object recognition features. This concept uses marker discs with a distinct isosceles triangle pattern to distinguish objects, therefore I needed a library to handle all the math for identifying and tracking these triangles.

You can find more information about this project here if you're interested.

Features

  • Identify distinct isosceles triangles within a given set of 2d points
  • Match triangles to pre-defined vertex angles
  • Calculate additional triangle properties like orientation, center, height etc.

Installation

Via NPM (recommended)

$ npm i --save tritra

Alternatively you can simply copy dist/js/tritra.js to your project.

Usage

Include the package in your HTML page:

<script type="text/javascript" src="/path/to/tritra.js"></script>

First create an instance of the triangle recognizer class tritra.Recognizer. This class does all the heavy lifting and you should only create it once during your application lifecycle. To create the recognizer you must supply an array of vertex angles which you'd like the recognizer class to look out for.

// we're looking for triangles with 18, 36 and 54 degrees vertex angles
var vertexAngles = [18, 36, 54];

// create the recognizer
var R = new tritra.Recognizer(vertexAngles);

Define a set of 2d points. The utility class tritra.Vector2d simply represents the X and Y coordinates of a 2d point.

var examplePoints = [
  // defines a triangle with 36 degrees vertex angle
  new tritra.Vector2d(300, 300),
  new tritra.Vector2d(366, 300),
  new tritra.Vector2d(333, 198)
];

Finally try to identify the triangles within your set of points. You will receive an array with all matches as the result value:

var matches = R.findMatches(examplePoints);

Each array item will be a tritra.Match object containing the details of the matching triangle:

var match = matches[0];

// index within your vertexAngles
console.log(match.index);
1

// the matched angle within the given tolerance (not the actual vertex angle)
console.log(match.matchedAngle);
36

// properties of the matched triangle
var centerPoint = match.getCenter();
console.log(centerPoint.x, centerPoint.y);
333, 266

// get the rotation (orientation) of the triangle in degrees
console.log(match.getOrientation());
-180

More properties are available. Check the documentation of tritra.Match or have a look at the included example HTML file.

Documentation

tritra.Recognizer(vertexAngles, [options])

The main recognizer class which will identify triangles for you

vertexAngles Array required

An array of vertex angles of the isosceles triangles you want to identify. You must provide at least one vertex angle.

Parameters

options object {}

Currently supports two options:

maxPointDistance (default: 150): The maximum distance for two points to still be considered as part of the same triangle. You should adjust these according to your screen size and DPI.

As a general rule, the larger the screen and the higher the DPI, the higher you should set this value. Do not set it too high though as this will cause two nearby markers to no longer be recognized.

** maxAngleTolerance (default: 5):** The recognizer will consider vertex angles +- this value (in degrees) as still matching the nearest vertex angle. Do not set this value higher than half of the smallest distance between your configured vertex angles.

Methods

findMatches(points)
points Array

An array of tritra.Vector2d objects containing the points you want to analyze

Returns an array of tritra.Matches objects describing the identified triangles.

tritra.Vector2d(x, y)

Simple wrapper object for a point in 2d space with x and y coordinates.

tritra.Triangle(p1, p2, p3)

Represents a triangle defined by three points in 2d space. Points are expected as tritra.Vector2d objects.

Methods

getOrientation()

Returns the clockwise orientation of the triangle in degrees as seen from its vertex angle. The vertex angle pointing south translates to zero degrees.

getWidth()

Returns the width of the triangle.

getAltitude()

Returns the height of the triangle.

getApexAngle()

Returns the vertex angle (apex angle) of the triangle.

getApex()

Returns a tritra.Vector2d coordinate representing the vertex point (apex point) of the triangle.

getCenter()

Returns a tritra.Vector2d coordinate representing the center point of the triangle.

tritra.Match()

Represents a match found by the recognizer based on a requested vertex angle. You will usually not create instances of this object by yourself but instead the recognizer will create it for you.

Properties

index: The index of the matched angle within your original vertexAngles parameter

matchedAngle: The matched angle. This is not the actual vertex angle but the configured vertex angle within the acceptable "maxAngleTolerance" (see above).

triangle: The raw matched tritra.Triangle object