简体   繁体   中英

How to approximate the radius of a circle containing a group of circles?

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) .

@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;
}

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM