All Projects → w8r → liang-barsky

w8r / liang-barsky

Licence: MIT license
Liang-Barsky line-clipping algorithm

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to liang-barsky

Unity3D-Coding-Examples
Various case-studies in Unity3D
Stars: ✭ 91 (+184.38%)
Mutual labels:  geometry
classy blocks
Python classes for easier creation of OpenFOAM's blockMesh dictionaries.
Stars: ✭ 53 (+65.63%)
Mutual labels:  geometry
vg
Vector-geometry toolbelt for 3D points and vectors
Stars: ✭ 106 (+231.25%)
Mutual labels:  geometry
geos-cli
A native geometry command line library using libgeos.
Stars: ✭ 20 (-37.5%)
Mutual labels:  geometry
vec
Vector graphics software to generate HPGL output to drive a plotter
Stars: ✭ 19 (-40.62%)
Mutual labels:  geometry
EsriRESTScraper
A Python class that scrapes ESRI Rest Endpoints and exports data to a geodatabase
Stars: ✭ 43 (+34.38%)
Mutual labels:  geometry
vexed-generation
Polymorphic helper functions & geometry ops for Houdini VEX / OpenCL
Stars: ✭ 32 (+0%)
Mutual labels:  geometry
ncdfgeom
NetCDF-CF Geometry and Timeseries Tools for R: https://code.usgs.gov/water/ncdfgeom
Stars: ✭ 13 (-59.37%)
Mutual labels:  geometry
Mathematics for Machine Learning
Learn mathematics behind machine learning and explore different mathematics in machine learning.
Stars: ✭ 28 (-12.5%)
Mutual labels:  geometry
dmc
Dual Marching Cubes Implementation in C++
Stars: ✭ 45 (+40.63%)
Mutual labels:  geometry
GIBBON
The Geometry and Image-Based Bioengineering add-On for MATLAB
Stars: ✭ 132 (+312.5%)
Mutual labels:  geometry
topo
A Geometry library for Elixir that calculates spatial relationships between two geometries
Stars: ✭ 125 (+290.63%)
Mutual labels:  geometry
martinez-src
Mirrored implementations of polygon clipping/CSG/operations algorithm, in C (original, by Martínez et al) and ActionScript3 (port, by Mahir Iqbal)
Stars: ✭ 34 (+6.25%)
Mutual labels:  geometry
DynamicalBilliards.jl
An easy-to-use, modular, extendable and absurdly fast Julia package for dynamical billiards in two dimensions.
Stars: ✭ 97 (+203.13%)
Mutual labels:  geometry
mikktspace-wasm
MikkTSpace vertex tangent calculation for JavaScript/TypeScript/Node.js, using Web Assembly.
Stars: ✭ 19 (-40.62%)
Mutual labels:  geometry
elm-geometry-svg
Render 2D elm-geometry types as SVG
Stars: ✭ 40 (+25%)
Mutual labels:  geometry
batyr
Microservice for on-demand synchronization of geographical vector datasources to a PostgreSQL/PostGIS database. The service provides an HTTP API for easy integration into other applications.
Stars: ✭ 25 (-21.87%)
Mutual labels:  geometry
polliwog
2D and 3D computational geometry library
Stars: ✭ 22 (-31.25%)
Mutual labels:  geometry
geofeatures2
A lightweight, high performance geometry library in Swift.
Stars: ✭ 18 (-43.75%)
Mutual labels:  geometry
variational-surface-cutting
Codebase for "Variational Surface Cutting" by Sharp & Crane, SIGGRAPH 2018
Stars: ✭ 64 (+100%)
Mutual labels:  geometry

Liang-Barsky line-clipping algorithm npm

Preview

Fast, destructive implemetation of Liang-Barsky line clipping algorithm. It clips a 2D segment by a rectangle.

This is an adaptation of the C++ code that impressed me by its simplicity.

API

Destructive

var a = [-10, -10], b = [10, 10];
clip(a, b, [-5, -5, 5, 5]); // returns 1 - "clipped"
console.log(a);             // [-5, -5]
console.log(b);             // [5, 5]

Non-destructive

var a  = [-10, -10], b  = [10, 10];
var an = a.slice(),  bn = b.slice();
clip(a, b, [-5, -5, 5, 5], an, bn); // returns 1 - "clipped"
console.log(an);              // [-5, -5]
console.log(bn);              // [5, 5]
console.log(a);               // [-10, -10]
console.log(b);               // [10, 10]

Return value is 1 if the line was clipped, and 0 if it lies completely outside of the provided bounding box.

Install

npm install -S liang-barsky
import clip from 'liang-barsky';
// or
var clip = require('liang-barsky');

Or just drop-in the file

<script src="path/to/liang-barsky.umd.js"></script>
<script>
  liangBarsky([0, 0], [10, 10], [0, 0, 5, 5]);
</script>

Performance

I ran a check against the Cohen-Sutherland algorithm implemented by @mourner for clipping just one segment. Though test include memory allocation, they are fair for the task at hand, since you can use the results in an equal manner after the invocation of the clipper.

npm run benchmark
liang-barsky x 136,671,492 ops/sec ±0.36% (90 runs sampled)
mapbox/lineclip x 9,030,039 ops/sec ±0.84% (86 runs sampled)
- Fastest is liang-barsky

Future plan

Implement a sub-routine for polylines. Loop through pairs, tracking in-out transitions.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].