mirror of
https://github.com/Turfjs/turf.git
synced 2025-12-08 20:26:16 +00:00
* Added JSDoc for internal types and constants e.g. Unts and earthRadius. Minimal other changes to bring JSDoc types into line with code types. Added GeoJsonProperties to documentation.yml for type linking. * Generated README.md files based on updated source that now includes JSDoc for internal types and constants e.g. Units. Also synced up some out of data JSDoc types with what is in the code. * Switched the structure of documentation.yml to be more traditionally nested. We'll use this new structure from the turf-www repo to generate the website documentation in a more robust manner. * For some reason leaving this function documented as the default (geojsonRbush) causes @turf/turf last-checks to fail. Specifically defining it as rbush like it used to be, except with the @function tag rather than @name.
42 lines
1.2 KiB
TypeScript
42 lines
1.2 KiB
TypeScript
import { Feature, Polygon } from "geojson";
|
|
import { getGeom } from "@turf/invariant";
|
|
|
|
/**
|
|
* Takes a polygon and return true or false as to whether it is concave or not.
|
|
*
|
|
* @function
|
|
* @param {Feature<Polygon>} polygon to be evaluated
|
|
* @returns {boolean} true/false
|
|
* @example
|
|
* var convexPolygon = turf.polygon([[[0,0],[0,1],[1,1],[1,0],[0,0]]]);
|
|
*
|
|
* turf.booleanConcave(convexPolygon)
|
|
* //=false
|
|
*/
|
|
function booleanConcave(polygon: Feature<Polygon> | Polygon) {
|
|
// Taken from https://stackoverflow.com/a/1881201 & https://stackoverflow.com/a/25304159
|
|
const coords = getGeom(polygon).coordinates;
|
|
if (coords[0].length <= 4) {
|
|
return false;
|
|
}
|
|
|
|
let sign = false;
|
|
const n = coords[0].length - 1;
|
|
for (let i = 0; i < n; i++) {
|
|
const dx1 = coords[0][(i + 2) % n][0] - coords[0][(i + 1) % n][0];
|
|
const dy1 = coords[0][(i + 2) % n][1] - coords[0][(i + 1) % n][1];
|
|
const dx2 = coords[0][i][0] - coords[0][(i + 1) % n][0];
|
|
const dy2 = coords[0][i][1] - coords[0][(i + 1) % n][1];
|
|
const zcrossproduct = dx1 * dy2 - dy1 * dx2;
|
|
if (i === 0) {
|
|
sign = zcrossproduct > 0;
|
|
} else if (sign !== zcrossproduct > 0) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
export { booleanConcave };
|
|
export default booleanConcave;
|