What is an elegant way to position 8 circles around a point

℡╲_俬逩灬. 提交于 2019-12-07 15:41:13

问题


var circles:Array = new Array();


for(var i:int = 0; i < 8; i++)
{

    var ball:Ball = new Ball();
        ball.x = ???
        ball.y = ???
        circles.push(ball);
}

What is the best way to position balls around some point lets say in 5-10 distance of each other, is there some formula?


回答1:


for(var i:int = 0; i < 8; i++)
{
    var ball:Ball = new Ball();

    // Point has a useful static function for this, it takes two parameters
    // First, length, in other words how far from the center we want to be
    // Second, it wants the angle in radians, a complete circle is 2 * Math.PI
    // So, we're multiplying that with (i / 8) to place them equally far apart
    var pos:Point = Point.polar(50, (i / 8) * Math.PI * 2);

    // Finally, set the position of the ball
    ball.x = pos.x;
    ball.y = pos.y;

    circles.push(ball);
}



回答2:


I don't know actionscript3, so this exact code will not work, but it should give you a basic idea

for(int c = 0; c < 8; c++)
{
   Ball ball;
   ball.x = point.x;
   ball.y = point.y;
   ball.x += sin(toRadians((c/8) * 360));
   ball.y += cos(toRadians((c/8) * 360));
   circles.add(ball);
}

If you don't know what "sin" and "cos" do, or what "toRadians" means, just Google something like: "Sine Cosine Trigonometry". You'll find plenty of tutorials.

Here, I found this. It will teach you what "sin", "cos" and "radians" mean. http://www.khanacademy.org/math/trigonometry

Obviously you could just stick with grapefrukt's answer, it works, but if you want to know what's really going on behind the hood in "Point.polar", check out those videos.



来源:https://stackoverflow.com/questions/13780252/what-is-an-elegant-way-to-position-8-circles-around-a-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!