Skip to content

Commit

Permalink
more precision
Browse files Browse the repository at this point in the history
related: #140
  • Loading branch information
Fil committed Aug 18, 2020
1 parent 2f59a1e commit 0c6d91a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/centroid.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Adder} from "d3-array";
import {asin, atan2, cos, degrees, epsilon, epsilon2, radians, sin, sqrt} from "./math.js";
import {asin, atan2, cos, degrees, epsilon, epsilon2, hypot, radians, sin, sqrt} from "./math.js";
import noop from "./noop.js";
import stream from "./stream.js";

Expand Down Expand Up @@ -102,7 +102,7 @@ function centroidRingPoint(lambda, phi) {
cx = y0 * z - z0 * y,
cy = z0 * x - x0 * z,
cz = x0 * y - y0 * x,
m = sqrt(cx * cx + cy * cy + cz * cz),
m = hypot(cx, cy, cz),
w = asin(m), // line weight = angle
v = m && -w / m; // area weight multiplier
X2.add(v * cx);
Expand All @@ -127,17 +127,17 @@ export default function(object) {
var x = +X2,
y = +Y2,
z = +Z2,
m = x * x + y * y + z * z;
m = hypot(x, y, z);

// If the area-weighted ccentroid is undefined, fall back to length-weighted ccentroid.
if (m < epsilon2) {
x = X1, y = Y1, z = Z1;
// If the feature has zero length, fall back to arithmetic mean of point vectors.
if (W1 < epsilon) x = X0, y = Y0, z = Z0;
m = x * x + y * y + z * z;
m = hypot(x, y, z);
// If the feature still has an undefined ccentroid, then return.
if (m < epsilon2 * epsilon2) return [NaN, NaN];
if (m < epsilon2) return [NaN, NaN];
}

return [atan2(y, x) * degrees, asin(z / sqrt(m)) * degrees];
return [atan2(y, x) * degrees, asin(z / m) * degrees];
}
1 change: 1 addition & 0 deletions src/math.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export var cos = Math.cos;
export var ceil = Math.ceil;
export var exp = Math.exp;
export var floor = Math.floor;
export var hypot = Math.hypot;
export var log = Math.log;
export var pow = Math.pow;
export var sin = Math.sin;
Expand Down

0 comments on commit 0c6d91a

Please sign in to comment.