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

twodee

v0.2.5

Published

Two dimensional geometry manipulation

Downloads

7

Readme

#twodee

Two dimensional geometry manipulation

##Install

$ npm install twodee

##Use

###Creating a simple Triangle

twodee uses xyzw for vector manipulation.

$ npm install xyzw
import Vector2 from 'xyzw/source/Vector2';
import Triangle2 from 'twodee/source/Triangle2';

To get es5 safe versions of all files replace /source with /es5

###Creating a triangle

const triangleA = new Triangle2(
	new Vector2([0.0, 0.0]),
	new Vector2([0.0, 1.0]),
	new Vector2([1.0, 0.0])
);

const point0 = triangleA.p0;
const point1 = triangleA.p1;
const point2 = triangleA.p2;
const orientation = triangleA.orientation;
const centroid = triangleA.centroid;
const circumcenter = triangleA.circumcenter
const area = triangleA.area;

####Factory constructors

All primitives come with convenient factory constructors:

const center = new Vector2([0.0, 0.0]);
const radius = 1.0;
const rotation = Math.PI;

const triangleB = Triangle2.Equilateral(center, radius, rotation);

####Collision testing

Triangles can test for collisions with points, segments and other triangles:

const intersections = [];
const collision = triangleA.intersects(triangleB, intersections);

All intersection tests have static versions that do not require actual objects:

const intersections = [];
const collision = Triangle2.intersect(
	triangleA.p0, triangleA.p1, triangleA.p2,
	triangleB.p0, triangleB.p1, triangleB.p2,
	intersections
)

###Creating a ray

import Ray2 from 'twodee/source/Ray2';

const ray = new Ray2(new Vector2([0.0, 0.0]), new Vector2([0.0, 1.0]));

const origin = ray.origin;
const orientation = ray.orientation;

Rays can test for intersections with line segments and other rays. All line intersection tests guarantee valid results for parallel and co-linear entities.

const point0 = new Vector2([4.0, -0.5]);
const point1 = new Vector2([4.0, 0.5]);
const intersection = new Vector2();
const collision = ray.intersectsSegment(point0, point1, intersection);

###Creating a Bounding Box Rectangle

import Vector2 from 'xyzw/source/Vector2';
import Rectangle2 from 'twodee/source/Rectangle2';


const box = Rectangle2.AABB([
	new Vector2([0.0, 0.0]),
	new Vector2([0.0, 1.0]),
	new Vector2([1.0, 0.0])
]);

const transform = box.transform;
const extend = box.extend;
const center = box.center;
const width = box.width;
const height = box.height;
const aspect = box.aspect;
const area = box.area;

Rectangles can test for collisions with points, segments and other rectangles

###Creating a segmented line

import PolyLine2 from 'twodee/source/PolyLine2';


const lineA = PolyLine2.Rectangle2(box);

const points = lineA.point;
const segments = lineA.segments;
const closed = lineA.closed;

Segmented lines can test for collisions with points, line segments and other segment lines. All line intersection test guarantee valid results for parallel and co-linear entities.

const lineB = PolyLine2.ConvexHullGraham([
	new Vector2([0.0, 0.0]),
	new Vector2([0.0, 1.0]),
	new Vector2([1.0, 0.0]),
	new Vector2([1.0, 1.0])
]);

const intersections = [];
const collision = lineB.intersects(lineA, intersections);

###Creating a Polygon

import Polygon2 from 'twodee/source/Polygon2';

const poly = new Polygon2();

const v0 = poly.createVertex(new Vector2([0.0, 0.0]));
const v1 = poly.createVertex(new Vector2([0.0, 1.0]));
const v2 = poly.createVertex(new Vector2([1.0, 0.0]));
const v3 = poly.createVertex(new Vector2([1.0, 1.0]));

const f0 = poly.createFace(v0, v1, v2);
const f1 = poly.createFace(v1, v3, v2);

const center = poly.centroid;
const area = poly.area;

const vertices = poly.vertex;
const edges = poly.edge;
const faces = poly.face;
const point = poly.point;
const drawList = poly.indexList;

Polygons expose a rich api for geometry manipulations:

const [e0] = poly.edgeOfVertex(v1, [v2]);
const e1 = poly.turnEdge(e0);

const [f2, f3] = poly.faceOfEdge(e1);
const [f4, f5, f6] = poly.subdivideFace(f3);

Polygons can test for collisions with Points and other Polygons

const polyB = Polygon2.PolyLine2(lineB);

const intersections = [];
const collision = poly.intersects(polyB);