2

我正在尝试创建一个圆圈,阿拉:

圆具有给定的半径,circleRadius。环有一个最大半径,maxRingRadius。圆的数量可以是任意整数,circles需要计算,以及环的实际半径,ringRadius。圆圈,当它们的中心ringRadius距离环中心的单位时,应该完全接触,如图所示。

给定 acircleRadius和 a maxRingRadius,如何找到ringRadius适合整数的最近(或下一个最小) 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也可以是minRingRadiusapproximateRingRadius出于我的目的。但ringRadius应该定义下一个最近的“环”,它可以容纳整数个圆圈。


已解决: 解决方案的视觉确认 解决方案的视觉确认

4

1 回答 1

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您无法检查是否相等,因为总会出现表示错误。

更新哎呀,我没看到你也在询问定位圈子。一旦你知道了环半径和中心角,它就非常简单了。

于 2014-10-08T13:13:41.907 回答