In my program I have an array of circles, each with an x
, y
, and r
component. The r
(radius) is 10
for all of them, and there can be anywhere between 1 and 1000 circles contained within the list.
They are all clumped around each other, such that they look like this:
My question is, what is a good way to approximate the radius of the outer circle? It does not need to be very accurate at all, and I am mostly looking for a very fast way to calculate this.
My current solution is basically this:
const outerRadius = (10 * innerCount) / 2
which is not very accurate, so I am looking for something more accurate, but ideally still O(1)
.
CodePudding user response:
@mykaf, is something like this what you had in mind? Wrote it based on your comments, but not sure if there's a better way:
function calculateOuterRadius(innerCircleCount, innerCircleRadius) {
const factor = 1.33;
const area = Math.PI * innerCircleRadius * innerCircleRadius * innerCircleCount * factor;
return Math.sqrt(area / Math.PI);
}
An even better way, since Math.PI
cancels out:
function calculateOuterRadius2(innerCircleCount, innerCircleRadius) {
return Math.sqrt(innerCircleCount * innerCircleRadius * innerCircleRadius);
}
Which I think can be further reduced to this?
function calculateOuterRadius3(innerCircleCount, innerCircleRadius) {
return Math.sqrt(innerCircleCount) * innerCircleRadius;
}
CodePudding user response:
A small improvement on the OP's answer for two adjacent inner circles, where the approximation error is most extreme...
// count and radius refer to innerCircleCount and innerCircleRadius
function calculateOuterRadius(count, radius) {
return count === 2 ? 2 * radius : Math.sqrt(count) * radius;
}