# visibility-polygon

> A library used to construct a visibility polygon for a set of line segments

Latest version **1.1.0** (published 2020-10-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install visibility-polygon
pnpm add visibility-polygon
yarn add visibility-polygon
bun add visibility-polygon
```

## 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.1.0 |
| Published | 2020-10-07 |
| First published | 2020-05-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 183.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Liang Chun Wong |
| Maintainers | liangchun |
| Keywords | visibility, polygon, geometry |

## Links

- npm: https://www.npmjs.com/package/visibility-polygon
- Repository: https://github.com/liangchunn/visibility-polygon
- Homepage: https://github.com/liangchunn/visibility-polygon#readme
- Issues: https://github.com/liangchunn/visibility-polygon/issues
- npm.io page: https://npm.io/package/visibility-polygon

## Recent versions

- 1.1.0 (latest) — 2020-10-07
- 1.0.2 — 2020-05-24
- 1.0.1 — 2020-05-24
- 1.0.0 — 2020-05-24

## README

# visibility-polygon ![](https://github.com/liangchunn/visibility-polygon/workflows/CI/badge.svg) [![npm](https://img.shields.io/npm/v/visibility-polygon.svg)](https://www.npmjs.com/package/visibility-polygon) [![bundle size](https://badgen.net/bundlephobia/min/visibility-polygon)](https://bundlephobia.com/result?p=visibility-polygon)

A library used to construct a visibility polygon for a set of line segments.

## Demo

Original demo by Byron Knoll: http://www.byronknoll.com/visibility.html

## Performance

The time complexity of this implementation is `O(n log n)` (where `n` is the total number of line segments). This is the optimal time complexity for this problem.

## Quick Start

### Installing

```sh
# npm
npm install --save visibility-polygon

# yarn
yarn add visibility-polygon
```

### Example Usage

```ts
import {
  breakIntersections,
  convertToSegments,
  compute,
  computeViewport,
  inPolygon,
  Position,
  Polygon,
} from 'visibility-polygon';

const polygons: Polygon[] = [];
// this is the 'world' polygon, which bounds all the polygons you want to compute againts
polygons.push([
  [-1, -1],
  [501, -1],
  [501, 501],
  [-1, 501],
]);
// define vertexes of your polygons
polygons.push([
  [250, 100],
  [260, 140],
  [240, 140],
]);

const segments = breakIntersections(convertToSegments(polygons));

// define your position in which the visibility should be calculated from
const position: Position = [60, 60];

// check if the position is inside the world polygon
if (inPolygon(position, polygons[0])) {
  // compute the visibility polygon, this can be used to draw a polygon with Canvas or WebGL
  const visibility = compute(position, segments);
}
const viewportVisibility = computeViewport(
  position,
  segments,
  [50, 50],
  [450, 450]
);
```

Detailed API information can be found in the usage of the methods in your code-editor (JSDoc).

## Credits

Original source code by Byron Knoll (@byronknoll) on https://github.com/byronknoll/visibility-polygon-js

This version of the library adds TypeScript support and re-implements it in an ESM module, while supporting CommonJS as well.

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