Skip to content

Commit

Permalink
build(geom-clip): rename pkg geom-clip-convex => geom-clip, update deps
Browse files Browse the repository at this point in the history
- update sutherlandHodgeman() (boundary centroid handling, corner2() reuse)
  • Loading branch information
postspectacular committed Jan 28, 2019
1 parent 6391d10 commit 004d7f4
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 19 deletions.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @thi.ng/geom-clip-convex
# @thi.ng/geom-clip

[![npm (scoped)](https://img.shields.io/npm/v/@thi.ng/geom-clip-convex.svg)](https://www.npmjs.com/package/@thi.ng/geom-clip-convex)
![npm downloads](https://img.shields.io/npm/dm/@thi.ng/geom-clip-convex.svg)
[![npm (scoped)](https://img.shields.io/npm/v/@thi.ng/geom-clip.svg)](https://www.npmjs.com/package/@thi.ng/geom-clip)
![npm downloads](https://img.shields.io/npm/dm/@thi.ng/geom-clip.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
Expand All @@ -25,7 +25,7 @@ TODO...
## Installation

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

## Dependencies
Expand All @@ -35,7 +35,7 @@ yarn add @thi.ng/geom-clip-convex
## Usage examples

```ts
import * as gc from "@thi.ng/geom-clip-convex";
import * as gc from "@thi.ng/geom-clip";
```

## Authors
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@thi.ng/geom-clip-convex",
"name": "@thi.ng/geom-clip",
"version": "0.0.1",
"description": "2D line & convex polygon clipping (Liang-Barsky / Sutherland-Hodgeman)",
"module": "./index.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-convex",
"homepage": "https://github.com/thi-ng/umbrella/tree/master/packages/geom-clip",
"author": "Karsten Schmidt <k+npm@thi.ng>",
"license": "Apache-2.0",
"scripts": {
Expand All @@ -33,6 +33,7 @@
},
"dependencies": {
"@thi.ng/geom-isec": "^0.0.1",
"@thi.ng/geom-poly-utils": "^0.0.1",
"@thi.ng/math": "^1.0.1",
"@thi.ng/vectors": "^2.1.0"
},
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { intersectLineLine } from "@thi.ng/geom-isec";
import { EPS, sign } from "@thi.ng/math";
import { ReadonlyVec, signedArea2 } from "@thi.ng/vectors";
import { centroid } from "@thi.ng/geom-poly-utils";
import { EPS } from "@thi.ng/math";
import { corner2, ReadonlyVec } from "@thi.ng/vectors";

/**
* Extended version of Sutherland-Hodgeman convex polygon clipping
* supporting any convex boundary (not only rects). Returns new array of
* clipped vertices.
* supporting any convex boundary polygon (not only rects). Returns new
* array of clipped vertices.
*
* https://en.wikipedia.org/wiki/Sutherland%E2%80%93Hodgman_algorithm
*
Expand All @@ -15,17 +16,18 @@ import { ReadonlyVec, signedArea2 } from "@thi.ng/vectors";
* @param eps edge classification tolerance
*/
export const sutherlandHodgeman =
(pts: ReadonlyVec[], bounds: ReadonlyVec[], bc: ReadonlyVec, eps = 1e-4) => {
(pts: ReadonlyVec[], bounds: ReadonlyVec[], bc?: ReadonlyVec, eps = EPS) => {
bc = bc || centroid(bounds);
for (let ne = bounds.length, j = ne - 1, i = 0; i < ne; j = i, i++) {
const clipped = [];
const ca = bounds[j];
const cb = bounds[i];
const sign = corner(ca, cb, bc, eps);
const sign = corner2(ca, cb, bc, eps);
for (let np = pts.length, k = np - 1, l = 0; l < np; k = l, l++) {
const p = pts[k];
const q = pts[l];
const cqsign = corner(ca, cb, q, eps);
if (corner(ca, cb, p, eps) === sign) {
const cqsign = corner2(ca, cb, q, eps);
if (corner2(ca, cb, p, eps) === sign) {
clipped.push(
cqsign !== sign ?
intersectLineLine(ca, cb, p, q).isec :
Expand All @@ -43,6 +45,3 @@ export const sutherlandHodgeman =
return pts;
};

const corner =
(a: ReadonlyVec, b: ReadonlyVec, c: ReadonlyVec, eps = EPS) =>
sign(signedArea2(a, b, c), eps);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import * as assert from "assert";
// import * as gc from "../src/index";

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

0 comments on commit 004d7f4

Please sign in to comment.