我试图创建一个圆圈,ala:
圆圈的半径为circleRadius
。环的最大半径为maxRingRadius
。圆圈的数量可以是任意整数,circles
,需要计算,以及环的实际半径ringRadius
。当圆圈从圆环中心放置ringRadius
单位时,圆圈应该完全接触,如图所示。
给定circleRadius
和maxRingRadius
,如何找到最接近(或次最小)ringRadius
的{{1}}整数,然后circles
定位那些圈子?
static Vector3[] RingOfCircles(float maxRingRadius, float circleRadius) {
//int circles = ...; // calculate this?
//float ringRadius = ...; // calculate this?
//Edit: Solution. These three lines are adapted from InBetween's GetNextSmallerRingRadius function but Unity3d-ized and without validation
int circles = Mathf.RoundToInt(Mathf.PI / Mathf.Asin(circleRadius / maxRingRadius));
float centralAngle = 2 * Mathf.PI / (numberOfCircles - 1);
float ringRadius = circleRadius / Mathf.Sin(centralAngle / 2);
// create ring of center points
float radsPerCircle = (Mathf.PI * 2) / circles;
Vector3[] centerPoints = new Vector3[circles];
for (int i=0; i < circles; i++) {
float angle = i * radsPerCircle;
centerPoints[i] = new Vector3(
Mathf.Sin(angle) * ringRadius,
Mathf.Cos(angle) * ringRadius,
0);
}
return centerPoints;
}
`
注意:maxRingRadius
也可以minRingRadius
或approximateRingRadius
用于我的目的。但是ringRadius
应该定义下一个最近的&#39;这可以容纳一大堆圈子。
解决: 视觉确认解决方案
答案 0 :(得分:1)
如果我理解你的问题,应该这样做:
public static double GetNextSmallerRingRadius(double startingRingRadius, double circleRadius)
{
Debug.Assert(startingRingRadius >= 0);
Debug.Assert(circleRadius > 0);
int currentNumberOfCircles = GetCurrentNumberOfCircles(startingRingRadius, circleRadius);
//Let's get trivial cases out of the way
if (currentNumberOfCircles == 1)
throw new ArgumentException();
if (currentNumberOfCircles == 2)
return 0; //trivial solution for 1 circle.
if (currentNumberOfCircles == 3)
return circleRadius; //trivial solution for 2 circles.
double centralAngle = 2 * Math.PI / (currentNumberOfCircles - 1);
return circleRadius / Math.Sin(centralAngle / 2);
}
public static double GetNextLargerRingRadius(double startingRingRadius, double circleRadius)
{
Debug.Assert(startingRingRadius >= 0);
Debug.Assert(circleRadius > 0);
int currentNumberOfCircles = GetCurrentNumberOfCircles(startingRingRadius, circleRadius);
//Let's get trivial cases out of the way
if (currentNumberOfCircles == 1)
return circleRadius; //trivial solution for 2 circles.
double centralAngle = 2 * Math.PI / (currentNumberOfCircles + 1);
return circleRadius / Math.Sin(centralAngle / 2);
}
private static int GetCurrentNumberOfCircles(double startingRingRadius, double circleRadius)
{
if (startingRingRadius == 0)
{
return 1;
}
else
{
return (int)Math.Round(Math.PI / Math.Asin(circleRadius / startingRingRadius), 0); //There would need to be some logic to make sure input values are correct.
}
}
要验证输入(定义的半径代表有效的解决方案),您可以比较舍入而不是舍入的numberOfcircles
,并确保差异在给定的容差范围内。请记住,使用double
,您无法检查是否存在相等性,因为始终存在表示错误。
更新糟糕,我没有看到你也在询问有关定位圈子的问题。一旦你知道了环半径和中心角,它就非常简单。