# polylabel **Repository Path**: ArkTSCentralRepository/polylabel ## Basic Information - **Project Name**: polylabel - **Description**: polylabel 是一个高效算法库,用于在多边形中找到最远离边界的内部点(不可达性极点),适用于多边形内文本标签的最佳位置计算,现已适配为 ArkTS 文件。 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-11-21 - **Last Updated**: 2024-11-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # polylabel 基于[polylabel](https://www.npmjs.com/package/polylabel)原库2.0.1版本进行适配, 所有功能代码已经转换为`ArkTS`文件 ## Install ```sh ohpm install polylabel ``` ## Description A fast algorithm for finding polygon _pole of inaccessibility_, the most distant internal point from the polygon outline (not to be confused with centroid), implemented as a ArkTS library. Useful for optimal placement of a text label on a polygon. It's an iterative grid algorithm, inspired by [paper by Garcia-Castellanos & Lombardo, 2007](https://sites.google.com/site/polesofinaccessibility/). Unlike the one in the paper, this algorithm: - guarantees finding **global optimum** within the given precision - is many times faster (10-40x) ![](https://cloud.githubusercontent.com/assets/25395/16745865/864a0a30-47c0-11e6-87bc-58acac41a520.png) ### How the algorithm works This is an iterative grid-based algorithm, which starts by covering the polygon with big square cells and then iteratively splitting them in the order of the most promising ones, while aggressively pruning uninteresting cells. 1. Generate initial square cells that fully cover the polygon (with cell size equal to either width or height, whichever is lower). Calculate distance from the center of each cell to the outer polygon, using negative value if the point is outside the polygon (detected by ray-casting). 2. Put the cells into a priority queue sorted by the maximum potential distance from a point inside a cell, defined as a sum of the distance from the center and the cell radius (equal to `cell_size * sqrt(2) / 2`). 3. Calculate the distance from the centroid of the polygon and pick it as the first "best so far". 4. Pull out cells from the priority queue one by one. If a cell's distance is better than the current best, save it as such. Then, if the cell potentially contains a better solution that the current best (`cell_max - best_dist > precision`), split it into 4 children cells and put them in the queue. 5. Stop the algorithm when we have exhausted the queue and return the best cell's center as the pole of inaccessibility. It will be guaranteed to be a global optimum within the given precision. ![image](https://cloud.githubusercontent.com/assets/25395/16748630/e6b3336c-47cd-11e6-8059-0eeccf22cf6b.png) ### Usage Given polygon coordinates in [GeoJSON-like format](http://geojson.org/geojson-spec.html#polygon) (an array of arrays of `[x, y]` points) and precision (`1.0` by default), Polylabel returns the pole of inaccessibility coordinate in `[x, y]` format. The distance to the closest polygon point (in input units) is included as a `distance` property. ```typescript import polylabel from 'polylabel'; import { Result } from 'polylabel'; const p: Result = polylabel([[[0, 0], [1, 0], ...]], 1.0); const distance: number = p.distance; ``` Be careful to pick precision appropriate for the input units. E.g. in case of geographic coordinates (longitude and latitude), `0.000001` is appropriate, while the default (`1.0`) would be too imprecise. #### Ports to other languages - [andrewharvey/geojson-polygon-labels](https://github.com/andrewharvey/geojson-polygon-labels) (CLI) - [Twista/python-polylabel](https://github.com/Twista/python-polylabel) (Python) - [Shapely](https://github.com/Toblerity/Shapely/blob/master/shapely/algorithms/polylabel.py) (Python) - [polylabelr](https://CRAN.R-project.org/package=polylabelr) (R) - [polylabel-rs](https://github.com/urschrei/polylabel-rs) (Rust) - [polylabel-java](https://github.com/FreshLlamanade/polylabel-java) (Java) - [php-polylabel](https://github.com/dliebner/php-polylabel) (PHP) - [dart_polylabel](https://github.com/beroso/dart_polylabel) (Dart) - [ruby-polylabel](https://github.com/fredplante/ruby-polylabel) (Ruby) - [Polylabel.jl](https://github.com/asinghvi17/Polylabel.jl) (Julia)