Skip to content

Commit

Permalink
feat(geom-clip-line): extract as own pkg (from @thi.ng/geom-clip)
Browse files Browse the repository at this point in the history
- add liangBarsky2Raw
  • Loading branch information
postspectacular committed Feb 15, 2020
1 parent 57a7487 commit 34e3262
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 55 deletions.
Expand Up @@ -3,12 +3,13 @@
.meta
.nyc_output
*.gz
*.h
*.html
*.o
*.svg
*.tgz
*.h
*.o
*.wasm
*.tpl.md
bench
build
coverage
Expand All @@ -18,6 +19,4 @@ export
src*
test
tools
api-extractor.json
README.tpl.md
tsconfig.json
File renamed without changes.
52 changes: 52 additions & 0 deletions packages/geom-clip-line/README.md
@@ -0,0 +1,52 @@
<!-- This file is generated - DO NOT EDIT! -->

# ![@thi.ng/geom-clip-line](https://media.thi.ng/umbrella/banners/thing-geom-clip-line.svg?1581809314)

[![npm version](https://img.shields.io/npm/v/@thi.ng/geom-clip-line.svg)](https://www.npmjs.com/package/@thi.ng/geom-clip-line)
![npm downloads](https://img.shields.io/npm/dm/@thi.ng/geom-clip-line.svg)
[![Twitter Follow](https://img.shields.io/twitter/follow/thing_umbrella.svg?style=flat-square&label=twitter)](https://twitter.com/thing_umbrella)

This project is part of the
[@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo.

- [About](#about)
- [Status](#status)
- [Installation](#installation)
- [Dependencies](#dependencies)
- [API](#api)
- [Authors](#authors)
- [License](#license)

## About

2D line clipping (Liang-Barsky).

### Status

**ALPHA** - bleeding edge / work-in-progress

## Installation

```bash
yarn add @thi.ng/geom-clip-line
```

Package sizes (gzipped): CJS: 0.4KB

## Dependencies

- [@thi.ng/vectors](https://github.com/thi-ng/umbrella/tree/develop/packages/vectors)

## API

[Generated API docs](https://docs.thi.ng/umbrella/geom-clip-line/)

TODO

## Authors

Karsten Schmidt

## License

&copy; 2020 Karsten Schmidt // Apache Software License 2.0
Expand Up @@ -13,9 +13,6 @@ This project is part of the

${pkg.description}

Current implementation is based on [toxiclibs](http://toxiclibs.org)
(Java) and Clojure version of [thi.ng/geom](http://thi.ng/geom).

${status}

${supportPackages}
Expand Down
File renamed without changes.
@@ -1,7 +1,7 @@
{
"name": "@thi.ng/geom-clip",
"version": "0.1.12",
"description": "2D line & convex polygon clipping (Liang-Barsky / Sutherland-Hodgeman)",
"name": "@thi.ng/geom-clip-line",
"version": "0.0.1",
"description": "2D line clipping (Liang-Barsky)",
"module": "./index.js",
"main": "./lib/index.js",
"umd:main": "./lib/index.umd.js",
Expand All @@ -10,7 +10,7 @@
"type": "git",
"url": "https://github.com/thi-ng/umbrella.git"
},
"homepage": "https://github.com/thi-ng/umbrella/tree/master/packages/geom-clip",
"homepage": "https://github.com/thi-ng/umbrella/tree/master/packages/geom-clip-line",
"author": "Karsten Schmidt <k+npm@thi.ng>",
"license": "Apache-2.0",
"scripts": {
Expand All @@ -20,10 +20,10 @@
"build:test": "rimraf build && tsc -p test/tsconfig.json",
"test": "mocha test",
"cover": "nyc mocha test && nyc report --reporter=lcov",
"clean": "rimraf *.js *.d.ts .nyc_output build coverage doc lib",
"clean": "rimraf *.js *.d.ts *.map .nyc_output build coverage doc lib",
"doc:readme": "../../scripts/generate-readme",
"doc": "node_modules/.bin/typedoc --mode modules --out doc src",
"doc:ae": "mkdir -p .ae/doc .ae/temp && node_modules/.bin/api-extractor run --local --verbose",
"doc": "node_modules/.bin/typedoc --mode modules --out doc src",
"pub": "yarn build:release && yarn publish --access public"
},
"devDependencies": {
Expand All @@ -38,30 +38,18 @@
"typescript": "^3.7.5"
},
"dependencies": {
"@thi.ng/geom-isec": "^0.4.1",
"@thi.ng/geom-poly-utils": "^0.1.30",
"@thi.ng/math": "^1.6.0",
"@thi.ng/vectors": "^4.0.3"
},
"keywords": [
"2D",
"convex",
"clipping",
"ES6",
"geometry",
"liang-barsky",
"line",
"polygon",
"rect",
"rectangle",
"sutherland-hodgeman",
"typescript"
],
"publishConfig": {
"access": "public"
},
"sideEffects": false,
"thi.ng": {
"year": 2013
"status": "alpha",
"year": 2020
}
}
@@ -1,4 +1,3 @@
import { EPS } from "@thi.ng/math";
import { Vec } from "@thi.ng/vectors";

/**
Expand Down Expand Up @@ -26,18 +25,58 @@ export const liangBarsky2 = (
b: Vec,
min: Vec,
max: Vec,
ca?: Vec,
cb?: Vec
ca: Vec = [],
cb: Vec = []
): [Vec, Vec, number, number] | undefined => {
const ax = a[0];
const ay = a[1];
const dx = b[0] - ax;
const dy = b[1] - ay;
const res = liangBarsky2Raw(
a[0],
a[1],
b[0],
b[1],
min[0],
min[1],
max[0],
max[1]
);
if (!res) return;

ca[0] = res[0];
ca[1] = res[1];
cb[0] = res[2];
cb[1] = res[3];

return [ca, cb, res[4], res[5]];
};

/**
* Same as {@link liangBarsky2} but for non-vector arguments.
*
* @param ax
* @param ay
* @param bx
* @param by
* @param minx
* @param miny
* @param maxx
* @param maxy
*/
export const liangBarsky2Raw = (
ax: number,
ay: number,
bx: number,
by: number,
minx: number,
miny: number,
maxx: number,
maxy: number
): [number, number, number, number, number, number] | undefined => {
const dx = bx - ax;
const dy = by - ay;
let alpha = 0;
let beta = 1;

const clip = (p: number, q: number) => {
if (q < 0 && Math.abs(p) < EPS) {
if (q < 0 && Math.abs(p) < 1e-6) {
return false;
}
const r = q / p;
Expand All @@ -57,24 +96,17 @@ export const liangBarsky2 = (
return true;
};

if (
!(
clip(-dx, -(min[0] - ax)) &&
clip(dx, max[0] - ax) &&
clip(-dy, -(min[1] - ay)) &&
clip(dy, max[1] - ay)
)
) {
return;
}

!ca && (ca = []);
!cb && (cb = []);

ca[0] = alpha * dx + ax;
ca[1] = alpha * dy + ay;
cb[0] = beta * dx + ax;
cb[1] = beta * dy + ay;

return [ca, cb, alpha, beta];
return clip(-dx, -(minx - ax)) &&
clip(dx, maxx - ax) &&
clip(-dy, -(miny - ay)) &&
clip(dy, maxy - ay)
? [
alpha * dx + ax,
alpha * dy + ay,
beta * dx + ax,
beta * dy + ay,
alpha,
beta
]
: undefined;
};
6 changes: 6 additions & 0 deletions packages/geom-clip-line/test/index.ts
@@ -0,0 +1,6 @@
// import * as assert from "assert";
// import { } from "../src";

describe("geom-liangbarsky", () => {
it("tests pending");
});
File renamed without changes.
File renamed without changes.

0 comments on commit 34e3262

Please sign in to comment.