绘制与AS3的蜂窝蜂窝

2023-09-08 14:13:38 作者:雨落倾城

我试图创建一个蜂窝 AS3 ,但我对细胞定位的一些问题。

I'm trying to create an honeycomb with as3 but I have some problem on cells positioning.

我已经创建的细胞(不与code)和的循环他们到一个 funcion 发送给它的东西我认为是必要的参数(蜂窝小区是媒体链接在舞台中央的精灵容器)。

I've already created the cells (not with code) and for cycled them to a funcion and send to it the parameters which what I thought was need (the honeycomb cell is allready on a sprite container in the center of the stage).

看周期的结构和它的参数传递,请参见下面的例子中,我唯一计算 placeCell 是我应该直接获得里面的角塔名为功能

to see the structure of the cycle and which parameters passes, please see the example below, the only thing i calculate in placeCell is the angle which I should obtain directly inside tha called function

注意:所述角度是相反的,但它并不重要,并且颜色是例如在有用的,只有用于可视分割例

Note: the angle is reversed but it isn't important, and the color are useful in example only for visually divide cases.

我对循环调用 placeCell 并通过细胞 current_case 柜台(指数)和蜂窝 cell_lv (电池级)。

My for cycle calls placeCell and passes cell, current_case, counter (index) and the honeycomb cell_lv (cell level).

我认为这是我需要的,但我不擅长几何和三角学,所以我不知道如何正确地定位细胞:

I thought it was what i needed but I'm not skilled in geometry and trigonometry, so I don't know how to position cells correctly:

    import flash.display.Sprite;
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

function createHoneycomb (cells:int):void {

    var honeycomb:Sprite = new Sprite ();
    addChild (honeycomb);

    var cell_lv:int = 1;
    var increment:int = 6;
    var tot_cur_cells:int = 1;

    var current_case:int = 0;
    var case_counter:int = 1;
    var case_length:int = 1;
    var max_cases:int = 6;

    var nucleus:Sprite = new cell (); // hexagon from library
    honeycomb.addChild (nucleus);

    for (var i:int = 1; i <= cells; i ++) {

        if (case_counter < case_length) {
            case_counter ++;
        } else {
            current_case ++;
            if (current_case > max_cases) current_case = 1;
            case_counter = 1;
        }

        if (i == tot_cur_cells) { // if i reach the current level
            if (i > 1) cell_lv ++;
            case_length = cell_lv;
            tot_cur_cells += (increment * cell_lv);
        }

        var current_cell:Sprite = new cell (); // hexagon from library
        honeycomb.addChild (current_cell);

        placeCell (current_cell, case_counter, current_case, cell_lv);

    }
    function centerHoneycomb (e:Event):void {
        honeycomb.x = stage.stageWidth / 2
        honeycomb.y = Math.round (stage.stageHeight / 2);
    }

    stage.addEventListener (Event.RESIZE, centerHoneycomb)
    stage.dispatchEvent (new Event (Event.RESIZE));
}

function placeCell (cell:Sprite, counter:int, current_case:int, cell_lv:int):void {
    var margin:int = 2;

    // THIS IS MY PROBLEM
    var start_degree:Number = (360 / (cell_lv * 6));
    var angle:Number = (start_degree * ((current_case - 1) + counter) - start_degree);
    var radius:Number = (cell.width + margin) * cell_lv;

    cell.x = radius * Math.cos (angle);
    cell.y = radius * Math.sin (angle);
    // end of the problem

    if (angle != 0) trace ("LV " + cell_lv + "  current_case " + current_case + "   counter " + counter + " angle " + angle + " radius " + radius);
    else trace ("LV " + cell_lv + " current_case " + current_case + "   counter " + counter + " angle " + angle + "     radius " + radius);
}

createHoneycomb (64);

如果您复制并粘贴此code,它的工作原理,但您需要创建一个六边形并调用它的动作库细胞

if you copy and paste this code, it works but you need to create an hexagon and call it in the actionscript library as cell

该怎么做才能解决这个问题?

how can I do to solve it?

我也想过用的情况下,开关对准它,但我认为是一个有点马车这样

I've also thought to use a switch with the cases to align it, but i think is a little bit buggy doing this

推荐答案

好了,我真的很喜欢这个问题。这是有趣的,和具有挑战性的,和我一个工作的结果。我没有使用任何您的code作为基础,虽然,但从头开始,所以这取决于你的最终用途,可能需要改变一下。

Okay, I really loved this question. It was interesting, and challenging, and I got a working result. I didn't use any of your code as the base though, but started from scratch, so depending on your final use, you might need to change a bit.

但是我没有创建类似的变量那些在你的细胞中(图中)。针对每个小区具有下列特性:

I did however created similar variables to those inside of your cells (in the picture). For each cell you have the following properties:

的迭代变量同样是你的细胞数的 中的半径的研究等于你的级别的,和前presses从中心(距离, 0 为中心) 中的位置的 P EX presses当前半径的位置 中的行业的取值等于你的情况的,但与零启动 点%R 等于你的首页的 the iterating variable i is equally to your cell number the radius r equals your level, and expresses the distance from the center (with 0 being the center) the position p expresses the position in the current radius the sector s equals your case, but starts with zero p % r equals your index

我没有一个角度,只是因为我不使用角度定位各六角形。相反,我基地六边形的(固定的)位置的位置半径为1和计算之间缺少的。

I don't have an angle, simply because I don't position the individual hexagons using an angle. Instead I base the position of the (fixed) positions of the hexagons at radius 1 and calculate the missing ones in between.

您可以看到细胞61(60 +中心),当然还有全$ C $上的​​ Wonderfl 。

You can see a working example with 61 cells (60 + the center), and of course the full code on Wonderfl.