Available basic shapes in flatten-js
masterThe flatten-js library provides several fundamental 2D geometric primitives:
PointVectorLineRaySegmentArc(circular)CircleBox(can be used as a rectangle)
repository·master·Indexed 20 days ago
https://github.com/alexbol99/flatten-jsA JavaScript library for manipulating abstract 2D geometric shapes including points, vectors, lines, circles, and polygons. It supports affine transformations, distance calculations, intersection finding, and boolean operations on polygons. The library includes a Planar Set for spatial queries and utilizes the DE-9IM model for describing spatial relationships. It provides an svg() method for shape visualization and supports CommonJS, UMD, and ES6 modules.
The flatten-js library provides several fundamental 2D geometric primitives:
PointVectorLineRaySegmentArc (circular)CircleBox (can be used as a rectangle)A Polygon in flatten-js is a multi-polygon consisting of a collection of faces.
For boolean operations and relationship calculations to work correctly, polygons must be orientable:
Use the isValid() method to check if a polygon meets these rules.
You can instantiate a Polygon using:
Flatten.Segment or Flatten.Arc) representing closed chains.Flatten.Point instances.[x, y] representing vertices.Circle or Box.flatten-js is an abstract geometry library and does not handle rendering directly, all shape classes implement an svg() method. This method returns an SVG string that can be inserted into an SVG container in a web browser. It is designed to work well with libraries like d3js.The library uses the Dimensionally Extended nine-Intersection Model (DE-9IM) to describe spatial relations between two geometries.
The model uses a 3x3 matrix representing the intersection of the Interior (I), Boundary (B), and Exterior (E) of two shapes (a and b).
You can access this matrix via Flatten.Relations.relate(shapeA, shapeB). The resulting object contains fields like I2I, B2I, E2I, I2B, etc.
You can get a compact string representation of the matrix (where T is intersection, F is no intersection, and . is not applicable) using the toString() method on the matrix object.
let {relate} = Flatten.Relations;
// define two shapes: polygon1, polygon2
let de9im = relate(polygon1, polygon2);
// explore fields of the de9im matrix:
de9im.I2I de9im.B2I de9im.E2I
de9im.I2B de9im.B2B de9im.E2B
de9im.I2E de9im.B2E // de9im.E2E is N/A
// Get string representation
let matrixString = de9im.toString();To run the demonstration of flatten-js within a React environment, clone the repository and use npm to install dependencies and start the development server. The application renders SVG graphics to demonstrate the library's capabilities.
npm install
npm startYou can use flatten-js directly in modern browsers that support <script type="module" without needing a build step or transpilation. The library can be imported as an ES module via unpkg.
To run the example, simply open the index.html file in a browser. The library will generate SVG graphics based on the geometry logic defined in the script.
<!-- Example of how to include the module in your HTML -->
<script type="module">
import { Polygon } from 'https://unpkg.com/@flatten-js/core';
// Your geometry logic here
</script>To create a production build of the React example, run the following command after installing dependencies:
npm buildInstall the core geometry library using npm to add it to your project dependencies.
npm install --save @flatten-js/coreTo use flatten-js directly in a browser without a build step or transpilation, you can load the package via unpkg. The package is assigned to the window global scope under the name @flatten-js/core. You can then destructure the Flatten namespace from this global object to access the library's geometry functions and classes.
// Accessing the library from the global scope
const { Flatten } = window['@flatten-js/core'];You can import geometry classes directly using named imports, or import the entire namespace as a default import and destructure it. The library supports CommonJS, UMD, and ES6 modules, and includes TypeScript definitions (index.d.ts) for static type checking.
// Named imports
import {Point, Vector, Circle, Line, Ray, Segment, Arc, Box, Polygon, Matrix, PlanarSet} from '@flatten-js/core';
// Default import with destructuring
import Flatten from '@flatten-js/core';
const {Point, Vector, Circle, Line, Ray, Segment, Arc, Box, Polygon, Matrix, PlanarSet} = Flatten;index.html file from the example directory and open it in a modern web browser. The example uses SVG graphics to visualize 2D geometry operations.All shapes can be serialized to JSON strings using JSON.stringify() (which calls the internal .toJSON() method). To reconstruct a shape, parse the JSON and pass the resulting object to the shape's constructor.
let {line, point} = Flatten;
let l = line(point(4, 0), point(0, 4));
// Serialize
let str = JSON.stringify(l);
// Parse and reconstruct
let l_json = JSON.parse(str);
let l_parsed = line(l_json);