Skip to content

Commit

Permalink
Slightly faster circular segment area calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanwyatt16 committed May 23, 2017
1 parent 4eaee6e commit 49f3c21
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 24 deletions.
2 changes: 1 addition & 1 deletion index.js
@@ -1,5 +1,5 @@
export {intersectionArea, circleCircleIntersection, circleOverlap, circleArea,
distance, circleIntegral} from "./src/circleintersection";
distance} from "./src/circleintersection";
export {venn, greedyLayout, scaleSolution, normalizeSolution, bestInitialLayout,
lossFunction, disjointCluster, distanceFromIntersectArea} from "./src/layout";
export {VennDiagram, wrapText, computeTextCentres, computeTextCentre, sortAreas,
Expand Down
9 changes: 2 additions & 7 deletions src/circleintersection.js
Expand Up @@ -148,14 +148,9 @@ function getIntersectionPoints(circles) {
return ret;
}

export function circleIntegral(r, x) {
var y = Math.sqrt(r * r - x * x);
return x * y + r * r * Math.atan2(x, y);
}

/** Returns the area of a circle of radius r - up to width */
/** Circular segment area calculation. See http://mathworld.wolfram.com/CircularSegment.html */
export function circleArea(r, width) {
return circleIntegral(r, width - r) - circleIntegral(r, -r);
return r * r * Math.acos(1 - width/r) - (r - width) * Math.sqrt(width * (2 * r - width));
}

/** euclidean distance between two points */
Expand Down
8 changes: 0 additions & 8 deletions tests/unittest.js
Expand Up @@ -62,14 +62,6 @@ tape("greedyLayout", function(test) {
test.end();
});

tape("circleIntegral", function(test) {
nearlyEqual(test, venn.circleIntegral(10, 0), 0, SMALL,
"empty circle test");
nearlyEqual(test, venn.circleIntegral(10, 10), Math.PI * 10 * 10 / 2,
SMALL, "half circle test");
test.end();
});

tape("circleArea", function(test) {
nearlyEqual(test, venn.circleArea(10,0), 0, SMALL, "empty circle test");
nearlyEqual(test, venn.circleArea(10, 10), Math.PI*10*10/2, SMALL,
Expand Down
10 changes: 2 additions & 8 deletions venn.js
Expand Up @@ -154,14 +154,9 @@
return ret;
}

function circleIntegral(r, x) {
var y = Math.sqrt(r * r - x * x);
return x * y + r * r * Math.atan2(x, y);
}

/** Returns the area of a circle of radius r - up to width */
/** Circular segment area calculation. See http://mathworld.wolfram.com/CircularSegment.html */
function circleArea(r, width) {
return circleIntegral(r, width - r) - circleIntegral(r, -r);
return r * r * Math.acos(1 - width/r) - (r - width) * Math.sqrt(width * (2 * r - width));
}

/** euclidean distance between two points */
Expand Down Expand Up @@ -1785,7 +1780,6 @@
exports.circleOverlap = circleOverlap;
exports.circleArea = circleArea;
exports.distance = distance;
exports.circleIntegral = circleIntegral;
exports.venn = venn;
exports.greedyLayout = greedyLayout;
exports.scaleSolution = scaleSolution;
Expand Down

0 comments on commit 49f3c21

Please sign in to comment.