A 2d geometry library written in Typescript.
You can install shapetypes with npm:
npm install shapetypes
import { Point, Polyline, Polygon, Rectangle, Vector } from 'shapetypes';
// Create a triangle
const triangle = new Polyline([new Point(0, 0), new Point(1, 1), new Point(2, 0)], true);
console.log(triangle.area);
// => 1
// Test to see if a point is inside
const testPoint = new Point(1, 0.5);
console.log(triangle.contains(testPoint));
// => True
// Move the triangle and see if the point is inside
const shifted = triangle.translate(new Vector(3, 4));
console.log(shifted.contains(testPoint));
// => False
// Create a rectangular polygon
const rectangle = new Rectangle(Plane.worldXY(), 10, 20);
const polygon = new Polygon(rectangle.toPolyline());
console.log(polygon.area);
// => 200
// Cut the triangle from the polygon
const result = polygon.difference(triangle);
console.log(result[0].area);
// => 199
Try on Runkit (note: Runkit is in Javascript rather than Typescript).
If you've used Rhino, the Shapetypes syntax should feel familiar. There are a couple of important differences though:
import {Point} from 'shapetypes';
// Create a new point at 3,4
const p = new Point(3,4);
// Access the x parameter
console.log(p.x);
// => 3
// Try to change the x parameter
p.x = 5;
// => This will throw an error since objects can't be modified after they're created.
// Instead, create a copy with a different x value.
const newPoint = p.withX(5);
console.log(newPoint.x);
// => 5
Package based on Typescript Starter. Booleans via the Polygon-clipping library. This project is not affiliated with Rhino3d or McNeel in any way.
Generated using TypeDoc