Skip to content

geometryjs/geometry.js

Geometry JS

Geometry JS is a JavaScript/TypeScript library for creating and manipulating 2D geometry objects.

Table of contents

Getting started

Install the library using npm:

npm install @jiricekcz/geometry-js

Import the library in your code:

import * as GeometryJS from '@jiricekcz/geometry-js';

Documentation

The API documentation is available at https://geometryjs.jiricekcz.dev/. For more indepth information, see the wiki.

Requirements

Geometry JS requires a JavaScript runtime with support for ES6. It is a zero-dependency TypeScript library, so it can be used in any JavaScript project (Node.js, React, Angular, Vue, etc.). For more information about requirements, see the wiki (Requirements).

Usage

After importing the library, getting started is easy.
First, we need to create the main Plane object. This object represents the 2D plane on which all geometry objects are created.

import * as GeometryJS from '@jiricekcz/geometry-js';

// Create the main Plane object
const plane = GeometryJS.createPlane();

After that, we can create some geometry objects. For example, let's create a value (variable) x and a readonly value y.

// Create a value (variable) x
const x = plane.createValue(1);

// Create a readonly value y
const y = plane.createReadonlyValue(2);

Now, we can create a point A with coordinates x and y.

// Create a point A with coordinates x and y
const A = plane.createPointFromTwoValues(x, y);

We can also create a point B with coordinates 3 and 4.

// Create a point B with coordinates 3 and 4
const B = plane.createPointFromCoordinates(3, 4);

Now, we can create a line l from points A and B.

// Create a line l from points A and B
const l = plane.createLineFromTwoPoints(A, B);

We can also create a point O at the origin of the plane.

// Create a point O at the origin of the plane
const O = plane.createPointFromCoordinates(0, 0);

Then we can construct a line perpendicular to AB through O.

// Create a line perpendicular to l through O
const perpendicular = plane.constructPerpendicularLineFromPoint(l, O);

With just this code, we have declaratively defined the geometry. If we change any property of any non-readonly object, the whole geometry will be recalculated.

The whole code with minimal comments:

import * as GeometryJS from '@jiricekcz/geometry-js';

const plane = GeometryJS.createPlane();

const x = plane.createValue(1);
const y = plane.createReadonlyValue(2);

const A = plane.createPointFromTwoValues(x, y);
const B = plane.createPointFromCoordinates(3, 4);

const l = plane.createLineFromTwoPoints(A, B);

const O = plane.createPointFromCoordinates(0, 0);

const perpendicular = plane.constructPerpendicularLineFromPoint(l, O);

Beacuse of the declarative nature and descriptive names of the functions, the code is very readable and easy to understand.