Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let pointArray: Array<[number, number]> = [[10, 10], [20, 20], [10, 30], [15, 15]];
let hull: Array<[number, number]>;
// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------
num = d3Polygon.polygonArea(polygon);
point = d3Polygon.polygonCentroid(polygon);
hull = d3Polygon.polygonHull(pointArray);
containsFlag = d3Polygon.polygonContains(polygon, point);
num = d3Polygon.polygonLength(polygon);
function addPoints(ring: Ring, numPoints: number) {
const desiredLength = ring.length + numPoints;
const step = polygonLength(ring) / numPoints;
let i = 0;
let cursor = 0;
let insertAt = step / 2;
while (ring.length < desiredLength) {
const a = ring[i];
const b = ring[(i + 1) % ring.length];
const segment = MathUtil.distance(a, b);
if (insertAt <= cursor + segment) {
const p = segment ? MathUtil.lerp(a, b, (insertAt - cursor) / segment) : [...a] as Point;
ring.splice(i + 1, 0, p);
insertAt += step;
continue;
}
cursor += segment;
i++;
}
export function addPoints(ring, numPoints) {
const desiredLength = ring.length + numPoints,
step = polygonLength(ring) / numPoints;
let i = 0,
cursor = 0,
insertAt = step / 2;
while (ring.length < desiredLength) {
let a = ring[i],
b = ring[(i + 1) % ring.length],
segment = distance(a, b);
if (insertAt <= cursor + segment) {
ring.splice(
i + 1,
0,
segment ? pointAlong(a, b, (insertAt - cursor) / segment) : a.slice(0)
);
export function addPoints(ring: Ring, numPoints: number, maxLength = Infinity) {
const desiredLength = ring.length + numPoints;
const step = polygonLength(ring) / numPoints;
let i = 0;
let cursor = 0;
let insertAt = step / 2;
while (ring.length < desiredLength) {
const a = ring[i];
const b = ring[(i + 1) % ring.length];
const segment = distance(a, b);
if (insertAt <= cursor + segment) {
ring.splice(
i + 1,
0,
segment ? pointAlong(a, b, (insertAt - cursor) / segment) : a.slice(0) as Point,
);
return function(ring) {
let centroid = polygonCentroid(ring),
perimeter = polygonLength([...ring, ring[0]]),
startingAngle = Math.atan2(
ring[0][1] - centroid[1],
ring[0][0] - centroid[0]
),
along = 0;
if (startingAngle < 0) {
startingAngle = 2 * Math.PI + startingAngle;
}
let startingProgress = startingAngle / (2 * Math.PI);
return ring.map((point, i) => {
if (i) {
along += distance(point, ring[i - 1]);
}