用javascript指向一个正方形

时间:2012-12-03 09:30:11

标签: javascript math plot

我很好地在stackoverflow上给出了AtomicRobot下面的代码。

var x,y;

// amount of chairs
var totalChairs = 12;
// square size
var squareSize = 200;
var chairSize = 60;

// issues with 3,5,7,8,9,10,11

for(var i=0; i<totalChairs; i++){

    var angle = 2*Math.PI * i/totalChairs;

    if (angle > Math.PI/4 && angle <= Math.PI* 3/4){
        x = (squareSize/2) / Math.tan(angle);
        y = -squareSize/2 - chairSize/2;
    } else if (angle > Math.PI* 3/4 && angle <= Math.PI* 5/4){
        x = -squareSize/2 - chairSize/2;
        y = (squareSize/2) * Math.tan(angle);
    } else if (angle > Math.PI* 5/4 && angle <= Math.PI* 7/4){
        x = -(squareSize/2) / Math.tan(angle);
        y = -squareSize/2 + squareSize + chairSize/2;
    } else {
        x = -squareSize/2 + squareSize + chairSize/2;
        y = -(squareSize/2) * Math.tan(angle);
    }

    x -= Math.round(chairSize/2) - squareSize/2;
    y -= Math.round(chairSize/2) - squareSize/2;

    $("#square").append("<div class='chair' style='left:"+x+"px;top:"+y+"px;'></div>");
}

用javascript绘制桌子周围的椅子,它适用于以下数量的椅子1,2,4,6,12的距离均匀绘制。

但是随着椅子3,5,7,8,9,10,11的数量增加,似乎是椅子的宽度。

他们的任何人可以让我知道为什么这可能会发生数学并不是我的强项,我真的很感激这方面的帮助,你可以在这里看到一个有8把椅子的例子。

http://jsfiddle.net/Ld769/4/

由于

我已经取得了进展,这完全改变了我现在的做法,现在实际上了解这个是怎么回事,可以扩大表格以适应仍然有问题的remander对某些人的数字????

       var x,y;

// amount of chairs
var totalChairs = 18;
var chairSize = 60;
// square size
var squareSize = Math.round(totalChairs/4) * chairSize;

//alert("squareSize: " + squareSize);
// issues with 5,9,13,17,21
// works with 1,2,4,6,12
var remainder = Math.round(totalChairs/4);

var s1 = 0;
var s2 = 0;
var s3 = 0;
var s4 = 0;

for(var i=0; i<totalChairs; i++){

    var iter = i;  
    if(iter+1 <= remainder){

         var s1i = s1++;
         console.log("s1i: " + s1i);

         x = s1i * chairSize;
         y = -chairSize;
         newr = remainder*2;

    } else if(iter+1 <= newr){
         var s2i = s2++;
         console.log("s2i: " + s2i);
         y = s2i * chairSize;
         x= squareSize;
         newe = remainder*3;

    } else if(iter+1 <= newe){

         var s3i = s3++;
         console.log("s3i: " + s3i);
         x =  - s3i * chairSize  + squareSize - chairSize;
         y = squareSize;

    }else{
         var s4i = s4++;
         console.log("s4i: " + s4i);
         y = - s4i * chairSize + squareSize - chairSize;
         x= -chairSize;

     }         

    $("#square").css({"width":(squareSize) + "px","height":(squareSize) + "px"});
    $("#square").append("<div class='chair' style='left:"+Math.round(x)+"px;top:"+Math.round(y)+"px;'><p>"+i+"<br/>x"+x+",y"+y+"</p></div>");
    $(".chair").css({"width":(chairSize) + "px","height":(chairSize) + "px"});
}

可以在这里看到jsfiddle版本 http://jsfiddle.net/isimpledesign/Ld769/19/

1 个答案:

答案 0 :(得分:0)

我想知道有多少人需要适应4个方面之一。 4 * 2 = 8,不够,4 * 3 = 12 OK !,这意味着3个侧面得到3个,1个得到2.这意味着最小宽度和高度必须与最多侧面相同(以便坐姿适合舒适)

如果你想让它们均匀分开?只需计算一下椅子的数量,比如它是11,分成360来计算你的分离角度然后投射到正方形上