# liang-barsky

> Liang-Barsky line clipping algorithm

Latest version **1.0.12** (published 2024-09-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install liang-barsky
pnpm add liang-barsky
yarn add liang-barsky
bun add liang-barsky
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.12 |
| Published | 2024-09-26 |
| First published | 2017-04-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 33 |
| Author | Alexander Milevski |
| Maintainers | w8r |
| Keywords | liang-barsky, line, clipping, geometry, algorithm |

## Links

- npm: https://www.npmjs.com/package/liang-barsky
- Repository: https://github.com/w8r/liang-barsky
- Homepage: https://github.com/w8r/liang-barsky#readme
- Issues: https://github.com/w8r/liang-barsky/issues
- npm.io page: https://npm.io/package/liang-barsky

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 1.0.12 (latest) — 2024-09-26
- 1.0.5 — 2021-06-02
- 1.0.4 — 2018-12-30
- 1.0.3 — 2018-12-30
- 1.0.1 — 2017-12-28
- 1.0.0 — 2017-04-13

## README

# Liang-Barsky line-clipping algorithm [![npm](https://badge.fury.io/js/liang-barsky.svg)](https://www.npmjs.com/package/liang-barsky)

![Preview](https://w8r.github.io/liang-barsky/thumbnail.png)

Fast, _destructive_ implemetation of [Liang-Barsky line clipping algorithm](https://en.wikipedia.org/wiki/Liang%E2%80%93Barsky_algorithm). It clips a 2D segment by a rectangle.

This is an adaptation of the [C++ code](http://hinjang.com/articles/04.html#eight)
that impressed me by its simplicity.

## API

Destructive

```js
const 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

```js
const a = [-10, -10],
  b = [10, 10];
const 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
```

```js
import { clip } from 'liang-barsky';
// or
var clip = require('liang-barsky');
```

Or just drop-in the file

```html
<script src="path/to/liang-barsky.umd.js"></script>
<script>
  liangBarsky.clip([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 112,058,856 ops/sec ±6.46% (87 runs sampled)
mapbox/lineclip x 27,754,592 ops/sec ±1.94% (98 runs sampled)
- Fastest is liang-barsky
```

## Future plan

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

## License

MIT

---
_Source: https://npm.io/package/liang-barsky · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
